use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method performUpdate.
protected void performUpdate(SaveOrUpdateEvent event, Object entity, EntityPersister persister) throws HibernateException {
final boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled && !persister.isMutable()) {
LOG.trace("Immutable instance passed to performUpdate()");
}
if (traceEnabled) {
LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), event.getSession().getFactory()));
}
final EventSource source = event.getSession();
final EntityKey key = source.generateEntityKey(event.getRequestedId(), persister);
source.getPersistenceContext().checkUniqueness(key, entity);
if (invokeUpdateLifecycle(entity, persister, source)) {
reassociate(event, event.getObject(), event.getRequestedId(), persister);
return;
}
// this is a transient object with existing persistent state not loaded by the session
new OnUpdateVisitor(source, event.getRequestedId(), entity).process(entity, persister);
// TODO: put this stuff back in to read snapshot from
// the second-level cache (needs some extra work)
/*Object[] cachedState = null;
if ( persister.hasCache() ) {
CacheEntry entry = (CacheEntry) persister.getCache()
.get( event.getRequestedId(), source.getTimestamp() );
cachedState = entry==null ?
null :
entry.getState(); //TODO: half-assemble this stuff
}*/
source.getPersistenceContext().addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), // cachedState,
null, key, persister.getVersion(entity), LockMode.NONE, true, persister, false);
persister.afterReassociate(entity, source);
if (traceEnabled) {
LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), source.getFactory()));
}
cascadeOnUpdate(event, persister, entity);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class JpaPostUpdateEventListener method onPostUpdateCollection.
@Override
public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
Object entity = event.getCollection().getOwner();
EventSource eventSource = event.getSession();
handlePostUpdate(entity, eventSource);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class JpaPostUpdateEventListener method onPostRecreateCollection.
@Override
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
Object entity = event.getCollection().getOwner();
EventSource eventSource = event.getSession();
handlePostUpdate(entity, eventSource);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class JpaPostUpdateEventListener method onPostRemoveCollection.
@Override
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
Object entity = event.getCollection().getOwner();
EventSource eventSource = event.getSession();
handlePostUpdate(entity, eventSource);
}
use of org.hibernate.event.spi.EventSource 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());
}
}
}
}
}
Aggregations