use of org.hibernate.cache.spi.entry.StandardCacheEntryImpl in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method convertCacheEntryToEntity.
private Object convertCacheEntryToEntity(CacheEntry entry, Serializable entityId, EntityPersister persister, LoadEvent event, EntityKey entityKey) {
final EventSource session = event.getSession();
final SessionFactoryImplementor factory = session.getFactory();
final EntityPersister subclassPersister;
if (traceEnabled) {
LOG.tracef("Converting second-level cache entry [%s] into entity : %s", entry, MessageHelper.infoString(persister, entityId, factory));
}
final Object entity;
subclassPersister = factory.getEntityPersister(entry.getSubclass());
final Object optionalObject = event.getInstanceToLoad();
entity = optionalObject == null ? session.instantiate(subclassPersister, entityId) : optionalObject;
// make it circular-reference safe
TwoPhaseLoad.addUninitializedCachedEntity(entityKey, entity, subclassPersister, LockMode.NONE, entry.getVersion(), session);
final PersistenceContext persistenceContext = session.getPersistenceContext();
final Object[] values;
final Object version;
final boolean isReadOnly;
final Type[] types = subclassPersister.getPropertyTypes();
// initializes the entity by (desired) side-effect
values = ((StandardCacheEntryImpl) entry).assemble(entity, entityId, subclassPersister, session.getInterceptor(), session);
if (((StandardCacheEntryImpl) entry).isDeepCopyNeeded()) {
TypeHelper.deepCopy(values, types, subclassPersister.getPropertyUpdateability(), values, session);
}
version = Versioning.getVersion(values, subclassPersister);
LOG.tracef("Cached Version : %s", version);
final Object proxy = persistenceContext.getProxy(entityKey);
if (proxy != null) {
// there is already a proxy for this impl
// only set the status to read-only if the proxy is read-only
isReadOnly = ((HibernateProxy) proxy).getHibernateLazyInitializer().isReadOnly();
} else {
isReadOnly = session.isDefaultReadOnly();
}
persistenceContext.addEntry(entity, (isReadOnly ? Status.READ_ONLY : Status.MANAGED), values, null, entityId, version, LockMode.NONE, true, subclassPersister, false);
subclassPersister.afterInitialize(entity, session);
persistenceContext.initializeNonLazyCollections();
//PostLoad is needed for EJB3
PostLoadEvent postLoadEvent = event.getPostLoadEvent().setEntity(entity).setId(entityId).setPersister(persister);
for (PostLoadEventListener listener : postLoadEventListeners(session)) {
listener.onPostLoad(postLoadEvent);
}
return entity;
}
use of org.hibernate.cache.spi.entry.StandardCacheEntryImpl in project hibernate-orm by hibernate.
the class BasicUnstructuredCachingOfConvertedValueTest method basicCacheStructureTest.
@Test
@TestForIssue(jiraKey = "HHH-9615")
@SuppressWarnings("unchecked")
public void basicCacheStructureTest() {
EntityPersister persister = sessionFactory().getMetamodel().entityPersisters().get(Address.class.getName());
EntityRegionImpl region = (EntityRegionImpl) persister.getCacheAccessStrategy().getRegion();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test during store...
PostalAreaConverter.clearCounts();
Session session = openSession();
session.getTransaction().begin();
session.save(new Address(1, "123 Main St.", null, PostalArea._78729));
session.getTransaction().commit();
session.close();
{
final Object cachedItem = region.getDataMap().values().iterator().next();
final StandardCacheEntryImpl state = (StandardCacheEntryImpl) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
assertThat(state.getDisassembledState()[postalAreaAttributeIndex], instanceOf(PostalArea.class));
}
assertThat(PostalAreaConverter.toDatabaseCallCount, is(1));
assertThat(PostalAreaConverter.toDomainCallCount, is(0));
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test during load...
PostalAreaConverter.clearCounts();
region.evictAll();
session = openSession();
session.getTransaction().begin();
Address address = session.get(Address.class, 1);
session.getTransaction().commit();
session.close();
{
final Object cachedItem = region.getDataMap().values().iterator().next();
final StandardCacheEntryImpl state = (StandardCacheEntryImpl) ((ReadWriteEntityRegionAccessStrategy.Item) cachedItem).getValue();
assertThat(state.getDisassembledState()[postalAreaAttributeIndex], instanceOf(PostalArea.class));
}
assertThat(PostalAreaConverter.toDatabaseCallCount, is(0));
assertThat(PostalAreaConverter.toDomainCallCount, is(1));
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// cleanup
session = openSession();
session.getTransaction().begin();
session.delete(address);
session.getTransaction().commit();
session.close();
}
use of org.hibernate.cache.spi.entry.StandardCacheEntryImpl 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();
}
Aggregations