Search in sources :

Example 16 with EventSource

use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.

the class DefaultMergeEventListener method entityIsTransient.

protected void entityIsTransient(MergeEvent event, Map copyCache) {
    LOG.trace("Merging transient instance");
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();
    final String entityName = event.getEntityName();
    final EntityPersister persister = source.getEntityPersister(entityName, entity);
    final Serializable id = persister.hasIdentifierProperty() ? persister.getIdentifier(entity, source) : null;
    if (copyCache.containsKey(entity)) {
        persister.setIdentifier(copyCache.get(entity), id, source);
    } else {
        //beforeQuery cascade!
        ((MergeContext) copyCache).put(entity, source.instantiate(persister, id), true);
    }
    final Object copy = copyCache.get(entity);
    // cascade first, so that all unsaved objects get their
    // copy created beforeQuery we actually copy
    //cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
    super.cascadeBeforeSave(source, persister, entity, copyCache);
    copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.FROM_PARENT);
    saveTransientEntity(copy, entityName, event.getRequestedId(), source, copyCache);
    // cascade first, so that all unsaved objects get their
    // copy created beforeQuery we actually copy
    super.cascadeAfterSave(source, persister, entity, copyCache);
    copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.TO_PARENT);
    event.setResult(copy);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable)

Example 17 with EventSource

use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.

the class OnReplicateVisitor method processCollection.

@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == CollectionType.UNFETCHED_COLLECTION) {
        return null;
    }
    final EventSource session = getSession();
    final CollectionPersister persister = session.getFactory().getMetamodel().collectionPersister(type.getRole());
    if (isUpdate) {
        removeCollection(persister, extractCollectionKeyFromOwner(persister), session);
    }
    if (collection != null && collection instanceof PersistentCollection) {
        final PersistentCollection wrapper = (PersistentCollection) collection;
        wrapper.setCurrentSession((SessionImplementor) session);
        if (wrapper.wasInitialized()) {
            session.getPersistenceContext().addNewCollection(persister, wrapper);
        } else {
            reattachCollection(wrapper, type);
        }
    } else {
    // otherwise a null or brand new collection
    // this will also (inefficiently) handle arrays, which
    // have no snapshot, so we can't do any better
    //processArrayOrNewCollection(collection, type);
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventSource(org.hibernate.event.spi.EventSource) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Example 18 with EventSource

use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.

the class OnUpdateVisitor method processCollection.

@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == CollectionType.UNFETCHED_COLLECTION) {
        return null;
    }
    EventSource session = getSession();
    CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
    final Serializable collectionKey = extractCollectionKeyFromOwner(persister);
    if (collection != null && (collection instanceof PersistentCollection)) {
        PersistentCollection wrapper = (PersistentCollection) collection;
        if (wrapper.setCurrentSession(session)) {
            //a "detached" collection!
            if (!isOwnerUnchanged(wrapper, persister, collectionKey)) {
                // if the collection belonged to a different entity,
                // clean up the existing state of the collection
                removeCollection(persister, collectionKey, session);
            }
            reattachCollection(wrapper, type);
        } else {
            // a collection loaded in the current session
            // can not possibly be the collection belonging
            // to the entity passed to update()
            removeCollection(persister, collectionKey, session);
        }
    } else {
        // null or brand new collection
        // this will also (inefficiently) handle arrays, which have
        // no snapshot, so we can't do any better
        removeCollection(persister, collectionKey, session);
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Example 19 with EventSource

use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.

the class DefaultSaveOrUpdateEventListener method entityIsTransient.

/**
	 * The given save-update event named a transient entity.
	 * <p/>
	 * Here, we will perform the save processing.
	 *
	 * @param event The save event to be handled.
	 *
	 * @return The entity's identifier afterQuery saving.
	 */
protected Serializable entityIsTransient(SaveOrUpdateEvent event) {
    LOG.trace("Saving transient instance");
    final EventSource source = event.getSession();
    EntityEntry entityEntry = event.getEntry();
    if (entityEntry != null) {
        if (entityEntry.getStatus() == Status.DELETED) {
            source.forceFlush(entityEntry);
        } else {
            throw new AssertionFailure("entity was persistent");
        }
    }
    Serializable id = saveWithGeneratedOrRequestedId(event);
    source.getPersistenceContext().reassociateProxy(event.getObject(), id);
    return id;
}
Also used : EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) AssertionFailure(org.hibernate.AssertionFailure)

Example 20 with EventSource

use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.

the class JpaDeleteEventListener method performDetachedEntityDeletionCheck.

@Override
protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
    EventSource source = event.getSession();
    String entityName = event.getEntityName();
    EntityPersister persister = source.getEntityPersister(entityName, event.getObject());
    Serializable id = persister.getIdentifier(event.getObject(), source);
    entityName = entityName == null ? source.guessEntityName(event.getObject()) : entityName;
    throw new IllegalArgumentException("Removing a detached instance " + entityName + "#" + id);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable)

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