Skip to content

Commit 1a1301d

Browse files
committed
Refine documentation of BeforeConvert/BeforeSave lifecycle hooks.
Closes #1448
1 parent 6d16857 commit 1a1301d

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/AfterConvertEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.datastax.oss.driver.api.core.cql.Row;
2424

2525
/**
26-
* Event to be triggered after converting a {@link Row}.
26+
* Event to be triggered after converting a {@link Row} into an entity.
2727
*
2828
* @author Mark Paluch
2929
* @since 2.1

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/AfterLoadEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public AfterLoadEvent(Row source, Class<T> type, CqlIdentifier tableName) {
5353
/**
5454
* Returns the type for which the {@link AfterLoadEvent} shall be invoked for.
5555
*
56-
* @return
56+
* @return the type for which the {@link AfterLoadEvent} shall be invoked for.
5757
*/
5858
public Class<T> getType() {
5959
return type;

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/BeforeConvertCallback.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
import org.springframework.data.mapping.callback.EntityCallback;
1919

2020
import com.datastax.oss.driver.api.core.CqlIdentifier;
21+
import com.datastax.oss.driver.api.core.cql.Statement;
2122

2223
/**
23-
* Callback being invoked before a domain object is converted to be persisted.
24+
* Callback being invoked before a domain object is converted to be persisted. Entity callback invoked before converting
25+
* a domain object to a {@code INSERT}/{@code UPDATE} {@link Statement}. This is useful to apply changes to the domain
26+
* objects to that these will be reflected in the generated {@link Statement}.
2427
*
2528
* @author Mark Paluch
2629
* @since 2.2
2730
* @see org.springframework.data.mapping.callback.EntityCallbacks
31+
* @see BeforeSaveCallback
2832
*/
2933
@FunctionalInterface
3034
public interface BeforeConvertCallback<T> extends EntityCallback<T> {

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/BeforeSaveCallback.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@
2121
import com.datastax.oss.driver.api.core.cql.Statement;
2222

2323
/**
24-
* Entity callback triggered before save of a row.
24+
* Entity callback invoked before inserting or updating a row in the database. Before save is invoked after
25+
* {@link BeforeConvertCallback converting the entity} into a {@link Statement}. This is useful to let the mapping layer
26+
* derive values into the statement while the save callback can either update the domain object without reflecting the
27+
* changes in the statement. Another use is to inspect the {@link Statement}.
2528
*
2629
* @author Mark Paluch
2730
* @since 2.2
2831
* @see org.springframework.data.mapping.callback.EntityCallbacks
32+
* @see BeforeConvertCallback
2933
*/
3034
@FunctionalInterface
3135
public interface BeforeSaveCallback<T> extends EntityCallback<T> {
3236
// TODO: Mutable statements
3337
/**
34-
* Entity callback method invoked before a domain object is saved. Can return either the same of a modified instance
35-
* of the domain object and can modify {@link Statement} contents. This method is called after converting the
36-
* {@code entity} to {@link Statement} so effectively the row is used as outcome of invoking this callback.
38+
* Entity callback method invoked before save. That is, before running the {@code INSERT}/{@code UPDATE}
39+
* {@link Statement} derived from the intent to save an object. Can return either the same of a modified instance of
40+
* the domain object and can inspect the {@link Statement} contents. This method is called after converting the
41+
* {@code entity} to {@link Statement} so effectively the entity is propagated as outcome of invoking this callback.
3742
*
3843
* @param entity the domain object to save.
3944
* @param tableName name of the table.

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/BeforeSaveEvent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import com.datastax.oss.driver.api.core.cql.Statement;
2222

2323
/**
24-
* {@link CassandraMappingEvent} triggered before save of an object.
24+
* {@link CassandraMappingEvent Mapping event} triggered before inserting or updating a row in the database. Before save
25+
* is invoked after {@link BeforeConvertCallback converting the entity} into a {@link Statement}. This is useful to let
26+
* the mapping layer derive values into the statement while the save callback can either update the domain object
27+
* without reflecting the changes in the statement. Another use is to inspect the {@link Statement}.
2528
*
2629
* @author Lukasz Antoniak
2730
* @author Mark Paluch

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/ReactiveBeforeConvertCallback.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import org.springframework.data.mapping.callback.EntityCallback;
2020

2121
import com.datastax.oss.driver.api.core.CqlIdentifier;
22+
import com.datastax.oss.driver.api.core.cql.Statement;
2223

2324
/**
24-
* Callback being invoked before a domain object is converted to be persisted.
25+
* Callback being invoked before a domain object is converted to be persisted. Entity callback invoked before converting
26+
* a domain object to a {@code INSERT}/{@code UPDATE} {@link Statement}. This is useful to apply changes to the domain
27+
* objects to that these will be reflected in the generated {@link Statement}.
2528
*
2629
* @author Mark Paluch
2730
* @since 2.2
2831
* @see org.springframework.data.mapping.callback.ReactiveEntityCallbacks
32+
* @see ReactiveBeforeSaveCallback
2933
*/
3034
@FunctionalInterface
3135
public interface ReactiveBeforeConvertCallback<T> extends EntityCallback<T> {

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/event/ReactiveBeforeSaveCallback.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,24 @@
2222
import com.datastax.oss.driver.api.core.cql.Statement;
2323

2424
/**
25-
* Entity callback triggered before save of a row.
25+
* Entity callback invoked before inserting or updating a row in the database. Before save is invoked after
26+
* {@link BeforeConvertCallback converting the entity} into a {@link Statement}. This is useful to let the mapping layer
27+
* derive values into the statement while the save callback can either update the domain object without reflecting the
28+
* changes in the statement. Another use is to inspect the {@link Statement}.
2629
*
2730
* @author Mark Paluch
2831
* @since 2.2
2932
* @see org.springframework.data.mapping.callback.ReactiveEntityCallbacks
33+
* @see ReactiveBeforeConvertCallback
3034
*/
3135
@FunctionalInterface
3236
public interface ReactiveBeforeSaveCallback<T> extends EntityCallback<T> {
3337
// TODO: Mutable statements
3438
/**
35-
* Entity callback method invoked before a domain object is saved. Can return either the same of a modified instance
36-
* of the domain object and can modify {@link Statement} contents. This method is called after converting the
37-
* {@code entity} to {@link Statement} so effectively the row is used as outcome of invoking this callback.
39+
* Entity callback method invoked before save. That is, before running the {@code INSERT}/{@code UPDATE}
40+
* {@link Statement} derived from the intent to save an object. Can return either the same of a modified instance of
41+
* the domain object and can inspect the {@link Statement} contents. This method is called after converting the
42+
* {@code entity} to {@link Statement} so effectively the entity is propagated as outcome of invoking this callback.
3843
*
3944
* @param entity the domain object to save.
4045
* @param tableName name of the table.

src/main/antora/modules/ROOT/pages/cassandra/events.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Declaring these beans in your Spring `ApplicationContext` will cause them to be
2121

2222
The `AbstractCassandraEventListener` has the following callback methods:
2323

24-
* `onBeforeSave`: Called in `CassandraTemplate.insert(…)` and `.update(…)` operations before inserting or updating a row in the database.
24+
* `onBeforeSave`: Called in `CassandraTemplate.insert(…)` and `.update(…)` operations before inserting or updating a row in the database but after creating the `Statement`.
2525
* `onAfterSave`: Called in `CassandraTemplate…insert(…)` and `.update(…)` operations after inserting or updating a row in the database.
2626
* `onBeforeDelete`: Called in `CassandraTemplate.delete(…)` operations before deleting row from the database.
2727
* `onAfterDelete`: Called in `CassandraTemplate.delete(…)` operations after deleting row from the database.
@@ -34,7 +34,7 @@ Complex types used as properties within an aggregate root are not subject to eve
3434
include::{commons}@data-commons::page$entity-callbacks.adoc[leveloffset=+1]
3535

3636
[[cassandra.entity-callbacks]]
37-
=== Store specific EntityCallbacks
37+
=== Store-specific EntityCallbacks
3838

3939
Spring Data for Apache Cassandra uses the `EntityCallback` API for its auditing support and reacts on the following callbacks.
4040

@@ -49,7 +49,8 @@ Spring Data for Apache Cassandra uses the `EntityCallback` API for its auditing
4949
| `ReactiveBeforeConvertCallback`
5050
`BeforeConvertCallback`
5151
| `onBeforeConvert(T entity, CqlIdentifier tableName)`
52-
| Invoked before a domain object is converted to `com.datastax.driver.core.Statement`.
52+
| Invoked before a domain object is converted to `Statement`.
53+
Domain objects can be updated to include the change in the `Statement`.
5354
| `Ordered.LOWEST_PRECEDENCE`
5455

5556
| `ReactiveAuditingEntityCallback`
@@ -62,7 +63,7 @@ Spring Data for Apache Cassandra uses the `EntityCallback` API for its auditing
6263
`BeforeSaveCallback`
6364
| `onBeforeSave(T entity, CqlIdentifier tableName, Statement statement)`
6465
| Invoked before a domain object is saved. +
65-
Can modify the target, to be persisted, `com.datastax.driver.core.Statement` containing all mapped entity information.
66+
Can modify the target object after the `Statement` has been created. The provided statement contains all mapped entity information but changes to the domain object are not included in the `Statement`.
6667
| `Ordered.LOWEST_PRECEDENCE`
6768

6869
|===

0 commit comments

Comments
 (0)