use of org.hibernate.cache.spi.CacheImplementor in project quarkus by quarkusio.
the class InfinispanCacheJPAFunctionalityTestEndpoint method getRegionFactory.
private QuarkusInfinispanRegionFactory getRegionFactory() {
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
CacheImplementor cache = (CacheImplementor) sessionFactory.getCache();
return (QuarkusInfinispanRegionFactory) cache.getRegionFactory();
}
use of org.hibernate.cache.spi.CacheImplementor in project hibernate-orm by hibernate.
the class SynchronizedSpaceTests method checkUseCase.
private void checkUseCase(Function<SessionImplementor, Query> queryProducer, Consumer<Query> executor, boolean shouldExistAfter) {
// first, load both `CachedEntity` instances into the L2 cache
loadAll();
final CacheImplementor cacheSystem = sessionFactory().getCache();
// make sure they are there
assertThat(cacheSystem.containsEntity(CachedEntity.class, 1), is(true));
assertThat(cacheSystem.containsEntity(CachedEntity.class, 2), is(true));
// create a query to update the specified table - allowing the passed consumer to register a space if needed
inTransaction(session -> {
// notice the type is the JPA Query interface
final Query nativeQuery = queryProducer.apply(session);
executor.accept(nativeQuery);
});
// see if the entries exist based on the expectation
assertThat(cacheSystem.containsEntity(CachedEntity.class, 1), is(shouldExistAfter));
assertThat(cacheSystem.containsEntity(CachedEntity.class, 2), is(shouldExistAfter));
}
use of org.hibernate.cache.spi.CacheImplementor in project hibernate-orm by hibernate.
the class SharedDomainDataAndQueryResultsTest method testCacheImplementorGetRegion.
@Test
@TestForIssue(jiraKey = "HHH-13586")
public void testCacheImplementorGetRegion() {
rebuildSessionFactory();
final CacheImplementor cache = sessionFactory().getCache();
final Region domainDataRegion = cache.getRegion(REGION);
assertTrue(DomainDataRegion.class.isInstance(domainDataRegion));
assertEquals(REGION, domainDataRegion.getName());
// There should not be a QueryResultsRegion named REGION until
// the named query is executed.
assertNull(cache.getQueryResultsCacheStrictly(REGION));
doInHibernate(this::sessionFactory, session -> {
session.createNamedQuery("Dog.findAll", Dog.class).list();
});
// No there should be a QueryResultsCache named REGION
final QueryResultsCache queryResultsCache = cache.getQueryResultsCacheStrictly(REGION);
assertNotNull(queryResultsCache);
assertEquals(REGION, queryResultsCache.getRegion().getName());
// Now there is a DomainDataRegion and QueryResultsRegion named REGION.
// Make sure that the same DomainDataRegion is returned by cache.getRegion( REGION ).
assertSame(domainDataRegion, cache.getRegion(REGION));
}
use of org.hibernate.cache.spi.CacheImplementor in project hibernate-orm by hibernate.
the class PolymorphicCacheTest method testPolymorphismAndCache.
@Test
public void testPolymorphismAndCache(SessionFactoryScope scope) {
final CacheImplementor cache = scope.getSessionFactory().getCache();
assertThat(cache.containsEntity(Cacheable.class, 1)).isTrue();
assertThat(cache.containsEntity(Cacheable.class, 2)).isTrue();
assertThat(cache.containsEntity(CachedItem1.class, 1)).isTrue();
assertThat(cache.containsEntity(CachedItem2.class, 2)).isTrue();
// would be nice-to-have
// assertThat( cache.containsEntity( CachedItem2.class, 1 ) ).isFalse();
// test accessing the wrong class by id with a cache-hit
scope.inTransaction((session) -> {
try {
final CachedItem2 loaded = session.get(CachedItem2.class, 1);
assertThat(loaded).isNull();
} catch (WrongClassException legacyBehavior) {
// the legacy behavior for loading an entity from a
// cache hit when it is the wrong class was to throw
// a WrongClassException.
//
// this was inconsistent with cases where there is no
// cache hit - there we return null.
//
// 6.0 makes the behavior here consistent by always
// returning null in such cases
//
// make sure WrongClassException is not thrown here
Assertions.fail("WrongClassException was thrown for but returning null was expected");
}
});
// test accessing the wrong class by id with no cache-hit
cache.evictEntityData(Cacheable.class, 1);
cache.evictEntityData(Cacheable.class, 2);
scope.inTransaction((session) -> {
// the legacy behavior for no cache hit was to return null
try {
final CachedItem2 loaded = session.get(CachedItem2.class, 1);
assertThat(loaded).isNull();
} catch (WrongClassException legacyBehavior) {
// as in the cache hit assertions above, make sure
// WrongClassException is not thrown here
Assertions.fail("WrongClassException was thrown for but returning null was expected");
}
});
// reload into cache
scope.inTransaction((session) -> {
session.get(CachedItem1.class, 1);
session.get(CachedItem2.class, 2);
});
assertThat(cache.containsEntity(CachedItem1.class, 1)).isTrue();
assertThat(cache.containsEntity(CachedItem2.class, 2)).isTrue();
final CachedItem1 detachedItem1 = scope.fromTransaction((session) -> session.get(CachedItem1.class, 1));
// test updating
scope.inSession((session) -> {
scope.inTransaction(session, (s) -> {
detachedItem1.setName("updated");
session.merge(detachedItem1);
});
assertThat(cache.containsEntity(CachedItem1.class, 1)).isTrue();
assertThat(cache.containsEntity(CachedItem2.class, 2)).isTrue();
scope.inTransaction(session, (s) -> {
final CachedItem1 loadedItem1 = s.get(CachedItem1.class, 1);
assertThat(loadedItem1).isNotNull();
assertThat(loadedItem1.getName()).isEqualTo("updated");
final CachedItem2 loadedItem2 = s.get(CachedItem2.class, 2);
assertThat(loadedItem2).isNotNull();
});
});
// test deleting
scope.inSession((session) -> {
scope.inTransaction(session, (s) -> {
session.remove(session.getReference(CachedItem1.class, 1));
});
scope.inTransaction(session, (s) -> {
final CachedItem1 cachedItem1 = session.get(CachedItem1.class, 1);
assertThat(cachedItem1).isNull();
final CachedItem2 cachedItem2 = session.get(CachedItem2.class, 2);
assertThat(cachedItem2).isNotNull();
});
});
}
use of org.hibernate.cache.spi.CacheImplementor in project hibernate-orm by hibernate.
the class CachedMutableNaturalIdTest method dropTestData.
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction((session) -> {
session.createQuery("delete from Another").executeUpdate();
session.createQuery("delete from AllCached").executeUpdate();
session.createQuery("delete from SubClass").executeUpdate();
session.createQuery("delete from A").executeUpdate();
});
final CacheImplementor cache = scope.getSessionFactory().getCache();
cache.evictAllRegions();
}
Aggregations