Search in sources :

Example 26 with PersistenceContext

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

the class CollectionType method getCollection.

/**
	 * instantiate a collection wrapper (called when loading an object)
	 *
	 * @param key The collection owner key
	 * @param session The session from which the request is originating.
	 * @param owner The collection owner
	 * @return The collection
	 */
public Object getCollection(Serializable key, SharedSessionContractImplementor session, Object owner) {
    CollectionPersister persister = getPersister(session);
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityMode entityMode = persister.getOwnerEntityPersister().getEntityMode();
    // check if collection is currently being loaded
    PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection(persister, key);
    if (collection == null) {
        // check if it is already completely loaded, but unowned
        collection = persistenceContext.useUnownedCollection(new CollectionKey(persister, key, entityMode));
        if (collection == null) {
            collection = persistenceContext.getCollection(new CollectionKey(persister, key, entityMode));
            if (collection == null) {
                // create a new collection wrapper, to be initialized later
                collection = instantiate(session, persister, key);
                collection.setOwner(owner);
                persistenceContext.addUninitializedCollection(persister, collection, key);
                // some collections are not lazy:
                if (initializeImmediately()) {
                    session.initializeCollection(collection, false);
                } else if (!persister.isLazy()) {
                    persistenceContext.addNonLazyCollection(collection);
                }
                if (hasHolder()) {
                    session.getPersistenceContext().addCollectionHolder(collection);
                }
            }
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracef("Created collection wrapper: %s", MessageHelper.collectionInfoString(persister, collection, key, session));
        }
    }
    collection.setOwner(owner);
    return collection.getValue();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CollectionKey(org.hibernate.engine.spi.CollectionKey) EntityMode(org.hibernate.EntityMode)

Example 27 with PersistenceContext

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

the class EntityType method loadByUniqueKey.

/**
	 * Load an instance by a unique key that is not the primary key.
	 *
	 * @param entityName The name of the entity to load
	 * @param uniqueKeyPropertyName The name of the property defining the uniqie key.
	 * @param key The unique key property value.
	 * @param session The originating session.
	 *
	 * @return The loaded entity
	 *
	 * @throws HibernateException generally indicates problems performing the load.
	 */
public Object loadByUniqueKey(String entityName, String uniqueKeyPropertyName, Object key, SharedSessionContractImplementor session) throws HibernateException {
    final SessionFactoryImplementor factory = session.getFactory();
    UniqueKeyLoadable persister = (UniqueKeyLoadable) factory.getMetamodel().entityPersister(entityName);
    //TODO: implement caching?! proxies?!
    EntityUniqueKey euk = new EntityUniqueKey(entityName, uniqueKeyPropertyName, key, getIdentifierOrUniqueKeyType(factory), persister.getEntityMode(), session.getFactory());
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    Object result = persistenceContext.getEntity(euk);
    if (result == null) {
        result = persister.loadByUniqueKey(uniqueKeyPropertyName, key, session);
    }
    return result == null ? null : persistenceContext.proxyFor(result);
}
Also used : EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable)

Example 28 with PersistenceContext

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

the class Collections method processDereferencedCollection.

private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
    final CollectionPersister loadedPersister = entry.getLoadedPersister();
    if (loadedPersister != null && LOG.isDebugEnabled()) {
        LOG.debugf("Collection dereferenced: %s", MessageHelper.collectionInfoString(loadedPersister, coll, entry.getLoadedKey(), session));
    }
    // do a check
    final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
    if (hasOrphanDelete) {
        Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier(coll.getOwner(), session);
        if (ownerId == null) {
            // the persistence context
            if (session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled()) {
                final EntityEntry ownerEntry = persistenceContext.getEntry(coll.getOwner());
                if (ownerEntry != null) {
                    ownerId = ownerEntry.getId();
                }
            }
            if (ownerId == null) {
                throw new AssertionFailure("Unable to determine collection owner identifier for orphan-delete processing");
            }
        }
        final EntityKey key = session.generateEntityKey(ownerId, loadedPersister.getOwnerEntityPersister());
        final Object owner = persistenceContext.getEntity(key);
        if (owner == null) {
            throw new AssertionFailure("collection owner not associated with session: " + loadedPersister.getRole());
        }
        final EntityEntry e = persistenceContext.getEntry(owner);
        //only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
        if (e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE) {
            throw new HibernateException("A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " + loadedPersister.getRole());
        }
    }
    // do the work
    entry.setCurrentPersister(null);
    entry.setCurrentKey(null);
    prepareCollectionForUpdate(coll, entry, session.getFactory());
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 29 with PersistenceContext

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

the class Collections method processNeverReferencedCollection.

private static void processNeverReferencedCollection(PersistentCollection coll, SessionImplementor session) throws HibernateException {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Found collection with unloaded owner: %s", MessageHelper.collectionInfoString(entry.getLoadedPersister(), coll, entry.getLoadedKey(), session));
    }
    entry.setCurrentPersister(entry.getLoadedPersister());
    entry.setCurrentKey(entry.getLoadedKey());
    prepareCollectionForUpdate(coll, entry, session.getFactory());
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 30 with PersistenceContext

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

the class TwoPhaseLoad method initializeEntity.

/**
	 * Perform the second step of 2-phase load. Fully initialize the entity
	 * instance.
	 * <p/>
	 * After processing a JDBC result set, we "resolve" all the associations
	 * between the entities which were instantiated and had their state
	 * "hydrated" into an array
	 *
	 * @param entity The entity being loaded
	 * @param readOnly Is the entity being loaded as read-only
	 * @param session The Session
	 * @param preLoadEvent The (re-used) pre-load event
	 */
public static void initializeEntity(final Object entity, final boolean readOnly, final SharedSessionContractImplementor session, final PreLoadEvent preLoadEvent) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityEntry entityEntry = persistenceContext.getEntry(entity);
    if (entityEntry == null) {
        throw new AssertionFailure("possible non-threadsafe access to the session");
    }
    doInitializeEntity(entity, entityEntry, readOnly, session, preLoadEvent);
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

PersistenceContext (org.hibernate.engine.spi.PersistenceContext)31 Serializable (java.io.Serializable)8 EntityEntry (org.hibernate.engine.spi.EntityEntry)8 EntityKey (org.hibernate.engine.spi.EntityKey)7 EventSource (org.hibernate.event.spi.EventSource)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 List (java.util.List)5 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)5 AssertionFailure (org.hibernate.AssertionFailure)4 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)4 Type (org.hibernate.type.Type)4 ArrayList (java.util.ArrayList)3 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)3 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 EventType (org.hibernate.event.spi.EventType)3 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3