Lifecycle Events

Spring Data JDBC publishes lifecycle events to ApplicationListener objects, typically beans in the application context. Events are notifications about a certain lifecycle phase. In contrast to entity callbacks, events are intended for notification. Transactional listeners will receive events when the transaction completes. Events and callbacks get only triggered for aggregate roots. If you want to process non-root entities, you need to do that through a listener for the containing aggregate root.spring-doc.cn

Entity lifecycle events can be costly, and you may notice a change in the performance profile when loading large result sets. You can disable lifecycle events on Template API.spring-doc.cn

For example, the following listener gets invoked before an aggregate gets saved:spring-doc.cn

@Bean
ApplicationListener<BeforeSaveEvent<Object>> loggingSaves() {

	return event -> {

		Object entity = event.getEntity();
		LOG.info("{} is getting saved.", entity);
	};
}

If you want to handle events only for a specific domain type you may derive your listener from AbstractRelationalEventListener and overwrite one or more of the onXXX methods, where XXX stands for an event type. Callback methods will only get invoked for events related to the domain type and their subtypes, therefore you don’t require further casting.spring-doc.cn

class PersonLoadListener extends AbstractRelationalEventListener<Person> {

	@Override
	protected void onAfterLoad(AfterLoadEvent<Person> personLoad) {
		LOG.info(personLoad.getEntity());
	}
}

The following table describes the available events.For more details about the exact relation between process steps see the description of available callbacks which map 1:1 to events.spring-doc.cn

Table 1. Available events
Event When It Is Published

BeforeDeleteEventspring-doc.cn

Before an aggregate root gets deleted.spring-doc.cn

AfterDeleteEventspring-doc.cn

After an aggregate root gets deleted.spring-doc.cn

BeforeConvertEventspring-doc.cn

Before an aggregate root gets converted into a plan for executing SQL statements, but after the decision was made if the aggregate is new or not, i.e. if an update or an insert is in order.spring-doc.cn

BeforeSaveEventspring-doc.cn

Before an aggregate root gets saved (that is, inserted or updated but after the decision about whether if it gets inserted or updated was made).spring-doc.cn

AfterSaveEventspring-doc.cn

After an aggregate root gets saved (that is, inserted or updated).spring-doc.cn

AfterConvertEventspring-doc.cn

After an aggregate root gets created from a database ResultSet and all its properties get set.spring-doc.cn

Lifecycle events depend on an ApplicationEventMulticaster, which in case of the SimpleApplicationEventMulticaster can be configured with a TaskExecutor, and therefore gives no guarantees when an Event is processed.

Store-specific EntityCallbacks

Spring Data JDBC uses the EntityCallback API for its auditing support and reacts on the callbacks listed in the following table.spring-doc.cn

Table 2. Process Steps and Callbacks of the Different Processes performed by Spring Data JDBC.
Process EntityCallback / Process Step Comment

Deletespring-doc.cn

BeforeDeleteCallbackspring-doc.cn

Before the actual deletion.spring-doc.cn

The aggregate root and all the entities of that aggregate get removed from the database.spring-doc.cn

AfterDeleteCallbackspring-doc.cn

After an aggregate gets deleted.spring-doc.cn

Savespring-doc.cn

Determine if an insert or an update of the aggregate is to be performed dependent on if it is new or not.spring-doc.cn

BeforeConvertCallbackspring-doc.cn

This is the correct callback if you want to set an id programmatically. In the previous step new aggregates got detected as such and a Id generated in this step would be used in the following step.spring-doc.cn

Convert the aggregate to a aggregate change, it is a sequence of SQL statements to be executed against the database. In this step the decision is made if an Id is provided by the aggregate or if the Id is still empty and is expected to be generated by the database.spring-doc.cn

BeforeSaveCallbackspring-doc.cn

Changes made to the aggregate root may get considered, but the decision if an id value will be sent to the database is already made in the previous step. Do not use this for creating Ids for new aggregates. Use BeforeConvertCallback instead.spring-doc.cn

The SQL statements determined above get executed against the database.spring-doc.cn

AfterSaveCallbackspring-doc.cn

After an aggregate root gets saved (that is, inserted or updated).spring-doc.cn

Loadspring-doc.cn

Load the aggregate using 1 or more SQL queries. Construct the aggregate from the resultset.spring-doc.cn

AfterConvertCallbackspring-doc.cn

We encourage the use of callbacks over events since they support the use of immutable classes and therefore are more powerful and versatile than events.spring-doc.cn