use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class AuditReaderImpl method getCurrentRevision.
@Override
@SuppressWarnings({ "unchecked" })
public <T> T getCurrentRevision(Class<T> revisionEntityClass, boolean persist) {
revisionEntityClass = getTargetClassIfProxied(revisionEntityClass);
if (!(session instanceof EventSource)) {
throw new IllegalArgumentException("The provided session is not an EventSource!");
}
// Obtaining the current audit sync
final AuditProcess auditProcess = enversService.getAuditProcessManager().get((EventSource) session);
// And getting the current revision data
return (T) auditProcess.getCurrentRevisionData(session, persist);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class AbstractFlushingEventListener method flushEntities.
/**
* 1. detect any dirty entities
* 2. schedule any entity updates
* 3. search out any reachable collections
*/
private int flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException {
LOG.trace("Flushing entities and processing referenced collections");
final EventSource source = event.getSession();
final Iterable<FlushEntityEventListener> flushListeners = source.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.FLUSH_ENTITY).listeners();
// Among other things, updateReachables() will recursively load all
// collections that are moving roles. This might cause entities to
// be loaded.
// So this needs to be safe from concurrent modification problems.
final Map.Entry<Object, EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries();
final int count = entityEntries.length;
for (Map.Entry<Object, EntityEntry> me : entityEntries) {
// Update the status of the object and if necessary, schedule an update
EntityEntry entry = me.getValue();
Status status = entry.getStatus();
if (status != Status.LOADING && status != Status.GONE) {
final FlushEntityEvent entityEvent = new FlushEntityEvent(source, me.getKey(), entry);
for (FlushEntityEventListener listener : flushListeners) {
listener.onFlushEntity(entityEvent);
}
}
}
source.getActionQueue().sortActions();
return count;
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class AbstractReassociateEventListener method reassociate.
/**
* Associates a given entity (either transient or associated with another session) to
* the given session.
*
* @param event The event triggering the re-association
* @param object The entity to be associated
* @param id The id of the entity.
* @param persister The entity's persister instance.
*
* @return An EntityEntry representing the entity within this session.
*/
protected final EntityEntry reassociate(AbstractEvent event, Object object, Serializable id, EntityPersister persister) {
if (log.isTraceEnabled()) {
log.tracev("Reassociating transient instance: {0}", MessageHelper.infoString(persister, id, event.getSession().getFactory()));
}
final EventSource source = event.getSession();
final EntityKey key = source.generateEntityKey(id, persister);
source.getPersistenceContext().checkUniqueness(key, object);
//get a snapshot
Object[] values = persister.getPropertyValues(object);
TypeHelper.deepCopy(values, persister.getPropertyTypes(), persister.getPropertyUpdateability(), values, source);
Object version = Versioning.getVersion(values, persister);
EntityEntry newEntry = source.getPersistenceContext().addEntity(object, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), values, key, version, LockMode.NONE, true, persister, false);
new OnLockVisitor(source, id, object).process(object, persister);
persister.afterReassociate(object, source);
return newEntry;
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultAutoFlushEventListener method onAutoFlush.
/**
* Handle the given auto-flush event.
*
* @param event
* The auto-flush event to be handled.
* @throws HibernateException
*/
public void onAutoFlush(AutoFlushEvent event) throws HibernateException {
final EventSource source = event.getSession();
try {
source.getEventListenerManager().partialFlushStart();
if (flushMightBeNeeded(source)) {
// Need to get the number of collection removals beforeQuery flushing to executions
// (because flushing to executions can add collection removal actions to the action queue).
final int oldSize = source.getActionQueue().numberOfCollectionRemovals();
flushEverythingToExecutions(event);
if (flushIsReallyNeeded(event, source)) {
LOG.trace("Need to execute flush");
// note: performExecutions() clears all collectionXxxxtion
// collections (the collection actions) in the session
performExecutions(source);
postFlush(source);
postPostFlush(source);
if (source.getFactory().getStatistics().isStatisticsEnabled()) {
source.getFactory().getStatistics().flush();
}
} else {
LOG.trace("Don't need to execute flush");
source.getActionQueue().clearFromFlushNeededCheck(oldSize);
}
event.setFlushRequired(flushIsReallyNeeded(event, source));
}
} finally {
source.getEventListenerManager().partialFlushEnd(event.getNumberOfEntitiesProcessed(), event.getNumberOfEntitiesProcessed());
}
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultDeleteEventListener method onDelete.
/**
* Handle the given delete event. This is the cascaded form.
*
* @param event The delete event.
* @param transientEntities The cache of entities already deleted
*
* @throws HibernateException
*/
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
final EventSource source = event.getSession();
final PersistenceContext persistenceContext = source.getPersistenceContext();
Object entity = persistenceContext.unproxyAndReassociate(event.getObject());
EntityEntry entityEntry = persistenceContext.getEntry(entity);
final EntityPersister persister;
final Serializable id;
final Object version;
if (entityEntry == null) {
LOG.trace("Entity was not persistent in delete processing");
persister = source.getEntityPersister(event.getEntityName(), entity);
if (ForeignKeys.isTransient(persister.getEntityName(), entity, null, source)) {
deleteTransientEntity(source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities);
// EARLY EXIT!!!
return;
}
performDetachedEntityDeletionCheck(event);
id = persister.getIdentifier(entity, source);
if (id == null) {
throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
}
final EntityKey key = source.generateEntityKey(id, persister);
persistenceContext.checkUniqueness(key, entity);
new OnUpdateVisitor(source, id, entity).process(entity, persister);
version = persister.getVersion(entity);
entityEntry = persistenceContext.addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), persister.getPropertyValues(entity), key, version, LockMode.NONE, true, persister, false);
} else {
LOG.trace("Deleting a persistent instance");
if (entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE) {
LOG.trace("Object was already deleted");
return;
}
persister = entityEntry.getPersister();
id = entityEntry.getId();
version = entityEntry.getVersion();
}
if (invokeDeleteLifecycle(source, entity, persister)) {
return;
}
deleteEntity(source, entity, entityEntry, event.isCascadeDeleteEnabled(), event.isOrphanRemovalBeforeUpdates(), persister, transientEntities);
if (source.getFactory().getSettings().isIdentifierRollbackEnabled()) {
persister.resetIdentifier(entity, id, version, source);
}
}
Aggregations