Top Description Methods
org.hibernate

public Interface Interceptor

Known Direct Implementers
org.hibernate.internal.EmptyInterceptor, org.hibernate.EmptyInterceptor
Imports
java.io.Serializable, java.util.Iterator, org.hibernate.metamodel.RepresentationMode, org.hibernate.metamodel.spi.EntityRepresentationStrategy, org.hibernate.type.Type

Allows user code to inspect and/or change entity property values before they are written to the database, or after they are read from the database.

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:

Whichever approach is used, the interceptor must be serializable for the Session to be serializable. This means that SessionFactory-scoped interceptors should implement readResolve().

Author
Gavin King
See Also
SessionBuilder#interceptor(Interceptor), SharedSessionBuilder#interceptor(), org.hibernate.cfg.Configuration#setInterceptor(Interceptor), org.hibernate.boot.SessionFactoryBuilder#applyInterceptor(Interceptor), org.hibernate.boot.SessionFactoryBuilder#applyStatelessInterceptor(Class)

Method Summary

Modifier and TypeMethod and Description
public default void
afterTransactionBegin(Transaction
The Hibernate transaction facade object
tx
)

Called when a Hibernate transaction is begun via the Hibernate Transaction API.

public default void
afterTransactionCompletion(Transaction
The Hibernate transaction facade object
tx
)

Called after a transaction is committed or rolled back.

public default void
beforeTransactionCompletion(Transaction
The Hibernate transaction facade object
tx
)

Called before a transaction is committed (but not before rollback).

public default int[]

Returns:

