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