Search in sources :

Example 1 with EventSource

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);
}
Also used : EventSource(org.hibernate.event.spi.EventSource) AuditProcess(org.hibernate.envers.internal.synchronization.AuditProcess)

Example 2 with EventSource

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;
}
Also used : Status(org.hibernate.engine.spi.Status) EventSource(org.hibernate.event.spi.EventSource) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) FlushEntityEvent(org.hibernate.event.spi.FlushEntityEvent) FlushEntityEventListener(org.hibernate.event.spi.FlushEntityEventListener) Map(java.util.Map) IdentityMap(org.hibernate.internal.util.collections.IdentityMap) CascadePoint(org.hibernate.engine.internal.CascadePoint)

Example 3 with EventSource

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;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 4 with EventSource

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());
    }
}
Also used : EventSource(org.hibernate.event.spi.EventSource)

Example 5 with EventSource

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);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TransientObjectException(org.hibernate.TransientObjectException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

EventSource (org.hibernate.event.spi.EventSource)36 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 Serializable (java.io.Serializable)10 EntityEntry (org.hibernate.engine.spi.EntityEntry)10 EntityKey (org.hibernate.engine.spi.EntityKey)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)7 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)5 HibernateProxy (org.hibernate.proxy.HibernateProxy)4 AssertionFailure (org.hibernate.AssertionFailure)3 HibernateException (org.hibernate.HibernateException)3 Status (org.hibernate.engine.spi.Status)3 PreLoadEvent (org.hibernate.event.spi.PreLoadEvent)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 Type (org.hibernate.type.Type)3 TransientObjectException (org.hibernate.TransientObjectException)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)2 CascadePoint (org.hibernate.engine.internal.CascadePoint)2 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EventType (org.hibernate.event.spi.EventType)2