|
15 | 15 | * since, by default, Hibernate must check each of the entity's attribute values one by one. Sometimes, an
|
16 | 16 | * application already has knowledge of an entity's dirtiness and making use of that information would save some
|
17 | 17 | * work. This contract allows the application to take over the task of determining if an entity is dirty.
|
| 18 | + * <p> |
| 19 | + * For example, the application program might define an interface implemented by entities which keep track of |
| 20 | + * their own modified fields: |
| 21 | + * <pre> |
| 22 | + * public interface DirtyTracker { |
| 23 | + * Set<String> changes(); |
| 24 | + * } |
| 25 | + * </pre> |
| 26 | + * Then the following implementation of {@code CustomEntityDirtinessStrategy} would be used: |
| 27 | + * <pre> |
| 28 | + * public class DirtyTrackerDirtinessStrategy implements CustomEntityDirtinessStrategy { |
| 29 | + * @Override |
| 30 | + * public boolean canDirtyCheck(Object entity, EntityPersister persister, Session session) { |
| 31 | + * return entity instanceof DirtyTracker; |
| 32 | + * } |
| 33 | + * |
| 34 | + * @Override |
| 35 | + * public boolean isDirty(Object entity, EntityPersister persister, Session session) { |
| 36 | + * return !((DirtyTracker) entity).changes().isEmpty(); |
| 37 | + * } |
| 38 | + * |
| 39 | + * @Override |
| 40 | + * public void resetDirty(Object entity, EntityPersister persister, Session session) { |
| 41 | + * ((DirtyTracker) entity).changes().clear(); |
| 42 | + * } |
| 43 | + * |
| 44 | + * @Override |
| 45 | + * public void findDirty(Object entity, EntityPersister persister, Session session, DirtyCheckContext dirtyCheckContext) { |
| 46 | + * dirtyCheckContext.doDirtyChecking( attributeInformation -> |
| 47 | + * ((DirtyTracker) entity).changes().contains( attributeInformation.getName() ) ); |
| 48 | + * } |
| 49 | + * } |
| 50 | + * </pre> |
| 51 | + * |
18 | 52 | *
|
19 | 53 | * @see org.hibernate.cfg.AvailableSettings#CUSTOM_ENTITY_DIRTINESS_STRATEGY
|
20 | 54 | * @see org.hibernate.cfg.Configuration#setCustomEntityDirtinessStrategy(CustomEntityDirtinessStrategy)
|
|
0 commit comments