Search in sources :

Example 26 with CollectionEntry

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

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

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

the class Collections method processReachableCollection.

/**
     * Initialize the role of the collection.
     *
     * @param collection The collection to be updated by reachability.
     * @param type The type of the collection.
     * @param entity The owner of the collection.
	 * @param session The session from which this request originates
     */
public static void processReachableCollection(PersistentCollection collection, CollectionType type, Object entity, SessionImplementor session) {
    collection.setOwner(entity);
    final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(collection);
    if (ce == null) {
        // refer to comment in StatefulPersistenceContext.addCollection()
        throw new HibernateException("Found two representations of same collection: " + type.getRole());
    }
    final SessionFactoryImplementor factory = session.getFactory();
    final CollectionPersister persister = factory.getMetamodel().collectionPersister(type.getRole());
    ce.setCurrentPersister(persister);
    //TODO: better to pass the id in as an argument?
    ce.setCurrentKey(type.getKeyOfOwner(entity, session));
    final boolean isBytecodeEnhanced = persister.getOwnerEntityPersister().getInstrumentationMetadata().isEnhancedForLazyLoading();
    if (isBytecodeEnhanced && !collection.wasInitialized()) {
        // skip it
        LOG.debugf("Skipping uninitialized bytecode-lazy collection: %s", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session));
        ce.setReached(true);
        ce.setProcessed(true);
    } else {
        // who set up circular or shared references between/to collections.
        if (ce.isReached()) {
            // We've been here beforeQuery
            throw new HibernateException("Found shared references to a collection: " + type.getRole());
        }
        ce.setReached(true);
        if (LOG.isDebugEnabled()) {
            if (collection.wasInitialized()) {
                LOG.debugf("Collection found: %s, was: %s (initialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
            } else {
                LOG.debugf("Collection found: %s, was: %s (uninitialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
            }
        }
        prepareCollectionForUpdate(collection, ce, factory);
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor)

Example 29 with CollectionEntry

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

the class StatefulPersistenceContext method addInitializedDetachedCollection.

@Override
public void addInitializedDetachedCollection(CollectionPersister collectionPersister, PersistentCollection collection) throws HibernateException {
    if (collection.isUnreferenced()) {
        //treat it just like a new collection
        addCollection(collection, collectionPersister);
    } else {
        final CollectionEntry ce = new CollectionEntry(collection, session.getFactory());
        addCollection(collection, ce, collection.getKey());
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 30 with CollectionEntry

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

the class StatefulPersistenceContext method addUninitializedCollection.

@Override
public void addUninitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id) {
    final CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
    addCollection(collection, ce, id);
    if (persister.getBatchSize() > 1) {
        getBatchFetchQueue().addBatchLoadableCollection(collection, ce);
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Aggregations

CollectionEntry (org.hibernate.engine.spi.CollectionEntry)42 Session (org.hibernate.Session)19 TestForIssue (org.hibernate.testing.TestForIssue)19 Test (org.junit.Test)19 EntityEntry (org.hibernate.engine.spi.EntityEntry)16 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)11 Serializable (java.io.Serializable)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Map (java.util.Map)5 Triggerable (org.hibernate.testing.logger.Triggerable)5 HibernateException (org.hibernate.HibernateException)4 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 HashMap (java.util.HashMap)3 EntityKey (org.hibernate.engine.spi.EntityKey)3 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 IdentityHashMap (java.util.IdentityHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CollectionKey (org.hibernate.engine.spi.CollectionKey)2