Search in sources :

Example 11 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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 12 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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 13 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class AbstractSaveEventListener method performSaveOrReplicate.

/**
	 * Performs all the actual work needed to save an entity (well to get the save moved to
	 * the execution queue).
	 *
	 * @param entity The entity to be saved
	 * @param key The id to be used for saving the entity (or null, in the case of identity columns)
	 * @param persister The entity's persister instance.
	 * @param useIdentityColumn Should an identity column be used for id generation?
	 * @param anything Generally cascade-specific information.
	 * @param source The session which is the source of the current event.
	 * @param requiresImmediateIdAccess Is access to the identifier required immediately
	 * afterQuery the completion of the save?  persist(), for example, does not require this...
	 *
	 * @return The id used to save the entity; may be null depending on the
	 *         type of id generator used and the requiresImmediateIdAccess value
	 */
protected Serializable performSaveOrReplicate(Object entity, EntityKey key, EntityPersister persister, boolean useIdentityColumn, Object anything, EventSource source, boolean requiresImmediateIdAccess) {
    Serializable id = key == null ? null : key.getIdentifier();
    boolean inTxn = source.isTransactionInProgress();
    boolean shouldDelayIdentityInserts = !inTxn && !requiresImmediateIdAccess;
    // Put a placeholder in entries, so we don't recurse back and try to save() the
    // same object again. QUESTION: should this be done beforeQuery onSave() is called?
    // likewise, should it be done beforeQuery onUpdate()?
    EntityEntry original = source.getPersistenceContext().addEntry(entity, Status.SAVING, null, null, id, null, LockMode.WRITE, useIdentityColumn, persister, false);
    cascadeBeforeSave(source, persister, entity, anything);
    Object[] values = persister.getPropertyValuesToInsert(entity, getMergeMap(anything), source);
    Type[] types = persister.getPropertyTypes();
    boolean substitute = substituteValuesIfNecessary(entity, id, values, persister, source);
    if (persister.hasCollections()) {
        substitute = substitute || visitCollectionsBeforeSave(entity, id, values, types, source);
    }
    if (substitute) {
        persister.setPropertyValues(entity, values);
    }
    TypeHelper.deepCopy(values, types, persister.getPropertyUpdateability(), values, source);
    AbstractEntityInsertAction insert = addInsertAction(values, id, entity, persister, useIdentityColumn, source, shouldDelayIdentityInserts);
    // postpone initializing id in case the insert has non-nullable transient dependencies
    // that are not resolved until cascadeAfterSave() is executed
    cascadeAfterSave(source, persister, entity, anything);
    if (useIdentityColumn && insert.isEarlyInsert()) {
        if (!EntityIdentityInsertAction.class.isInstance(insert)) {
            throw new IllegalStateException("Insert should be using an identity column, but action is of unexpected type: " + insert.getClass().getName());
        }
        id = ((EntityIdentityInsertAction) insert).getGeneratedId();
        insert.handleNaturalIdPostSaveNotifications(id);
    }
    EntityEntry newEntry = source.getPersistenceContext().getEntry(entity);
    if (newEntry != original) {
        EntityEntryExtraState extraState = newEntry.getExtraState(EntityEntryExtraState.class);
        if (extraState == null) {
            newEntry.addExtraState(original.getExtraState(EntityEntryExtraState.class));
        }
    }
    return id;
}
Also used : EntityIdentityInsertAction(org.hibernate.action.internal.EntityIdentityInsertAction) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) Type(org.hibernate.type.Type) AbstractEntityInsertAction(org.hibernate.action.internal.AbstractEntityInsertAction) EntityEntryExtraState(org.hibernate.engine.spi.EntityEntryExtraState)

Example 14 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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)

Example 15 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class DefaultEvictEventListener method onEvict.

/**
	 * Handle the given evict event.
	 *
	 * @param event The evict event to be handled.
	 *
	 * @throws HibernateException
	 */
public void onEvict(EvictEvent event) throws HibernateException {
    final Object object = event.getObject();
    if (object == null) {
        throw new NullPointerException("null passed to Session.evict()");
    }
    final EventSource source = event.getSession();
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    if (object instanceof HibernateProxy) {
        final LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        final Serializable id = li.getIdentifier();
        if (id == null) {
            throw new IllegalArgumentException("Could not determine identifier of proxy passed to evict()");
        }
        final EntityPersister persister = source.getFactory().getEntityPersister(li.getEntityName());
        final EntityKey key = source.generateEntityKey(id, persister);
        persistenceContext.removeProxy(key);
        if (!li.isUninitialized()) {
            final Object entity = persistenceContext.removeEntity(key);
            if (entity != null) {
                EntityEntry e = persistenceContext.removeEntry(entity);
                doEvict(entity, key, e.getPersister(), event.getSession());
            }
        }
        li.unsetSession();
    } else {
        EntityEntry e = persistenceContext.removeEntry(object);
        if (e != null) {
            persistenceContext.removeEntity(e.getEntityKey());
            doEvict(object, e.getEntityKey(), e.getPersister(), source);
        } else {
            // see if the passed object is even an entity, and if not throw an exception
            // 		this is different than legacy Hibernate behavior, but what JPA 2.1 is calling for
            //		with EntityManager.detach
            EntityPersister persister = null;
            final String entityName = persistenceContext.getSession().guessEntityName(object);
            if (entityName != null) {
                try {
                    persister = persistenceContext.getSession().getFactory().getEntityPersister(entityName);
                } catch (Exception ignore) {
                }
            }
            if (persister == null) {
                throw new IllegalArgumentException("Non-entity object instance passed to evict : " + object);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) HibernateException(org.hibernate.HibernateException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Aggregations

EntityEntry (org.hibernate.engine.spi.EntityEntry)79 Serializable (java.io.Serializable)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)21 Session (org.hibernate.Session)18 Test (org.junit.Test)17 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)16 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 EntityKey (org.hibernate.engine.spi.EntityKey)12 TestForIssue (org.hibernate.testing.TestForIssue)12 AssertionFailure (org.hibernate.AssertionFailure)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)11 HibernateException (org.hibernate.HibernateException)10 EventSource (org.hibernate.event.spi.EventSource)9 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)8 TransientObjectException (org.hibernate.TransientObjectException)7 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Status (org.hibernate.engine.spi.Status)6 LazyInitializer (org.hibernate.proxy.LazyInitializer)6 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5