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;
}
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();
}
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;
}
Aggregations