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