Search in sources :

Example 46 with CollectionPersister

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);
        }
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) SoftLock(org.hibernate.cache.spi.access.SoftLock) CompositeType(org.hibernate.type.CompositeType)

Example 47 with CollectionPersister

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());
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 48 with CollectionPersister

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);
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess)

Example 49 with CollectionPersister

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);
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess)

Example 50 with CollectionPersister

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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Aggregations

CollectionPersister (org.hibernate.persister.collection.CollectionPersister)55 HibernateException (org.hibernate.HibernateException)11 Type (org.hibernate.type.Type)10 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)9 CollectionType (org.hibernate.type.CollectionType)9 Serializable (java.io.Serializable)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)8 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 Session (org.hibernate.Session)6 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Test (org.junit.Test)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 CompositeType (org.hibernate.type.CompositeType)5 EntityType (org.hibernate.type.EntityType)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4