mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-18 09:05:21 +00:00
HHH-17955 add Interceptor callbacks for StatelessSession
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
9a4d21d71d
commit
dd77ef651a
@ -148,6 +148,8 @@ default boolean onFlushDirty(
|
|||||||
* @return {@code true} if the user modified the {@code currentState} in any way.
|
* @return {@code true} if the user modified the {@code currentState} in any way.
|
||||||
*
|
*
|
||||||
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see Session#flush()
|
||||||
*/
|
*/
|
||||||
default boolean onFlushDirty(
|
default boolean onFlushDirty(
|
||||||
Object entity,
|
Object entity,
|
||||||
@ -163,7 +165,9 @@ default boolean onFlushDirty(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called before an object is saved. The interceptor may modify the {@code state}, which will be used for
|
* Called before an object is made persistent by a stateful session.
|
||||||
|
* <p>
|
||||||
|
* The interceptor may modify the {@code state}, which will be used for
|
||||||
* the SQL {@code INSERT} and propagated to the persistent object.
|
* the SQL {@code INSERT} and propagated to the persistent object.
|
||||||
*
|
*
|
||||||
* @param entity The entity instance whose state is being inserted
|
* @param entity The entity instance whose state is being inserted
|
||||||
@ -185,7 +189,9 @@ default boolean onSave(Object entity, Serializable id, Object[] state, String[]
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called before an object is saved. The interceptor may modify the {@code state}, which will be used for
|
* Called before an object is made persistent by a stateful session.
|
||||||
|
* <p>
|
||||||
|
* The interceptor may modify the {@code state}, which will be used for
|
||||||
* the SQL {@code INSERT} and propagated to the persistent object.
|
* the SQL {@code INSERT} and propagated to the persistent object.
|
||||||
*
|
*
|
||||||
* @param entity The entity instance whose state is being inserted
|
* @param entity The entity instance whose state is being inserted
|
||||||
@ -197,6 +203,10 @@ default boolean onSave(Object entity, Serializable id, Object[] state, String[]
|
|||||||
* @return {@code true} if the user modified the {@code state} in any way.
|
* @return {@code true} if the user modified the {@code state} in any way.
|
||||||
*
|
*
|
||||||
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see Session#persist(Object)
|
||||||
|
* @see Session#merge(Object)
|
||||||
|
* @see Session#save(Object)
|
||||||
*/
|
*/
|
||||||
default boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)
|
default boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)
|
||||||
throws CallbackException {
|
throws CallbackException {
|
||||||
@ -206,7 +216,9 @@ default boolean onSave(Object entity, Object id, Object[] state, String[] proper
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Called before an object is deleted. It is not recommended that the interceptor modify the {@code state}.
|
* Called before an object is removed by a stateful session.
|
||||||
|
* <p>
|
||||||
|
* It is not recommended that the interceptor modify the {@code state}.
|
||||||
*
|
*
|
||||||
* @param entity The entity instance being deleted
|
* @param entity The entity instance being deleted
|
||||||
* @param id The identifier of the entity
|
* @param id The identifier of the entity
|
||||||
@ -223,7 +235,9 @@ default void onDelete(Object entity, Serializable id, Object[] state, String[] p
|
|||||||
throws CallbackException {}
|
throws CallbackException {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called before an object is deleted. It is not recommended that the interceptor modify the {@code state}.
|
* Called before an object is removed by a stateful session.
|
||||||
|
* <p>
|
||||||
|
* It is not recommended that the interceptor modify the {@code state}.
|
||||||
*
|
*
|
||||||
* @param entity The entity instance being deleted
|
* @param entity The entity instance being deleted
|
||||||
* @param id The identifier of the entity
|
* @param id The identifier of the entity
|
||||||
@ -232,6 +246,9 @@ default void onDelete(Object entity, Serializable id, Object[] state, String[] p
|
|||||||
* @param types The types of the entity properties
|
* @param types The types of the entity properties
|
||||||
*
|
*
|
||||||
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see Session#remove(Object)
|
||||||
|
* @see Session#delete(Object)
|
||||||
*/
|
*/
|
||||||
default void onDelete(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)
|
default void onDelete(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types)
|
||||||
throws CallbackException {
|
throws CallbackException {
|
||||||
@ -515,4 +532,63 @@ default void beforeTransactionCompletion(Transaction tx) {}
|
|||||||
* @param tx The Hibernate transaction facade object
|
* @param tx The Hibernate transaction facade object
|
||||||
*/
|
*/
|
||||||
default void afterTransactionCompletion(Transaction tx) {}
|
default void afterTransactionCompletion(Transaction tx) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before a record is inserted by a {@link StatelessSession}.
|
||||||
|
*
|
||||||
|
* @param entity The entity instance being deleted
|
||||||
|
* @param id The identifier of the entity
|
||||||
|
* @param state The entity state
|
||||||
|
* @param propertyNames The names of the entity properties.
|
||||||
|
* @param propertyTypes The types of the entity properties
|
||||||
|
*
|
||||||
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see StatelessSession#insert(Object)
|
||||||
|
*/
|
||||||
|
default void onInsert(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before a record is updated by a {@link StatelessSession}.
|
||||||
|
*
|
||||||
|
* @param entity The entity instance being deleted
|
||||||
|
* @param id The identifier of the entity
|
||||||
|
* @param state The entity state
|
||||||
|
* @param propertyNames The names of the entity properties.
|
||||||
|
* @param propertyTypes The types of the entity properties
|
||||||
|
*
|
||||||
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see StatelessSession#update(Object)
|
||||||
|
*/
|
||||||
|
default void onUpdate(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before a record is upserted by a {@link StatelessSession}.
|
||||||
|
*
|
||||||
|
* @param entity The entity instance being deleted
|
||||||
|
* @param id The identifier of the entity
|
||||||
|
* @param state The entity state
|
||||||
|
* @param propertyNames The names of the entity properties.
|
||||||
|
* @param propertyTypes The types of the entity properties
|
||||||
|
*
|
||||||
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see StatelessSession#upsert(String, Object)
|
||||||
|
*/
|
||||||
|
default void onUpsert(Object entity, Object id, Object[] state, String[] propertyNames, Type[] propertyTypes) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before a record is deleted by a {@link StatelessSession}.
|
||||||
|
*
|
||||||
|
* @param entity The entity instance being deleted
|
||||||
|
* @param id The identifier of the entity
|
||||||
|
* @param propertyNames The names of the entity properties.
|
||||||
|
* @param propertyTypes The types of the entity properties
|
||||||
|
*
|
||||||
|
* @throws CallbackException Thrown if the interceptor encounters any problems handling the callback.
|
||||||
|
*
|
||||||
|
* @see StatelessSession#delete(Object)
|
||||||
|
*/
|
||||||
|
default void onDelete(Object entity, Object id, String[] propertyNames, Type[] propertyTypes) {}
|
||||||
}
|
}
|
||||||
|
@ -30,13 +30,10 @@
|
|||||||
* checking.
|
* checking.
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* Furthermore, operations performed via a stateless session:
|
* Furthermore, the basic operations of a stateless session do not have
|
||||||
* <ul>
|
* corresponding {@linkplain jakarta.persistence.CascadeType cascade types},
|
||||||
* <li>never cascade to associated instances, no matter what the
|
* and so an operation performed via a stateless session never cascades to
|
||||||
* {@link jakarta.persistence.CascadeType}, and
|
* associated instances.
|
||||||
* <li>bypass Hibernate's {@linkplain org.hibernate.event.spi event model},
|
|
||||||
* lifecycle callbacks, and {@linkplain Interceptor interceptors}.
|
|
||||||
* </ul>
|
|
||||||
* <p>
|
* <p>
|
||||||
* Stateless sessions are vulnerable to data aliasing effects, due to the
|
* Stateless sessions are vulnerable to data aliasing effects, due to the
|
||||||
* lack of a first-level cache.
|
* lack of a first-level cache.
|
||||||
|
@ -133,12 +133,16 @@ public Object insert(String entityName, Object entity) {
|
|||||||
if ( firePreInsert(entity, id, state, persister) ) {
|
if ( firePreInsert(entity, id, state, persister) ) {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
getInterceptor()
|
||||||
|
.onInsert( entity, id, state, persister.getPropertyNames(), persister.getPropertyTypes() );
|
||||||
persister.getInsertCoordinator().insert( entity, id, state, this );
|
persister.getInsertCoordinator().insert( entity, id, state, this );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ( firePreInsert(entity, null, state, persister) ) {
|
if ( firePreInsert(entity, null, state, persister) ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
getInterceptor()
|
||||||
|
.onInsert( entity, null, state, persister.getPropertyNames(), persister.getPropertyTypes() );
|
||||||
final GeneratedValues generatedValues = persister.getInsertCoordinator().insert( entity, state, this );
|
final GeneratedValues generatedValues = persister.getInsertCoordinator().insert( entity, state, this );
|
||||||
id = castNonNull( generatedValues ).getGeneratedValue( persister.getIdentifierMapping() );
|
id = castNonNull( generatedValues ).getGeneratedValue( persister.getIdentifierMapping() );
|
||||||
}
|
}
|
||||||
@ -164,6 +168,8 @@ public void delete(String entityName, Object entity) {
|
|||||||
final Object id = persister.getIdentifier( entity, this );
|
final Object id = persister.getIdentifier( entity, this );
|
||||||
final Object version = persister.getVersion( entity );
|
final Object version = persister.getVersion( entity );
|
||||||
if ( !firePreDelete(entity, id, persister) ) {
|
if ( !firePreDelete(entity, id, persister) ) {
|
||||||
|
getInterceptor()
|
||||||
|
.onDelete( entity, id, persister.getPropertyNames(), persister.getPropertyTypes() );
|
||||||
forEachOwnedCollection( entity, id, persister,
|
forEachOwnedCollection( entity, id, persister,
|
||||||
(descriptor, collection) -> descriptor.remove(id, this) );
|
(descriptor, collection) -> descriptor.remove(id, this) );
|
||||||
persister.getDeleteCoordinator().delete( entity, id, version, this );
|
persister.getDeleteCoordinator().delete( entity, id, version, this );
|
||||||
@ -203,6 +209,8 @@ public void update(String entityName, Object entity) {
|
|||||||
oldVersion = null;
|
oldVersion = null;
|
||||||
}
|
}
|
||||||
if ( !firePreUpdate(entity, id, state, persister) ) {
|
if ( !firePreUpdate(entity, id, state, persister) ) {
|
||||||
|
getInterceptor()
|
||||||
|
.onUpdate( entity, id, state, persister.getPropertyNames(), persister.getPropertyTypes() );
|
||||||
persister.getUpdateCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
|
persister.getUpdateCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
|
||||||
// TODO: can we do better here?
|
// TODO: can we do better here?
|
||||||
forEachOwnedCollection( entity, id, persister,
|
forEachOwnedCollection( entity, id, persister,
|
||||||
@ -220,6 +228,8 @@ public void upsert(String entityName, Object entity) {
|
|||||||
final Object id = idToUpsert( entity, persister );
|
final Object id = idToUpsert( entity, persister );
|
||||||
final Object[] state = persister.getValues( entity );
|
final Object[] state = persister.getValues( entity );
|
||||||
if ( !firePreUpsert(entity, id, state, persister) ) {
|
if ( !firePreUpsert(entity, id, state, persister) ) {
|
||||||
|
getInterceptor()
|
||||||
|
.onUpsert( entity, id, state, persister.getPropertyNames(), persister.getPropertyTypes() );
|
||||||
final Object oldVersion = versionToUpsert( entity, persister, state );
|
final Object oldVersion = versionToUpsert( entity, persister, state );
|
||||||
persister.getMergeCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
|
persister.getMergeCoordinator().update( entity, id, null, state, oldVersion, null, null, false, this );
|
||||||
// TODO: need PreUpsert and PostUpsert events!
|
// TODO: need PreUpsert and PostUpsert events!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user