use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class DefaultRefreshEventListener method evictCachedCollections.
private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException {
for (Type type : types) {
if (type.isCollectionType()) {
CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister(((CollectionType) type).getRole());
if (collectionPersister.hasCache()) {
final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(id, collectionPersister, source.getFactory(), source.getTenantIdentifier());
final SoftLock lock = cache.lockItem(source, ck, null);
cache.remove(source, ck);
source.getActionQueue().registerProcess((success, session) -> cache.unlockItem(session, ck, lock));
}
} else if (type.isComponentType()) {
CompositeType actype = (CompositeType) type;
evictCachedCollections(actype.getSubtypes(), id, source);
}
}
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class CollectionUpdateAction method execute.
@Override
public void execute() throws HibernateException {
final Serializable id = getKey();
final SharedSessionContractImplementor session = getSession();
final CollectionPersister persister = getPersister();
final PersistentCollection collection = getCollection();
final boolean affectedByFilters = persister.isAffectedByEnabledFilters(session);
preUpdate();
if (!collection.wasInitialized()) {
if (!collection.hasQueuedOperations()) {
throw new AssertionFailure("no queued adds");
}
// do nothing - we only need to notify the cache...
} else if (!affectedByFilters && collection.empty()) {
if (!emptySnapshot) {
persister.remove(id, session);
}
} else if (collection.needsRecreate(persister)) {
if (affectedByFilters) {
throw new HibernateException("cannot recreate collection while filter is enabled: " + MessageHelper.collectionInfoString(persister, collection, id, session));
}
if (!emptySnapshot) {
persister.remove(id, session);
}
persister.recreate(collection, id, session);
} else {
persister.deleteRows(collection, id, session);
persister.updateRows(collection, id, session);
persister.insertRows(collection, id, session);
}
getSession().getPersistenceContext().getCollectionEntry(collection).afterAction(collection);
evict();
postUpdate();
if (getSession().getFactory().getStatistics().isStatisticsEnabled()) {
getSession().getFactory().getStatistics().updateCollection(getPersister().getRole());
}
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class EnabledCaching method evictCollectionData.
@Override
public void evictCollectionData(String role, Serializable ownerIdentifier) {
final CollectionPersister collectionDescriptor = sessionFactory.getMetamodel().collectionPersister(role);
final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccessStrategy();
if (cacheAccess == null) {
return;
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Evicting second-level cache: %s", MessageHelper.collectionInfoString(collectionDescriptor, ownerIdentifier, sessionFactory));
}
final Object key = cacheAccess.generateCacheKey(ownerIdentifier, collectionDescriptor, sessionFactory, null);
cacheAccess.evict(key);
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class EnabledCaching method containsCollection.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Collection data
@Override
public boolean containsCollection(String role, Serializable ownerIdentifier) {
final CollectionPersister collectionDescriptor = sessionFactory.getMetamodel().collectionPersister(role);
final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccessStrategy();
if (cacheAccess == null) {
return false;
}
final Object key = cacheAccess.generateCacheKey(ownerIdentifier, collectionDescriptor, sessionFactory, null);
return cacheAccess.contains(key);
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method getIndexInOwner.
@Override
public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap) {
final EntityPersister persister = session.getFactory().getMetamodel().entityPersister(entity);
final CollectionPersister cp = session.getFactory().getMetamodel().collectionPersister(entity + '.' + property);
// try cache lookup first
final Object parent = parentsByChild.get(childEntity);
if (parent != null) {
final EntityEntry entityEntry = entityEntryContext.getEntityEntry(parent);
// there maybe more than one parent, filter by type
if (persister.isSubclassEntityName(entityEntry.getEntityName())) {
Object index = getIndexInParent(property, childEntity, persister, cp, parent);
if (index == null && mergeMap != null) {
final Object unMergedInstance = mergeMap.get(parent);
final Object unMergedChild = mergeMap.get(childEntity);
if (unMergedInstance != null && unMergedChild != null) {
index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
LOG.debugf("A detached object being merged (corresponding to a parent in parentsByChild) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
}
}
if (index != null) {
return index;
}
} else {
// remove wrong entry
parentsByChild.remove(childEntity);
}
}
// Not found in cache, proceed
for (Entry<Object, EntityEntry> me : reentrantSafeEntityEntries()) {
final EntityEntry ee = me.getValue();
if (persister.isSubclassEntityName(ee.getEntityName())) {
final Object instance = me.getKey();
Object index = getIndexInParent(property, childEntity, persister, cp, instance);
if (index == null && mergeMap != null) {
final Object unMergedInstance = mergeMap.get(instance);
final Object unMergedChild = mergeMap.get(childEntity);
if (unMergedInstance != null && unMergedChild != null) {
index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
LOG.debugf("A detached object being merged (corresponding to a managed entity) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
}
}
if (index != null) {
return index;
}
}
}
return null;
}
Aggregations