use of org.hibernate.event.spi.EventSource 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.event.spi.EventSource in project hibernate-orm by hibernate.
the class AbstractFlushingEventListener method logFlushResults.
@SuppressWarnings(value = { "unchecked" })
private void logFlushResults(FlushEvent event) {
if (!LOG.isDebugEnabled()) {
return;
}
final EventSource session = event.getSession();
final PersistenceContext persistenceContext = session.getPersistenceContext();
LOG.debugf("Flushed: %s insertions, %s updates, %s deletions to %s objects", session.getActionQueue().numberOfInsertions(), session.getActionQueue().numberOfUpdates(), session.getActionQueue().numberOfDeletions(), persistenceContext.getNumberOfManagedEntities());
LOG.debugf("Flushed: %s (re)creations, %s updates, %s removals to %s collections", session.getActionQueue().numberOfCollectionCreations(), session.getActionQueue().numberOfCollectionUpdates(), session.getActionQueue().numberOfCollectionRemovals(), persistenceContext.getCollectionEntries().size());
new EntityPrinter(session.getFactory()).toString(persistenceContext.getEntitiesByKey().entrySet());
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultLockEventListener method cascadeOnLock.
private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
EventSource source = event.getSession();
source.getPersistenceContext().incrementCascadeLevel();
try {
Cascade.cascade(CascadingActions.LOCK, CascadePoint.AFTER_LOCK, source, persister, entity, event.getLockOptions());
} finally {
source.getPersistenceContext().decrementCascadeLevel();
}
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultMergeEventListener method entityIsPersistent.
protected void entityIsPersistent(MergeEvent event, Map copyCache) {
LOG.trace("Ignoring persistent instance");
//TODO: check that entry.getIdentifier().equals(requestedId)
final Object entity = event.getEntity();
final EventSource source = event.getSession();
final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
//beforeQuery cascade!
((MergeContext) copyCache).put(entity, entity, true);
cascadeOnMerge(source, persister, entity, copyCache);
copyValues(persister, entity, entity, source, copyCache);
event.setResult(entity);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method cascadeOnUpdate.
/**
* Handles the calls needed to perform cascades as part of an update request
* for the given entity.
*
* @param event The event currently being processed.
* @param persister The defined persister for the entity being updated.
* @param entity The entity being updated.
*/
private void cascadeOnUpdate(SaveOrUpdateEvent event, EntityPersister persister, Object entity) {
final EventSource source = event.getSession();
source.getPersistenceContext().incrementCascadeLevel();
try {
Cascade.cascade(CascadingActions.SAVE_UPDATE, CascadePoint.AFTER_UPDATE, source, persister, entity);
} finally {
source.getPersistenceContext().decrementCascadeLevel();
}
}
Aggregations