array of dirty property indices or null to indicate Hibernate should perform default behaviour
findDirty
(Object
The entity for which to find dirty properties.
entity
,
Serializable
The identifier of the entity
id
,
Object[]
The current entity state as taken from the entity instance
currentState
,
Object[]
The state of the entity when it was last synchronized (generally when it was loaded)
previousState
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called from flush().
public default int[]

Returns:

array of dirty property indices or null to indicate Hibernate should perform default behaviour
findDirty
(Object
The entity for which to find dirty properties.
entity
,
Object
The identifier of the entity
id
,
Object[]
The current entity state as taken from the entity instance
currentState
,
Object[]
The state of the entity when it was last synchronized (generally when it was loaded)
previousState
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called from flush().

public default Object

Returns:

a fully initialized entity
getEntity
(String
the name of the entity
entityName
,
Serializable
the instance identifier
id
)

Deprecated since 6.0. use getEntity(String, Object)
Get a fully loaded entity instance that is cached externally.
public default Object

Returns:

a fully initialized entity
getEntity
(String
the name of the entity
entityName
,
Object
the instance identifier
id
)

Get a fully loaded entity instance that is cached externally.

public default String

Returns:

the name of the entity
getEntityName
(Object
an entity instance
object
)

Get the entity name for a persistent or transient instance.

public default Object
instantiate(String entityName, EntityRepresentationStrategy representationStrategy, Object id)

Instantiate the entity.

public default Object
instantiate(String entityName, RepresentationMode representationMode, Object id)

Instantiate the entity.

public default Boolean

Returns:

Boolean or null to choose default behaviour
isTransient
(Object
a transient or detached entity
entity
)

Called to distinguish between transient and detached entities.

public default void
onCollectionRecreate(Object
The collection instance.
collection
,
Serializable
The collection key value.
key
)

Deprecated since 6.0. use onCollectionRecreate(Object, Object)
Called before a collection is (re)created.
public default void
onCollectionRecreate(Object
The collection instance.
collection
,
Object
The collection key value.
key
)

Called before a collection is (re)created.

public default void
onCollectionRemove(Object
The collection instance.
collection
,
Serializable
The collection key value.
key
)

Deprecated since 6.0. use onCollectionRemove(Object, Object)
Called before a collection is deleted.
public default void
onCollectionRemove(Object
The collection instance.
collection
,
Object
The collection key value.
key
)

Called before a collection is deleted.

public default void
onCollectionUpdate(Object
The collection instance.
collection
,
Serializable
The collection key value.
key
)

Deprecated since 6.0. use onCollectionUpdate(Object, Object)
Called before a collection is updated.
public default void
onCollectionUpdate(Object
The collection instance.
collection
,
Object
The collection key value.
key
)

Called before a collection is updated.

public default void
onDelete(Object
The entity instance being deleted
entity
,
Serializable
The identifier of the entity
id
,
Object[]
The state of the entity
state
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called before an object is deleted.
public default void
onDelete(Object
The entity instance being deleted
entity
,
Object
The identifier of the entity
id
,
Object[]
The state of the entity
state
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called before an object is deleted.

public default boolean

Returns:

true if the user modified the currentState in any way.
onFlushDirty
(Object
The entity instance detected as being dirty and being flushed
entity
,
Serializable
The identifier of the entity
id
,
Object[]
The entity's current state
currentState
,
Object[]
The entity's previous (load time) state.
previousState
,
String[]
The names of the entity properties
propertyNames
,
Type[]
The types of the entity properties
types
)

Called when an object is detected to be dirty, during a flush.
public default boolean

Returns:

true if the user modified the currentState in any way.
onFlushDirty
(Object
The entity instance detected as being dirty and being flushed
entity
,
Object
The identifier of the entity
id
,
Object[]
The entity's current state
currentState
,
Object[]
The entity's previous (load time) state.
previousState
,
String[]
The names of the entity properties
propertyNames
,
Type[]
The types of the entity properties
types
)

Called when an object is detected to be dirty, during a flush.

public default boolean

Returns:

true if the user modified the state in any way.
onLoad
(Object
The entity instance being loaded
entity
,
Serializable
The identifier value being loaded
id
,
Object[]
The entity state (which will be pushed into the entity instance)
state
,
String[]
The names of the entity properties, corresponding to the state.
propertyNames
,
Type[]
The types of the entity properties, corresponding to the state.
types
)

Called just before an object is initialized.
public default boolean

Returns:

true if the user modified the state in any way.
onLoad
(Object
The entity instance being loaded
entity
,
Object
The identifier value being loaded
id
,
Object[]
The entity state (which will be pushed into the entity instance)
state
,
String[]
The names of the entity properties, corresponding to the state.
propertyNames
,
Type[]
The types of the entity properties, corresponding to the state.
types
)

Called just before an object is initialized.

public default boolean

Returns:

true if the user modified the state in any way.
onSave
(Object
The entity instance whose state is being inserted
entity
,
Serializable
The identifier of the entity
id
,
Object[]
The state of the entity which will be inserted
state
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called before an object is saved.
public default boolean

Returns:

true if the user modified the state in any way.
onSave
(Object
The entity instance whose state is being inserted
entity
,
Object
The identifier of the entity
id
,
Object[]
The state of the entity which will be inserted
state
,
String[]
The names of the entity properties.
propertyNames
,
Type[]
The types of the entity properties
types
)

Called before an object is saved.

public default void
postFlush(Iterator<Object>
The entities that were flushed.
entities
)

Called after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.

public default void
preFlush(Iterator<Object>
The entities to be flushed.
entities
)

Called before a flush.

Method Detail

afterTransactionBeginback to summary
public default void afterTransactionBegin(Transaction tx)

Called when a Hibernate transaction is begun via the Hibernate Transaction API. Will not be called if transactions are being controlled via some other mechanism (CMT, for example).

Parameters
tx:Transaction

The Hibernate transaction facade object

afterTransactionCompletionback to summary
public default void afterTransactionCompletion(Transaction tx)

Called after a transaction is committed or rolled back.

Parameters
tx:Transaction

The Hibernate transaction facade object

beforeTransactionCompletionback to summary
public default void beforeTransactionCompletion(Transaction tx)

Called before a transaction is committed (but not before rollback).

Parameters
tx:Transaction

The Hibernate transaction facade object

findDirtyback to summary
public default int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)

Called from flush(). 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:Object

The entity for which to find dirty properties.

id:Serializable

The identifier of the entity

currentState:Object[]

The current entity state as taken from the entity instance

previousState:Object[]

The state of the entity when it was last synchronized (generally when it was loaded)

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Returns:int[]

array of dirty property indices or null to indicate Hibernate should perform default behaviour

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
findDirtyback to summary
public default int[] findDirty(Object entity, Object id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)

Called from flush(). 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:Object

The entity for which to find dirty properties.

id:Object

The identifier of the entity

currentState:Object[]

The current entity state as taken from the entity instance

previousState:Object[]

The state of the entity when it was last synchronized (generally when it was loaded)

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Returns:int[]

array of dirty property indices or null to indicate Hibernate should perform default behaviour

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
getEntityback to summary
public default Object getEntity(String entityName, Serializable id) throws CallbackException

Deprecated

since 6.0.

use getEntity(String, Object)

Get a fully loaded entity instance that is cached externally.

Parameters
entityName:String

the name of the entity

id:Serializable

the instance identifier

Returns:Object

a fully initialized entity

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
getEntityback to summary
public default Object getEntity(String entityName, Object id) throws CallbackException

Get a fully loaded entity instance that is cached externally.

Parameters
entityName:String

the name of the entity

id:Object

the instance identifier

Returns:Object

a fully initialized entity

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
getEntityNameback to summary
public default String getEntityName(Object object) throws CallbackException

Get the entity name for a persistent or transient instance.

Parameters
object:Object

an entity instance

Returns:String

the name of the entity

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
See Also
EntityNameResolver
instantiateback to summary
public default Object instantiate(String entityName, EntityRepresentationStrategy representationStrategy, Object id) throws CallbackException

Instantiate the entity. Return null to 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.

instantiateback to summary
public default Object instantiate(String entityName, RepresentationMode representationMode, Object id) throws CallbackException

Instantiate the entity. Return null to 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.

isTransientback to summary
public 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 transient
  • Boolean.FALSE - the entity is detached
  • null - Hibernate uses the unsaved-value mapping and other heuristics to determine if the object is unsaved
Parameters
entity:Object

a transient or detached entity

Returns:Boolean

Boolean or null to choose default behaviour

onCollectionRecreateback to summary
public default void onCollectionRecreate(Object collection, Serializable key) throws CallbackException

Deprecated

since 6.0.

use onCollectionRecreate(Object, Object)

Called before a collection is (re)created.

Parameters
collection:Object

The collection instance.

key:Serializable

The collection key value.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onCollectionRecreateback to summary
public default void onCollectionRecreate(Object collection, Object key) throws CallbackException

Called before a collection is (re)created.

Parameters
collection:Object

The collection instance.

key:Object

The collection key value.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onCollectionRemoveback to summary
public default void onCollectionRemove(Object collection, Serializable key) throws CallbackException

Deprecated

since 6.0.

use onCollectionRemove(Object, Object)

Called before a collection is deleted.

Parameters
collection:Object

The collection instance.

key:Serializable

The collection key value.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onCollectionRemoveback to summary
public default void onCollectionRemove(Object collection, Object key) throws CallbackException

Called before a collection is deleted.

Parameters
collection:Object

The collection instance.

key:Object

The collection key value.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onCollectionUpdateback to summary
public default void onCollectionUpdate(Object collection, Serializable key) throws CallbackException

Deprecated

since 6.0.

use onCollectionUpdate(Object, Object)

Called before a collection is updated.

Parameters
collection:Object

The collection instance.

key:Serializable

The collection key value.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onCollectionUpdateback to summary
public default void onCollectionUpdate(Object collection, Object key) throws CallbackException

Called before a collection is updated.

Parameters
collection:Object

The collection instance.

key:Object

The collection key value.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onDeleteback to summary
public default void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException

Called before an object is deleted. It is not recommended that the interceptor modify the state.

Parameters
entity:Object

The entity instance being deleted

id:Serializable

The identifier of the entity

state:Object[]

The state of the entity

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onDeleteback to summary
public 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 the state.

Parameters
entity:Object

The entity instance being deleted

id:Object

The identifier of the entity

state:Object[]

The state of the entity

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onFlushDirtyback to summary
public 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 detected currentState, 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 new currentState will be propagated to the object, but not necessarily (immediately) to the database. It is strongly recommended that the interceptor not modify the previousState.

API Note

The indexes across the currentState, previousState, propertyNames, and types arrays match.

Parameters
entity:Object

The entity instance detected as being dirty and being flushed

id:Serializable

The identifier of the entity

currentState:Object[]

The entity's current state

previousState:Object[]

The entity's previous (load time) state.

propertyNames:String[]

The names of the entity properties

types:Type[]

The types of the entity properties

Returns:boolean

true if the user modified the currentState in any way.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onFlushDirtyback to summary
public 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 detected currentState, 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 new currentState will be propagated to the object, but not necessarily (immediately) to the database. It is strongly recommended that the interceptor not modify the previousState.

API Note

The indexes across the currentState, previousState, propertyNames, and types arrays match.

Parameters
entity:Object

The entity instance detected as being dirty and being flushed

id:Object

The identifier of the entity

currentState:Object[]

The entity's current state

previousState:Object[]

The entity's previous (load time) state.

propertyNames:String[]

The names of the entity properties

types:Type[]

The types of the entity properties

Returns:boolean

true if the user modified the currentState in any way.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onLoadback to summary
public default boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException

Called just before an object is initialized. The interceptor may change the state, which will be propagated to the persistent object. Note that when this method is called, entity will be an empty uninitialized instance of the class.

API Note

The indexes across the state, propertyNames, and types arrays match.

Parameters
entity:Object

The entity instance being loaded

id:Serializable

The identifier value being loaded

state:Object[]

The entity state (which will be pushed into the entity instance)

propertyNames:String[]

The names of the entity properties, corresponding to the state.

types:Type[]

The types of the entity properties, corresponding to the state.

Returns:boolean

true if the user modified the state in any way.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onLoadback to summary
public 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 the state, which will be propagated to the persistent object. Note that when this method is called, entity will be an empty uninitialized instance of the class.

API Note

The indexes across the state, propertyNames, and types arrays match.

Parameters
entity:Object

The entity instance being loaded

id:Object

The identifier value being loaded

state:Object[]

The entity state (which will be pushed into the entity instance)

propertyNames:String[]

The names of the entity properties, corresponding to the state.

types:Type[]

The types of the entity properties, corresponding to the state.

Returns:boolean

true if the user modified the state in any way.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onSaveback to summary
public default boolean onSave(Object entity, Serializable 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:Object

The entity instance whose state is being inserted

id:Serializable

The identifier of the entity

state:Object[]

The state of the entity which will be inserted

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Returns:boolean

true if the user modified the state in any way.

Annotations
@Deprecated
since:6.0
Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
onSaveback to summary
public 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:Object

The entity instance whose state is being inserted

id:Object

The identifier of the entity

state:Object[]

The state of the entity which will be inserted

propertyNames:String[]

The names of the entity properties.

types:Type[]

The types of the entity properties

Returns:boolean

true if the user modified the state in any way.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
postFlushback to summary
public 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:Iterator<Object>

The entities that were flushed.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.
preFlushback to summary
public default void preFlush(Iterator<Object> entities) throws CallbackException

Called before a flush.

Parameters
entities:Iterator<Object>

The entities to be flushed.

Exceptions
CallbackException:
Thrown if the interceptor encounters any problems handling the callback.