Search in sources :

Example 1 with CollectionDataAccess

use of org.hibernate.cache.spi.access.CollectionDataAccess in project hibernate-orm by hibernate.

the class CollectionCacheEvictionTest method testCachedValueAfterEviction.

@Test
public void testCachedValueAfterEviction() {
    CollectionPersister persister = sessionFactory().getCollectionPersister(Company.class.getName() + ".users");
    Session session = openSession();
    SessionImplementor sessionImplementor = (SessionImplementor) session;
    CollectionDataAccess cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(sessionImplementor, key);
    assertNull(cachedValue);
    Company company = session.get(Company.class, 1);
    // should add in cache
    assertEquals(1, company.getUsers().size());
    session.close();
    session = openSession();
    sessionImplementor = (SessionImplementor) session;
    key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    cachedValue = cache.get(sessionImplementor, key);
    assertNotNull("Collection wasn't cached", cachedValue);
    session.close();
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with CollectionDataAccess

use of org.hibernate.cache.spi.access.CollectionDataAccess in project hibernate-orm by hibernate.

the class CollectionAction method evict.

protected final void evict() throws CacheException {
    if (persister.hasCache()) {
        final CollectionDataAccess cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(key, persister, session.getFactory(), session.getTenantIdentifier());
        cache.remove(session, ck);
    }
}
Also used : CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess)

Example 3 with CollectionDataAccess

use of org.hibernate.cache.spi.access.CollectionDataAccess in project hibernate-orm by hibernate.

the class EnabledCaching method prime.

@Override
public void prime(Set<DomainDataRegionConfig> cacheRegionConfigs) {
    for (DomainDataRegionConfig regionConfig : cacheRegionConfigs) {
        final DomainDataRegion region = getRegionFactory().buildDomainDataRegion(regionConfig, this);
        regionsByName.put(region.getName(), region);
        if (!StringTypeDescriptor.INSTANCE.areEqual(region.getName(), regionConfig.getRegionName())) {
            throw new HibernateException(String.format(Locale.ROOT, "Region [%s] returned from RegionFactory [%s] was named differently than requested name.  Expecting `%s`, but found `%s`", region, getRegionFactory().getClass().getName(), regionConfig.getRegionName(), region.getName()));
        }
        for (EntityDataCachingConfig entityAccessConfig : regionConfig.getEntityCaching()) {
            final EntityDataAccess entityDataAccess = entityAccessMap.put(entityAccessConfig.getNavigableRole(), region.getEntityDataAccess(entityAccessConfig.getNavigableRole()));
            legacySecondLevelCacheNames.add(StringHelper.qualifyConditionally(getSessionFactory().getSessionFactoryOptions().getCacheRegionPrefix(), region.getName()));
        }
        if (regionConfig.getNaturalIdCaching().isEmpty()) {
            legacyNaturalIdAccessesForRegion.put(region.getName(), Collections.emptySet());
        } else {
            final HashSet<NaturalIdDataAccess> accesses = new HashSet<>();
            for (NaturalIdDataCachingConfig naturalIdAccessConfig : regionConfig.getNaturalIdCaching()) {
                final NaturalIdDataAccess naturalIdDataAccess = naturalIdAccessMap.put(naturalIdAccessConfig.getNavigableRole(), region.getNaturalIdDataAccess(naturalIdAccessConfig.getNavigableRole()));
                accesses.add(naturalIdDataAccess);
            }
            legacyNaturalIdAccessesForRegion.put(region.getName(), accesses);
        }
        for (CollectionDataCachingConfig collectionAccessConfig : regionConfig.getCollectionCaching()) {
            final CollectionDataAccess collectionDataAccess = collectionAccessMap.put(collectionAccessConfig.getNavigableRole(), region.getCollectionDataAccess(collectionAccessConfig.getNavigableRole()));
            legacySecondLevelCacheNames.add(StringHelper.qualifyConditionally(getSessionFactory().getSessionFactoryOptions().getCacheRegionPrefix(), region.getName()));
        }
    }
}
Also used : NaturalIdDataCachingConfig(org.hibernate.cache.cfg.spi.NaturalIdDataCachingConfig) EntityDataCachingConfig(org.hibernate.cache.cfg.spi.EntityDataCachingConfig) HibernateException(org.hibernate.HibernateException) DomainDataRegionConfig(org.hibernate.cache.cfg.spi.DomainDataRegionConfig) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) CollectionDataCachingConfig(org.hibernate.cache.cfg.spi.CollectionDataCachingConfig) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with CollectionDataAccess

use of org.hibernate.cache.spi.access.CollectionDataAccess in project hibernate-orm by hibernate.

the class BatchFetchQueue method isCached.

private boolean isCached(Serializable collectionKey, CollectionPersister persister) {
    SharedSessionContractImplementor session = context.getSession();
    if (session.getCacheMode().isGetEnabled() && persister.hasCache()) {
        CollectionDataAccess cache = persister.getCacheAccessStrategy();
        Object cacheKey = cache.generateCacheKey(collectionKey, persister, session.getFactory(), session.getTenantIdentifier());
        return CacheHelper.fromSharedCache(session, cacheKey, cache) != null;
    }
    return false;
}
Also used : CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess)

Example 5 with CollectionDataAccess

use of org.hibernate.cache.spi.access.CollectionDataAccess in project hibernate-orm by hibernate.

the class DefaultInitializeCollectionEventListener method initializeCollectionFromCache.

/**
 * Try to initialize a collection from the cache
 *
 * @param id The id of the collection of initialize
 * @param persister The collection persister
 * @param collection The collection to initialize
 * @param source The originating session
 *
 * @return true if we were able to initialize the collection from the cache;
 *         false otherwise.
 */
private boolean initializeCollectionFromCache(Serializable id, CollectionPersister persister, PersistentCollection collection, SessionImplementor source) {
    if (!source.getLoadQueryInfluencers().getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters(source)) {
        LOG.trace("Disregarding cached version (if any) of collection due to enabled filters");
        return false;
    }
    final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled();
    if (!useCache) {
        return false;
    }
    final SessionFactoryImplementor factory = source.getFactory();
    final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
    final Object ck = cacheAccessStrategy.generateCacheKey(id, persister, factory, source.getTenantIdentifier());
    final Object ce = CacheHelper.fromSharedCache(source, ck, persister.getCacheAccessStrategy());
    if (factory.getStatistics().isStatisticsEnabled()) {
        if (ce == null) {
            factory.getStatistics().collectionCacheMiss(persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName());
        } else {
            factory.getStatistics().collectionCacheHit(persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName());
        }
    }
    if (ce == null) {
        return false;
    }
    CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure().destructure(ce, factory);
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    cacheEntry.assemble(collection, persister, persistenceContext.getCollectionOwner(id, persister));
    persistenceContext.getCollectionEntry(collection).postInitialize(collection);
    // addInitializedCollection(collection, persister, id);
    return true;
}
Also used : CollectionCacheEntry(org.hibernate.cache.spi.entry.CollectionCacheEntry) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess)

Aggregations

CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)13 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 HibernateException (org.hibernate.HibernateException)3 Session (org.hibernate.Session)3 CollectionCacheEntry (org.hibernate.cache.spi.entry.CollectionCacheEntry)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)2 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)2 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Type (org.hibernate.type.Type)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1