Search in sources :

Example 1 with CacheImplementor

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();
}
Also used : SessionFactory(org.hibernate.SessionFactory) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) QuarkusInfinispanRegionFactory(org.infinispan.quarkus.hibernate.cache.QuarkusInfinispanRegionFactory)

Example 2 with CacheImplementor

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));
}
Also used : NamedNativeQuery(org.hibernate.annotations.NamedNativeQuery) Query(jakarta.persistence.Query) CacheImplementor(org.hibernate.cache.spi.CacheImplementor)

Example 3 with CacheImplementor

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));
}
Also used : DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Region(org.hibernate.cache.spi.Region) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) QueryResultsCache(org.hibernate.cache.spi.QueryResultsCache) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with CacheImplementor

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();
        });
    });
}
Also used : WrongClassException(org.hibernate.WrongClassException) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.jupiter.api.Test)

Example 5 with CacheImplementor

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();
}
Also used : CacheImplementor(org.hibernate.cache.spi.CacheImplementor) AfterEach(org.junit.jupiter.api.AfterEach)

Aggregations

CacheImplementor (org.hibernate.cache.spi.CacheImplementor)13 Test (org.junit.jupiter.api.Test)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)1 Query (jakarta.persistence.Query)1 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 Transactional (javax.transaction.Transactional)1 SessionFactory (org.hibernate.SessionFactory)1 WrongClassException (org.hibernate.WrongClassException)1 NamedNativeQuery (org.hibernate.annotations.NamedNativeQuery)1 DomainDataRegion (org.hibernate.cache.spi.DomainDataRegion)1 QueryResultsCache (org.hibernate.cache.spi.QueryResultsCache)1 Region (org.hibernate.cache.spi.Region)1 TimestampsCache (org.hibernate.cache.spi.TimestampsCache)1 StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)1 TestForIssue (org.hibernate.testing.TestForIssue)1 QuarkusInfinispanRegionFactory (org.infinispan.quarkus.hibernate.cache.QuarkusInfinispanRegionFactory)1 Test (org.junit.Test)1 AfterEach (org.junit.jupiter.api.AfterEach)1