Search in sources :

Example 1 with BaseRegion

use of org.hibernate.testing.cache.BaseRegion in project hibernate-orm by hibernate.

the class InitFromCacheTestTask method execute.

@Override
public void execute() {
    EntityPersister p = sessionFactory().getEntityPersister(Document.class.getName());
    assertTrue(p.hasCache());
    BaseRegion region = (BaseRegion) p.getCacheAccessStrategy().getRegion();
    Session s = sessionFactory().openSession();
    s.beginTransaction();
    s.persist(new Document("HiA", "Hibernate book", "Hibernate is...."));
    s.getTransaction().commit();
    s.close();
    s = sessionFactory().openSession();
    s.beginTransaction();
    Document d = (Document) s.createQuery("from Document fetch all properties").uniqueResult();
    assertTrue(Hibernate.isPropertyInitialized(d, "text"));
    assertTrue(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
    StandardCacheEntryImpl cacheEntry = (StandardCacheEntryImpl) region.getDataMap().get(p.getCacheAccessStrategy().generateCacheKey(d.getId(), p, sessionFactory(), null));
    assertNotNull(cacheEntry);
    sessionFactory().getStatistics().clear();
    s = sessionFactory().openSession();
    s.beginTransaction();
    d = (Document) s.createCriteria(Document.class).uniqueResult();
    assertFalse(Hibernate.isPropertyInitialized(d, "text"));
    assertFalse(Hibernate.isPropertyInitialized(d, "summary"));
    assertEquals("Hibernate is....", d.getText());
    assertTrue(Hibernate.isPropertyInitialized(d, "text"));
    assertTrue(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
    assertEquals(2, sessionFactory().getStatistics().getPrepareStatementCount());
    s = sessionFactory().openSession();
    s.beginTransaction();
    d = s.get(Document.class, d.getId());
    assertFalse(Hibernate.isPropertyInitialized(d, "text"));
    assertFalse(Hibernate.isPropertyInitialized(d, "summary"));
    s.getTransaction().commit();
    s.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BaseRegion(org.hibernate.testing.cache.BaseRegion) StandardCacheEntryImpl(org.hibernate.cache.spi.entry.StandardCacheEntryImpl) Session(org.hibernate.Session)

Aggregations

Session (org.hibernate.Session)1 StandardCacheEntryImpl (org.hibernate.cache.spi.entry.StandardCacheEntryImpl)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 BaseRegion (org.hibernate.testing.cache.BaseRegion)1