Package org.hibernate
Interface CustomEntityDirtinessStrategy
- All Known Implementing Classes:
DefaultCustomEntityDirtinessStrategy
public interface CustomEntityDirtinessStrategy
During a flush cycle, Hibernate needs to determine which of the entities associated with a
Session
are dirty, meaning modified. Dirty entities will be UPDATEed in the database.
In some circumstances, the process of determining whether an entity is dirty can carry a significant overhead, since, by default, Hibernate must check each of the entity's attribute values one by one. Sometimes, an application already has knowledge of an entity's dirtiness and making use of that information would save some work. This contract allows the application to take over the task of determining if an entity is dirty.
For example, the application program might define an interface implemented by entities which keep track of their own modified fields:
public interface DirtyTracker {
Set<String> changes();
}
Then the following implementation of CustomEntityDirtinessStrategy would be used:
public class DirtyTrackerDirtinessStrategy implements CustomEntityDirtinessStrategy {
@Override
public boolean canDirtyCheck(Object entity, EntityPersister persister, Session session) {
return entity instanceof DirtyTracker;
}
@Override
public boolean isDirty(Object entity, EntityPersister persister, Session session) {
return !((DirtyTracker) entity).changes().isEmpty();
}
@Override
public void resetDirty(Object entity, EntityPersister persister, Session session) {
((DirtyTracker) entity).changes().clear();
}
@Override
public void findDirty(Object entity, EntityPersister persister, Session session, DirtyCheckContext dirtyCheckContext) {
dirtyCheckContext.doDirtyChecking( attributeInformation ->
((DirtyTracker) entity).changes().contains( attributeInformation.getName() ) );
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceResponsible for identifying when attributes are dirty.static interfaceProvidesCustomEntityDirtinessStrategy.AttributeCheckerwith meta information about the attributes being checked.static interfaceA callback to drive dirty checking. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanDirtyCheck(Object entity, EntityPersister persister, Session session) Is this strategy capable of telling whether the given entity is dirty? A return oftruemeans thatisDirty(java.lang.Object, org.hibernate.persister.entity.EntityPersister, org.hibernate.Session)will be called next as the definitive means to determine whether the entity is dirty.voidfindDirty(Object entity, EntityPersister persister, Session session, CustomEntityDirtinessStrategy.DirtyCheckContext dirtyCheckContext) Callback used to hook into Hibernate algorithm for determination of which attributes have changed.booleanisDirty(Object entity, EntityPersister persister, Session session) The callback used by Hibernate to determine if the given entity is dirty.voidresetDirty(Object entity, EntityPersister persister, Session session) Callback used by Hibernate to signal that the entity dirty flag should be cleared.
-
Method Details
-
canDirtyCheck
Is this strategy capable of telling whether the given entity is dirty? A return oftruemeans thatisDirty(java.lang.Object, org.hibernate.persister.entity.EntityPersister, org.hibernate.Session)will be called next as the definitive means to determine whether the entity is dirty.- Parameters:
entity- The entity to be checkedpersister- The persister corresponding to the given entitysession- The session from which this check originates.- Returns:
trueindicates the dirty check can be done;falseindicates it cannot.
-
isDirty
The callback used by Hibernate to determine if the given entity is dirty. Only called if the previouscanDirtyCheck(java.lang.Object, org.hibernate.persister.entity.EntityPersister, org.hibernate.Session)returnedtrue- Parameters:
entity- The entity to check.persister- The persister corresponding to the given entitysession- The session from which this check originates.- Returns:
trueindicates the entity is dirty;falseindicates the entity is not dirty.
-
resetDirty
Callback used by Hibernate to signal that the entity dirty flag should be cleared. Generally this happens after previous dirty changes were written to the database.- Parameters:
entity- The entity to resetpersister- The persister corresponding to the given entitysession- The session from which this call originates.
-
findDirty
void findDirty(Object entity, EntityPersister persister, Session session, CustomEntityDirtinessStrategy.DirtyCheckContext dirtyCheckContext) Callback used to hook into Hibernate algorithm for determination of which attributes have changed. Applications wanting to hook in to this would call back into the givenCustomEntityDirtinessStrategy.DirtyCheckContext.doDirtyChecking(org.hibernate.CustomEntityDirtinessStrategy.AttributeChecker)method, passing along an appropriateCustomEntityDirtinessStrategy.AttributeCheckerimplementation.- Parameters:
entity- The entity being checkedpersister- The persister corresponding to the given entitysession- The session from which this call originates.dirtyCheckContext- The callback context
-