Interface Interceptor
- All Known Implementing Classes:
EmptyInterceptor
- For a stateful session, the callbacks
onLoad(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]),onPersist(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]),onRemove(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]), andonFlushDirty(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])reflect the basic lifecycle of a managed entity. - For a stateless session, the relevant callbacks
are
onLoad(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]),onInsert(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]),onUpdate(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]),onUpsert(java.lang.Object, java.lang.Object, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[]), andonDelete.
The Session may not be invoked from a callback (nor may a callback cause
a collection or proxy to be lazily initialized).
There might be a single instance of Interceptor for a SessionFactory,
or a new instance might be created for each Session. Use:
SessionEventSettings.INTERCEPTORto specify an interceptor shared between sessions, orSessionEventSettings.SESSION_SCOPED_INTERCEPTORto specify that there is a dedicated instance of the interceptor for each session.
Whichever approach is used, the interceptor must be serializable for the
Session to be serializable. This means that SessionFactory-scoped
interceptors should implement readResolve().
- See Also:
- API Note:
- This venerable callback interface, dating from the very earliest days of
Hibernate, competes with standard JPA entity listener callbacks:
PostLoad,PrePersist,PreUpdate, andPreRemove. However, JPA callbacks do not provide the ability to access the previous value of an updated property in a@PreUpdatecallback, and do not provide a well-defined way to intercept changes to collections.Note that this API exposes the interface
Type, which in modern versions of Hibernate is considered an SPI. This is unfortunate, and might change in the future, but is bearable for now.
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidCalled when a Hibernate transaction is begun via the JPA-standardEntityTransactionAPI, or viaTransaction.default voidCalled after a transaction is committed or rolled back.default voidCalled before a transaction is committed (but not before rollback).default int[]findDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) Called fromflush().default ObjectGet a fully loaded entity instance that is cached externally.default StringgetEntityName(Object object) Get the entity name for a persistent or transient instance.default Objectinstantiate(String entityName, RepresentationMode representationMode, Object id) Instantiate the entity.default Objectinstantiate(String entityName, EntityRepresentationStrategy representationStrategy, Object id) Instantiate the entity.default BooleanisTransient(Object entity) Called to distinguish between transient and detached entities.default voidonCollectionRecreate(Object collection, Object key) Called before a collection is (re)created.default voidonCollectionRemove(Object collection, Object key) Called before a collection is deleted.default voidonCollectionUpdate(Object collection, Object key) Called before a collection is updated.default voidDeprecated.default voidCalled before a record is deleted by aStatelessSession.default booleanonFlushDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) Called when an object is detected to be dirty, during a flush.default voidCalled before a record is inserted by aStatelessSession.default booleanCalled just before an object is initialized.default booleanCalled before an object is made persistent by a stateful session.default voidCalled before an object is removed by a stateful session.default booleanDeprecated.default voidCalled before a record is updated by aStatelessSession.default voidCalled before a record is upserted by aStatelessSession.default voidCalled after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.default voidpostMerge(Object source, Object target, Object id, Object[] targetState, Object[] originalState, String[] propertyNames, Type[] propertyTypes) Called after copying the state of a merged entity to a managed entity belonging to the persistence context of a statefulSession.default voidCalled before a flush.default voidCalled before copying the state of a merged entity to a managed entity belonging to the persistence context of a statefulSession.
-
Method Details
-
onLoad
default boolean onLoad(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) Called just before an object is initialized. The interceptor may change thestate, which will be propagated to the persistent object. Note that when this method is called,entitywill be an empty uninitialized instance of the class.- Parameters:
entity- The entity instance being loadedid- The identifier value being loadedstate- The entity state (which will be pushed into the entity instance)propertyNames- The names of the entity properties, corresponding to thestate.types- The types of the entity properties, corresponding to thestate.- Returns:
trueif the user modified thestatein any way.- API Note:
- The indexes across the
state,propertyNames, andtypesarrays match.
-
onPersist
default boolean onPersist(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) Called before an object is made persistent by a stateful session.The interceptor may modify the
state, which will be used for the SQLINSERTand propagated to the persistent object.- Parameters:
entity- The entity instance whose state is being insertedid- The identifier of the entitystate- The state of the entity which will be insertedpropertyNames- The names of the entity properties.types- The types of the entity properties- Returns:
trueif the user modified thestatein any way.- See Also:
-
onRemove
default void onRemove(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) Called before an object is removed by a stateful session.It is not recommended that the interceptor modify the
state.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitystate- The state of the entitypropertyNames- The names of the entity properties.types- The types of the entity properties- See Also:
-
onFlushDirty
default boolean onFlushDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) Called when an object is detected to be dirty, during a flush. The interceptor may modify the detectedcurrentState, which will be propagated to both the database and the persistent object. Note that not all flushes end in actual synchronization with the database, in which case the newcurrentStatewill be propagated to the object, but not necessarily (immediately) to the database. It is strongly recommended that the interceptor not modify thepreviousState.- Parameters:
entity- The entity instance detected as being dirty and being flushedid- The identifier of the entitycurrentState- The entity's current statepreviousState- The entity's previous (load time) state.propertyNames- The names of the entity propertiestypes- The types of the entity properties- Returns:
trueif the user modified thecurrentStatein any way.- See Also:
- API Note:
- The indexes across the
currentState,previousState,propertyNames, andtypesarrays match.
-
onSave
@Deprecated(since="6.6") default boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) Deprecated.Called before an object is made persistent by a stateful session.The interceptor may modify the
state, which will be used for the SQLINSERTand propagated to the persistent object.- Parameters:
entity- The entity instance whose state is being insertedid- The identifier of the entitystate- The state of the entity which will be insertedpropertyNames- The names of the entity properties.types- The types of the entity properties- Returns:
trueif the user modified thestatein any way.- See Also:
-
onDelete
@Deprecated(since="6.6") default void onDelete(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) Deprecated.Called before an object is removed by a stateful session.It is not recommended that the interceptor modify the
state.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitystate- The state of the entitypropertyNames- The names of the entity properties.types- The types of the entity properties- See Also:
-
onCollectionRecreate
Called before a collection is (re)created.- Parameters:
collection- The collection instance.key- The collection key value.
-
onCollectionRemove
Called before a collection is deleted.- Parameters:
collection- The collection instance.key- The collection key value.
-
onCollectionUpdate
Called before a collection is updated.- Parameters:
collection- The collection instance.key- The collection key value.
-
preFlush
Called before a flush.- Parameters:
entities- The entities to be flushed.
-
postFlush
Called after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.- Parameters:
entities- The entities that were flushed.
-
isTransient
Called to distinguish between transient and detached entities. The return value determines the state of the entity with respect to the current session. This method should return:Boolean.TRUEif the entity is transient,Boolean.FALSEif the entity is detached, ornullto signal that the usual heuristics should be used to determine if the instance is transient
- Parameters:
entity- a transient or detached entity- Returns:
Booleanornullto choose default behaviour
-
findDirty
default int[] findDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) Called fromflush(). The return value determines whether the entity is updated- an array of property indices - the entity is dirty
- an empty array - the entity is not dirty
null- use Hibernate's default dirty-checking algorithm
- Parameters:
entity- The entity for which to find dirty properties.id- The identifier of the entitycurrentState- The current entity state as taken from the entity instancepreviousState- The state of the entity when it was last synchronized (generally when it was loaded)propertyNames- The names of the entity properties.types- The types of the entity properties- Returns:
- array of dirty property indices or
nullto indicate Hibernate should perform default behaviour
-
instantiate
default Object instantiate(String entityName, EntityRepresentationStrategy representationStrategy, Object id) Instantiate the entity. Returnnullto indicate that Hibernate should use the default constructor of the class. The identifier property of the returned instance should be initialized with the given identifier. -
instantiate
Instantiate the entity. Returnnullto indicate that Hibernate should use the default constructor of the class. The identifier property of the returned instance should be initialized with the given identifier. -
getEntityName
Get the entity name for a persistent or transient instance.- Parameters:
object- an entity instance- Returns:
- the name of the entity
- See Also:
-
getEntity
Get a fully loaded entity instance that is cached externally.- Parameters:
entityName- the name of the entityid- the instance identifier- Returns:
- a fully initialized entity
-
afterTransactionBegin
Called when a Hibernate transaction is begun via the JPA-standardEntityTransactionAPI, or viaTransaction. This method is not be called if transactions are being controlled via some other mechanism, for example, if transactions are managed by a container.- Parameters:
tx- The Hibernate transaction facade object
-
beforeTransactionCompletion
Called before a transaction is committed (but not before rollback).- Parameters:
tx- The Hibernate transaction facade object
-
afterTransactionCompletion
Called after a transaction is committed or rolled back.- Parameters:
tx- The Hibernate transaction facade object
-
onInsert
default void onInsert(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) Called before a record is inserted by aStatelessSession.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitystate- The entity statepropertyNames- The names of the entity properties.propertyTypes- The types of the entity properties- See Also:
-
onUpdate
default void onUpdate(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) Called before a record is updated by aStatelessSession.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitystate- The entity statepropertyNames- The names of the entity propertiespropertyTypes- The types of the entity properties- See Also:
-
onUpsert
default void onUpsert(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) Called before a record is upserted by aStatelessSession.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitystate- The entity statepropertyNames- The names of the entity propertiespropertyTypes- The types of the entity properties- See Also:
-
onDelete
Called before a record is deleted by aStatelessSession.- Parameters:
entity- The entity instance being deletedid- The identifier of the entitypropertyNames- The names of the entity propertiespropertyTypes- The types of the entity properties- See Also:
-
preMerge
@Incubating default void preMerge(Object entity, Object[] state, String[] propertyNames, Type[] propertyTypes) Called before copying the state of a merged entity to a managed entity belonging to the persistence context of a statefulSession.The interceptor may modify the
state.- Parameters:
entity- The entity passed tomerge()state- The state of the entity passed tomerge()propertyNames- The names of the entity propertiespropertyTypes- The types of the entity properties- Since:
- 7.1
-
postMerge
@Incubating default void postMerge(Object source, Object target, Object id, Object[] targetState, Object[] originalState, String[] propertyNames, Type[] propertyTypes) Called after copying the state of a merged entity to a managed entity belonging to the persistence context of a statefulSession.Modification of the
sourceStateortargetStatehas no effect.- Parameters:
source- The entity passed tomerge()target- The target managed entityid- The identifier of the managed entitytargetState- The copied state already assigned to the target managed entityoriginalState- The original state of the target managed entity before assignment of the copied state, ornullif the target entity is a new instancepropertyNames- The names of the entity propertiespropertyTypes- The types of the entity properties- Since:
- 7.1
-
onRemove(Object, Object, Object[], String[], Type[])