Search in sources :

Example 6 with CacheImplementor

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

the class CompleteComponentPropertyRefTest method testComponentPropertyRef.

@Test
public void testComponentPropertyRef(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        Person p = new Person();
        p.setIdentity(new Identity());
        Account a = new Account();
        a.setNumber("123-12345-1236");
        a.setOwner(p);
        p.getIdentity().setName("Gavin");
        p.getIdentity().setSsn("123-12-1234");
        session.persist(p);
        session.persist(a);
    });
    scope.inTransaction(session -> {
        Account a = (Account) session.createQuery("from Account a left join fetch a.owner").uniqueResult();
        assertTrue(Hibernate.isInitialized(a.getOwner()));
        assertNotNull(a.getOwner());
        assertEquals("Gavin", a.getOwner().getIdentity().getName());
        session.clear();
        a = session.get(Account.class, "123-12345-1236");
        assertFalse(Hibernate.isInitialized(a.getOwner()));
        assertNotNull(a.getOwner());
        assertEquals("Gavin", a.getOwner().getIdentity().getName());
        assertTrue(Hibernate.isInitialized(a.getOwner()));
        session.clear();
        final CacheImplementor cache = scope.getSessionFactory().getCache();
        cache.evictEntityData(Account.class);
        cache.evictEntityData(Person.class);
        a = session.get(Account.class, "123-12345-1236");
        assertTrue(Hibernate.isInitialized(a.getOwner()));
        assertNotNull(a.getOwner());
        assertEquals("Gavin", a.getOwner().getIdentity().getName());
        assertTrue(Hibernate.isInitialized(a.getOwner()));
        session.delete(a);
        session.delete(a.getOwner());
    });
}
Also used : CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.jupiter.api.Test)

Example 7 with CacheImplementor

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

the class UnconstrainedTest method testUnconstrainedNoCache.

@Test
public void testUnconstrainedNoCache(SessionFactoryScope scope) {
    final CacheImplementor cache = scope.getSessionFactory().getCache();
    cache.evictEntityData(Person.class);
    scope.inTransaction(session -> {
        Person p = session.get(Person.class, "gavin");
        assertNull(p.getEmployee());
        p.setEmployee(new Employee("123456"));
    });
    cache.evictEntityData(Person.class);
    scope.inTransaction(session -> {
        Person p = session.get(Person.class, "gavin");
        assertTrue(Hibernate.isInitialized(p.getEmployee()));
        assertNotNull(p.getEmployee());
        session.delete(p);
    });
}
Also used : CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.jupiter.api.Test)

Example 8 with CacheImplementor

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

the class UnconstrainedTest method testUnconstrainedOuterJoinFetch.

@Test
public void testUnconstrainedOuterJoinFetch(SessionFactoryScope scope) {
    final CacheImplementor cache = scope.getSessionFactory().getCache();
    cache.evictEntityData(Person.class);
    scope.inTransaction(session -> {
        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = criteriaBuilder.createQuery(Person.class);
        Root<Person> root = criteria.from(Person.class);
        root.fetch("employee", JoinType.LEFT);
        criteria.where(criteriaBuilder.equal(root.get("name"), "gavin"));
        Person p = session.createQuery(criteria).uniqueResult();
        // Person p = session.createCriteria( Person.class )
        // .setFetchMode( "employee", FetchMode.JOIN )
        // .add( Restrictions.idEq( "gavin" ) )
        // .uniqueResult();
        assertNull(p.getEmployee());
        p.setEmployee(new Employee("123456"));
    });
    cache.evictEntityData(Person.class);
    scope.inTransaction(session -> {
        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = criteriaBuilder.createQuery(Person.class);
        Root<Person> root = criteria.from(Person.class);
        root.fetch("employee", JoinType.LEFT);
        criteria.where(criteriaBuilder.equal(root.get("name"), "gavin"));
        Person p = session.createQuery(criteria).uniqueResult();
        // Person p = session.createCriteria( Person.class )
        // .setFetchMode( "employee", FetchMode.JOIN )
        // .add( Restrictions.idEq( "gavin" ) )
        // .uniqueResult();
        assertTrue(Hibernate.isInitialized(p.getEmployee()));
        assertNotNull(p.getEmployee());
        session.delete(p);
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.jupiter.api.Test)

Example 9 with CacheImplementor

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

the class InheritedNaturalIdCacheTest method clearCaches.

private void clearCaches(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final CacheImplementor cache = sessionFactory.getCache();
    cache.evictEntityData(MyEntity.class);
    cache.evictEntityData(ExtendedEntity.class);
    cache.evictNaturalIdData(MyEntity.class);
    cache.evictNaturalIdData(ExtendedEntity.class);
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CacheImplementor(org.hibernate.cache.spi.CacheImplementor)

Example 10 with CacheImplementor

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

the class InheritedNaturalIdCacheTest method testLoadWrongClassByNaturalIdFromCache.

@Test
public void testLoadWrongClassByNaturalIdFromCache(SessionFactoryScope scope) {
    clearCaches(scope);
    // load `MyEntity#1` into the cache
    scope.inTransaction((session) -> {
        final MyEntity loaded = session.bySimpleNaturalId(MyEntity.class).load("base");
        assertThat(loaded).isNotNull();
    });
    final CacheImplementor cache = scope.getSessionFactory().getCache();
    assertThat(cache.containsEntity(MyEntity.class, 1)).isTrue();
    // now try to access it as an ExtendedEntity
    scope.inTransaction((session) -> {
        final ExtendedEntity loaded = session.bySimpleNaturalId(ExtendedEntity.class).load("base");
        assertThat(loaded).isNull();
    });
}
Also used : CacheImplementor(org.hibernate.cache.spi.CacheImplementor) Test(org.junit.jupiter.api.Test)

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