use of org.hibernate.event.spi.PreLoadEvent in project hibernate-orm by hibernate.
the class AbstractRowReader method finishUp.
@Override
public void finishUp(ResultSetProcessingContextImpl context, List<AfterLoadAction> afterLoadActionList) {
final List<HydratedEntityRegistration> hydratedEntityRegistrations = context.getHydratedEntityRegistrationList();
// for arrays, we should end the collection load beforeQuery resolving the entities, since the
// actual array instances are not instantiated during loading
finishLoadingArrays(context);
// IMPORTANT: reuse the same event instances for performance!
final PreLoadEvent preLoadEvent;
final PostLoadEvent postLoadEvent;
if (context.getSession().isEventSource()) {
preLoadEvent = new PreLoadEvent((EventSource) context.getSession());
postLoadEvent = new PostLoadEvent((EventSource) context.getSession());
} else {
preLoadEvent = null;
postLoadEvent = null;
}
// now finish loading the entities (2-phase load)
performTwoPhaseLoad(preLoadEvent, context, hydratedEntityRegistrations);
// now we can finalize loading collections
finishLoadingCollections(context);
// finally, perform post-load operations
postLoad(postLoadEvent, context, hydratedEntityRegistrations, afterLoadActionList);
}
use of org.hibernate.event.spi.PreLoadEvent in project hibernate-orm by hibernate.
the class Loader method initializeEntitiesAndCollections.
private void initializeEntitiesAndCollections(final List hydratedObjects, final Object resultSetId, final SharedSessionContractImplementor session, final boolean readOnly, List<AfterLoadAction> afterLoadActions) throws HibernateException {
final CollectionPersister[] collectionPersisters = getCollectionPersisters();
if (collectionPersisters != null) {
for (CollectionPersister collectionPersister : collectionPersisters) {
if (collectionPersister.isArray()) {
//for arrays, we should end the collection load beforeQuery resolving
//the entities, since the actual array instances are not instantiated
//during loading
//TODO: or we could do this polymorphically, and have two
// different operations implemented differently for arrays
endCollectionLoad(resultSetId, session, collectionPersister);
}
}
}
//important: reuse the same event instances for performance!
final PreLoadEvent pre;
final PostLoadEvent post;
if (session.isEventSource()) {
pre = new PreLoadEvent((EventSource) session);
post = new PostLoadEvent((EventSource) session);
} else {
pre = null;
post = null;
}
if (hydratedObjects != null) {
int hydratedObjectsSize = hydratedObjects.size();
LOG.tracev("Total objects hydrated: {0}", hydratedObjectsSize);
for (Object hydratedObject : hydratedObjects) {
TwoPhaseLoad.initializeEntity(hydratedObject, readOnly, session, pre);
}
}
if (collectionPersisters != null) {
for (CollectionPersister collectionPersister : collectionPersisters) {
if (!collectionPersister.isArray()) {
//for sets, we should end the collection load afterQuery resolving
//the entities, since we might call hashCode() on the elements
//TODO: or we could do this polymorphically, and have two
// different operations implemented differently for arrays
endCollectionLoad(resultSetId, session, collectionPersister);
}
}
}
// persistence context.
if (hydratedObjects != null) {
for (Object hydratedObject : hydratedObjects) {
TwoPhaseLoad.postLoad(hydratedObject, session, post);
if (afterLoadActions != null) {
for (AfterLoadAction afterLoadAction : afterLoadActions) {
final EntityEntry entityEntry = session.getPersistenceContext().getEntry(hydratedObject);
if (entityEntry == null) {
// big problem
throw new HibernateException("Could not locate EntityEntry immediately afterQuery two-phase load");
}
afterLoadAction.afterLoad(session, hydratedObject, (Loadable) entityEntry.getPersister());
}
}
}
}
}
use of org.hibernate.event.spi.PreLoadEvent in project hibernate-orm by hibernate.
the class StandardCacheEntryImpl method assemble.
/**
* Assemble the previously disassembled state represented by this entry into the given entity instance.
*
* Additionally manages the PreLoadEvent callbacks.
*
* @param instance The entity instance
* @param id The entity identifier
* @param persister The entity persister
* @param interceptor (currently unused)
* @param session The session
*
* @return The assembled state
*
* @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
*
* @see org.hibernate.type.Type#assemble
* @see org.hibernate.type.Type#disassemble
*/
public Object[] assemble(final Object instance, final Serializable id, final EntityPersister persister, final Interceptor interceptor, final EventSource session) throws HibernateException {
if (!persister.getEntityName().equals(subclass)) {
throw new AssertionFailure("Tried to assemble a different subclass instance");
}
//assembled state gets put in a new array (we read from cache by value!)
final Object[] assembledProps = TypeHelper.assemble(disassembledState, persister.getPropertyTypes(), session, instance);
//persister.setIdentifier(instance, id); //beforeQuery calling interceptor, for consistency with normal load
//TODO: reuse the PreLoadEvent
final PreLoadEvent preLoadEvent = new PreLoadEvent(session).setEntity(instance).setState(assembledProps).setId(id).setPersister(persister);
final EventListenerGroup<PreLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.PRE_LOAD);
for (PreLoadEventListener listener : listenerGroup.listeners()) {
listener.onPreLoad(preLoadEvent);
}
persister.setPropertyValues(instance, assembledProps);
return assembledProps;
}
use of org.hibernate.event.spi.PreLoadEvent in project hibernate-orm by hibernate.
the class CustomPersister method load.
/**
* @see EntityPersister#load(Serializable, Object, LockMode, SharedSessionContractImplementor)
*/
public Object load(Serializable id, Object optionalObject, LockMode lockMode, SharedSessionContractImplementor session) throws HibernateException {
// fails when optional object is supplied
Custom clone = null;
Custom obj = (Custom) INSTANCES.get(id);
if (obj != null) {
clone = (Custom) obj.clone();
TwoPhaseLoad.addUninitializedEntity(session.generateEntityKey(id, this), clone, this, LockMode.NONE, session);
TwoPhaseLoad.postHydrate(this, id, new String[] { obj.getName() }, null, clone, LockMode.NONE, session);
TwoPhaseLoad.initializeEntity(clone, false, session, new PreLoadEvent((EventSource) session));
TwoPhaseLoad.postLoad(clone, session, new PostLoadEvent((EventSource) session));
}
return clone;
}
Aggregations