Package org.hibernate
Interface Interceptor
-
- All Known Implementing Classes:
EmptyInterceptor
public interface InterceptorAllows user code to inspect and/or change entity property values before they are written to the database, or after the are read from the database.The
Sessionmay 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
Interceptorfor aSessionFactory, or a new instance might be created for eachSession. Use:AvailableSettings.INTERCEPTORto specify an interceptor shared between sessions, orAvailableSettings.SESSION_SCOPED_INTERCEPTORto specify that there is a dedicated instance of the interceptor for each session.
Sessionto be serializable. This means thatSessionFactory-scoped interceptors should implementreadResolve().
-
-
Method Summary
All Methods Instance Methods Default Methods Deprecated Methods Modifier and Type Method Description default voidafterTransactionBegin(Transaction tx)Called when a Hibernate transaction is begun via the HibernateTransactionAPI.default voidafterTransactionCompletion(Transaction tx)Called after a transaction is committed or rolled back.default voidbeforeTransactionCompletion(Transaction tx)Called before a transaction is committed (but not before rollback).default int[]findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)Deprecated.default int[]findDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)Called fromflush().default ObjectgetEntity(String entityName, Serializable id)Deprecated.default ObjectgetEntity(String entityName, Object id)Get 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, Serializable key)Deprecated.default voidonCollectionRecreate(Object collection, Object key)Called before a collection is (re)created.default voidonCollectionRemove(Object collection, Serializable key)Deprecated.default voidonCollectionRemove(Object collection, Object key)Called before a collection is deleted.default voidonCollectionUpdate(Object collection, Serializable key)Deprecated.default voidonCollectionUpdate(Object collection, Object key)Called before a collection is updated.default voidonDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)Deprecated.default voidonDelete(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)Called before an object is deleted.default booleanonFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)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 booleanonLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)Deprecated.default booleanonLoad(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)Called just before an object is initialized.default booleanonSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)Deprecated.default booleanonSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)Called before an object is saved.default voidpostFlush(Iterator<Object> entities)Called after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.default voidpreFlush(Iterator<Object> entities)Called before a flush.
-
-
-
Method Detail
-
onLoad
@Deprecated(since="6.0") default boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
Deprecated.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. NOTE: The indexes across thestate,propertyNamesandtypesarrays match.- 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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onLoad
default boolean onLoad(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
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. NOTE: The indexes across thestate,propertyNamesandtypesarrays match.- 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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onFlushDirty
@Deprecated(since="6.0") default boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException
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. NOTE: The indexes across thecurrentState,previousState,propertyNamesandtypesarrays match.- 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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onFlushDirty
default boolean onFlushDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException
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. NOTE: The indexes across thecurrentState,previousState,propertyNamesandtypesarrays match.- 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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onSave
@Deprecated(since="6.0") default boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
Deprecated.Called before an object is saved. The interceptor may modify thestate, 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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onSave
default boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
Called before an object is saved. The interceptor may modify the state, which will be used for the SQL INSERT and 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:
- true if the user modified the state in any way.
- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onDelete
@Deprecated(since="6.0") default void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
Deprecated.Called before an object is deleted. It is not recommended that the interceptor modify thestate.- 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- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onDelete
default void onDelete(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
Called before an object is deleted. It is not recommended that the interceptor modify thestate.- 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- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionRecreate
@Deprecated(since="6.0") default void onCollectionRecreate(Object collection, Serializable key) throws CallbackException
Deprecated.Called before a collection is (re)created.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionRecreate
default void onCollectionRecreate(Object collection, Object key) throws CallbackException
Called before a collection is (re)created.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionRemove
@Deprecated(since="6.0") default void onCollectionRemove(Object collection, Serializable key) throws CallbackException
Deprecated.Called before a collection is deleted.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionRemove
default void onCollectionRemove(Object collection, Object key) throws CallbackException
Called before a collection is deleted.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionUpdate
@Deprecated(since="6.0") default void onCollectionUpdate(Object collection, Serializable key) throws CallbackException
Deprecated.Called before a collection is updated.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
onCollectionUpdate
default void onCollectionUpdate(Object collection, Object key) throws CallbackException
Called before a collection is updated.- Parameters:
collection- The collection instance.key- The collection key value.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
preFlush
default void preFlush(Iterator<Object> entities) throws CallbackException
Called before a flush.- Parameters:
entities- The entities to be flushed.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
postFlush
default void postFlush(Iterator<Object> entities) throws CallbackException
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.- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
isTransient
default Boolean isTransient(Object entity)
Called to distinguish between transient and detached entities. The return value determines the state of the entity with respect to the current session.Boolean.TRUE- the entity is transientBoolean.FALSE- the entity is detachednull- Hibernate uses theunsaved-valuemapping and other heuristics to determine if the object is unsaved
- Parameters:
entity- a transient or detached entity- Returns:
- Boolean or
nullto choose default behaviour
-
findDirty
@Deprecated(since="6.0") default int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
Deprecated.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 - Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
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 - Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
instantiate
default Object instantiate(String entityName, EntityRepresentationStrategy representationStrategy, Object id) throws CallbackException
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.- Throws:
CallbackException
-
instantiate
default Object instantiate(String entityName, RepresentationMode representationMode, Object id) throws CallbackException
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.- Throws:
CallbackException
-
getEntityName
default String getEntityName(Object object) throws CallbackException
Get the entity name for a persistent or transient instance.- Parameters:
object- an entity instance- Returns:
- the name of the entity
- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
getEntity
@Deprecated(since="6.0") default Object getEntity(String entityName, Serializable id) throws CallbackException
Deprecated.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
- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
getEntity
default Object getEntity(String entityName, Object id) throws CallbackException
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
- Throws:
CallbackException- Thrown if the interceptor encounters any problems handling the callback.
-
afterTransactionBegin
default void afterTransactionBegin(Transaction tx)
Called when a Hibernate transaction is begun via the HibernateTransactionAPI. Will not be called if transactions are being controlled via some other mechanism (CMT, for example).- Parameters:
tx- The Hibernate transaction facade object
-
beforeTransactionCompletion
default void beforeTransactionCompletion(Transaction tx)
Called before a transaction is committed (but not before rollback).- Parameters:
tx- The Hibernate transaction facade object
-
afterTransactionCompletion
default void afterTransactionCompletion(Transaction tx)
Called after a transaction is committed or rolled back.- Parameters:
tx- The Hibernate transaction facade object
-
-