private void evictCache( Object entity, EntityPersister persister, EventSource session, Object[] oldState) { try { SessionFactoryImplementor factory = persister.getFactory(); Set<String> collectionRoles = factory.getCollectionRolesByEntityParticipant(persister.getEntityName()); if (collectionRoles == null || collectionRoles.isEmpty()) { return; } for (String role : collectionRoles) { CollectionPersister collectionPersister = factory.getCollectionPersister(role); if (!collectionPersister.hasCache()) { // ignore collection if no caching is used continue; } // this is the property this OneToMany relation is mapped by String mappedBy = collectionPersister.getMappedByProperty(); if (mappedBy != null) { int i = persister.getEntityMetamodel().getPropertyIndex(mappedBy); Serializable oldId = null; if (oldState != null) { // in case of updating an entity we perhaps have to decache 2 entity collections, this // is the // old one oldId = session.getIdentifier(oldState[i]); } Object ref = persister.getPropertyValue(entity, i); Serializable id = null; if (ref != null) { id = session.getIdentifier(ref); } // only evict if the related entity has changed if (id != null && !id.equals(oldId)) { evict(id, collectionPersister, session); if (oldId != null) { evict(oldId, collectionPersister, session); } } } else { LOG.debug("Evict CollectionRegion " + role); collectionPersister.getCacheAccessStrategy().evictAll(); } } } catch (Exception e) { // don't let decaching influence other logic LOG.error("", e); } }
@Override public Set<String> getCollectionRolesByEntityParticipant(String entityName) { return delegate.getCollectionRolesByEntityParticipant(entityName); }