use of org.hibernate.event.spi.PostLoadEventListener in project hibernate-orm by hibernate.
the class TwoPhaseLoad method postLoad.
/**
* PostLoad cannot occur during initializeEntity, as that call occurs *before*
* the Set collections are added to the persistence context by Loader.
* Without the split, LazyInitializationExceptions can occur in the Entity's
* postLoad if it acts upon the collection.
*
* HHH-6043
*
* @param entity The entity
* @param session The Session
* @param postLoadEvent The (re-used) post-load event
*/
public static void postLoad(final Object entity, final SharedSessionContractImplementor session, final PostLoadEvent postLoadEvent) {
if (session.isEventSource()) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final EntityEntry entityEntry = persistenceContext.getEntry(entity);
postLoadEvent.setEntity(entity).setId(entityEntry.getId()).setPersister(entityEntry.getPersister());
final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.POST_LOAD);
for (PostLoadEventListener listener : listenerGroup.listeners()) {
listener.onPostLoad(postLoadEvent);
}
}
}
use of org.hibernate.event.spi.PostLoadEventListener in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method assembleCacheEntry.
private Object assembleCacheEntry(final StandardCacheEntryImpl entry, final Serializable id, final EntityPersister persister, final LoadEvent event) throws HibernateException {
final Object optionalObject = event.getInstanceToLoad();
final EventSource session = event.getSession();
final SessionFactoryImplementor factory = session.getFactory();
if (traceEnabled) {
LOG.tracev("Assembling entity from second-level cache: {0}", MessageHelper.infoString(persister, id, factory));
}
EntityPersister subclassPersister = factory.getEntityPersister(entry.getSubclass());
Object result = optionalObject == null ? session.instantiate(subclassPersister, id) : optionalObject;
// make it circular-reference safe
final EntityKey entityKey = session.generateEntityKey(id, subclassPersister);
TwoPhaseLoad.addUninitializedCachedEntity(entityKey, result, subclassPersister, LockMode.NONE, entry.getVersion(), session);
Type[] types = subclassPersister.getPropertyTypes();
Object[] values = entry.assemble(result, id, subclassPersister, session.getInterceptor(), session);
// intializes result by side-effect
TypeHelper.deepCopy(values, types, subclassPersister.getPropertyUpdateability(), values, session);
Object version = Versioning.getVersion(values, subclassPersister);
LOG.tracev("Cached Version: {0}", version);
final PersistenceContext persistenceContext = session.getPersistenceContext();
boolean isReadOnly = session.isDefaultReadOnly();
if (persister.isMutable()) {
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 = true;
}
persistenceContext.addEntry(result, (isReadOnly ? Status.READ_ONLY : Status.MANAGED), values, null, id, version, LockMode.NONE, true, subclassPersister, false);
subclassPersister.afterInitialize(result, session);
persistenceContext.initializeNonLazyCollections();
// upgrade the lock if necessary:
// lock(result, lockMode);
// PostLoad is needed for EJB3
// TODO: reuse the PostLoadEvent...
PostLoadEvent postLoadEvent = event.getPostLoadEvent().setEntity(result).setId(id).setPersister(persister);
for (PostLoadEventListener listener : postLoadEventListeners(session)) {
listener.onPostLoad(postLoadEvent);
}
return result;
}
use of org.hibernate.event.spi.PostLoadEventListener 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;
}
Aggregations