Search in sources :

Example 16 with CollectionRegionAccessStrategy

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

the class CacheImpl method determineCollectionRegionAccessStrategy.

@Override
public CollectionRegionAccessStrategy determineCollectionRegionAccessStrategy(Collection model) {
    final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
    CollectionRegionAccessStrategy accessStrategy = collectionRegionAccessStrategyMap.get(cacheRegionName);
    if (accessStrategy == null && settings.isSecondLevelCacheEnabled()) {
        final AccessType accessType = AccessType.fromExternalName(model.getCacheConcurrencyStrategy());
        if (accessType != null) {
            LOG.tracev("Building shared cache region for collection data [{0}]", model.getRole());
            CollectionRegion collectionRegion = regionFactory.buildCollectionRegion(cacheRegionName, sessionFactory.getProperties(), CacheDataDescriptionImpl.decode(model));
            accessStrategy = collectionRegion.buildAccessStrategy(accessType);
            collectionRegionAccessStrategyMap.put(cacheRegionName, accessStrategy);
        }
    }
    return accessStrategy;
}
Also used : CollectionRegion(org.hibernate.cache.spi.CollectionRegion) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) AccessType(org.hibernate.cache.spi.access.AccessType)

Example 17 with CollectionRegionAccessStrategy

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

the class DynamicFilterTest method testSecondLevelCachedCollectionsFiltering.

@Test
public void testSecondLevelCachedCollectionsFiltering() {
    TestData testData = new TestData();
    testData.prepare();
    Session session = openSession();
    long ts = ((SessionImplementor) session).getTimestamp();
    // Force a collection into the second level cache, with its non-filtered elements
    Salesperson sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    Hibernate.initialize(sp.getOrders());
    CollectionPersister persister = sessionFactory().getCollectionPersister(Salesperson.class.getName() + ".orders");
    assertTrue("No cache for collection", persister.hasCache());
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object cacheKey = cache.generateCacheKey(testData.steveId, persister, sessionFactory(), session.getTenantIdentifier());
    CollectionCacheEntry cachedData = (CollectionCacheEntry) cache.get((SessionImplementor) session, cacheKey, ts);
    assertNotNull("collection was not in cache", cachedData);
    session.close();
    session = openSession();
    ts = ((SessionImplementor) session).getTimestamp();
    session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
    sp = (Salesperson) session.createQuery("from Salesperson as s where s.id = :id").setLong("id", testData.steveId).uniqueResult();
    assertEquals("Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size());
    Object cacheKey2 = cache.generateCacheKey(testData.steveId, persister, sessionFactory(), session.getTenantIdentifier());
    CollectionCacheEntry cachedData2 = (CollectionCacheEntry) persister.getCacheAccessStrategy().get((SessionImplementor) session, cacheKey2, ts);
    assertNotNull("collection no longer in cache!", cachedData2);
    assertSame("Different cache values!", cachedData, cachedData2);
    session.close();
    session = openSession();
    session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
    sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    assertEquals("Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size());
    session.close();
    // Finally, make sure that the original cached version did not get over-written
    session = openSession();
    sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    assertEquals("Actual cached version got over-written", 2, sp.getOrders().size());
    session.close();
    testData.release();
}
Also used : CollectionCacheEntry(org.hibernate.cache.spi.entry.CollectionCacheEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 18 with CollectionRegionAccessStrategy

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

the class CacheLazyLoadNoTransTest method isCached.

private boolean isCached(Serializable id, Class<?> entityClass, String attr) {
    Session session = openSession();
    CollectionPersister persister = sessionFactory().getCollectionPersister(entityClass.getName() + "." + attr);
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(id, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(((SessionImplementor) session), key, ((SessionImplementor) session).getTimestamp());
    session.close();
    return cachedValue != null;
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) Session(org.hibernate.Session)

Aggregations

CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)18 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5 Session (org.hibernate.Session)3 CollectionCacheEntry (org.hibernate.cache.spi.entry.CollectionCacheEntry)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 HibernateException (org.hibernate.HibernateException)2 CollectionRegion (org.hibernate.cache.spi.CollectionRegion)2 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Type (org.hibernate.type.Type)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1 EntityType (javax.persistence.metamodel.EntityType)1