Search in sources :

Example 1 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class CollectionRemoveAction method execute.

@Override
public void execute() throws HibernateException {
    preRemove();
    if (!emptySnapshot) {
        // an existing collection that was either non-empty or uninitialized
        // is replaced by null or a different collection
        // (if the collection is uninitialized, hibernate has no way of
        // knowing if the collection is actually empty without querying the db)
        getPersister().remove(getKey(), getSession());
    }
    final PersistentCollection collection = getCollection();
    if (collection != null) {
        getSession().getPersistenceContext().getCollectionEntry(collection).afterAction(collection);
    }
    evict();
    postRemove();
    if (getSession().getFactory().getStatistics().isStatisticsEnabled()) {
        getSession().getFactory().getStatistics().removeCollection(getPersister().getRole());
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection)

Example 2 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class CollectionRecreateAction method execute.

@Override
public void execute() throws HibernateException {
    // this method is called when a new non-null collection is persisted
    // or when an existing (non-null) collection is moved to a new owner
    final PersistentCollection collection = getCollection();
    preRecreate();
    getPersister().recreate(collection, getKey(), getSession());
    getSession().getPersistenceContext().getCollectionEntry(collection).afterAction(collection);
    evict();
    postRecreate();
    if (getSession().getFactory().getStatistics().isStatisticsEnabled()) {
        getSession().getFactory().getStatistics().recreateCollection(getPersister().getRole());
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection)

Example 3 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractFlushingEventListener method flushCollections.

/**
	 * process any unreferenced collections and then inspect all known collections,
	 * scheduling creates/removes/updates
	 */
@SuppressWarnings("unchecked")
private int flushCollections(final EventSource session, final PersistenceContext persistenceContext) throws HibernateException {
    LOG.trace("Processing unreferenced collections");
    final Map.Entry<PersistentCollection, CollectionEntry>[] entries = IdentityMap.concurrentEntries((Map<PersistentCollection, CollectionEntry>) persistenceContext.getCollectionEntries());
    final int count = entries.length;
    for (Map.Entry<PersistentCollection, CollectionEntry> me : entries) {
        CollectionEntry ce = me.getValue();
        if (!ce.isReached() && !ce.isIgnore()) {
            Collections.processUnreachableCollection(me.getKey(), session);
        }
    }
    // Schedule updates to collections:
    LOG.trace("Scheduling collection removes/(re)creates/updates");
    ActionQueue actionQueue = session.getActionQueue();
    for (Map.Entry<PersistentCollection, CollectionEntry> me : IdentityMap.concurrentEntries((Map<PersistentCollection, CollectionEntry>) persistenceContext.getCollectionEntries())) {
        PersistentCollection coll = me.getKey();
        CollectionEntry ce = me.getValue();
        if (ce.isDorecreate()) {
            session.getInterceptor().onCollectionRecreate(coll, ce.getCurrentKey());
            actionQueue.addAction(new CollectionRecreateAction(coll, ce.getCurrentPersister(), ce.getCurrentKey(), session));
        }
        if (ce.isDoremove()) {
            session.getInterceptor().onCollectionRemove(coll, ce.getLoadedKey());
            actionQueue.addAction(new CollectionRemoveAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        if (ce.isDoupdate()) {
            session.getInterceptor().onCollectionUpdate(coll, ce.getLoadedKey());
            actionQueue.addAction(new CollectionUpdateAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        // todo : I'm not sure the !wasInitialized part should really be part of this check
        if (!coll.wasInitialized() && coll.hasQueuedOperations()) {
            actionQueue.addAction(new QueuedOperationCollectionAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), session));
        }
    }
    actionQueue.sortCollectionActions();
    return count;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ActionQueue(org.hibernate.engine.spi.ActionQueue) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) QueuedOperationCollectionAction(org.hibernate.action.internal.QueuedOperationCollectionAction) CollectionRecreateAction(org.hibernate.action.internal.CollectionRecreateAction) CollectionRemoveAction(org.hibernate.action.internal.CollectionRemoveAction) CollectionUpdateAction(org.hibernate.action.internal.CollectionUpdateAction) Map(java.util.Map) IdentityMap(org.hibernate.internal.util.collections.IdentityMap) CascadePoint(org.hibernate.engine.internal.CascadePoint)

Example 4 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class DefaultInitializeCollectionEventListener method onInitializeCollection.

/**
	 * called by a collection that wants to initialize itself
	 */
public void onInitializeCollection(InitializeCollectionEvent event) throws HibernateException {
    PersistentCollection collection = event.getCollection();
    SessionImplementor source = event.getSession();
    CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
    if (ce == null) {
        throw new HibernateException("collection was evicted");
    }
    if (!collection.wasInitialized()) {
        final boolean traceEnabled = LOG.isTraceEnabled();
        if (traceEnabled) {
            LOG.tracev("Initializing collection {0}", MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), source));
            LOG.trace("Checking second-level cache");
        }
        final boolean foundInCache = initializeCollectionFromCache(ce.getLoadedKey(), ce.getLoadedPersister(), collection, source);
        if (foundInCache) {
            if (traceEnabled) {
                LOG.trace("Collection initialized from cache");
            }
        } else {
            if (traceEnabled) {
                LOG.trace("Collection not cached");
            }
            ce.getLoadedPersister().initialize(ce.getLoadedKey(), source);
            if (traceEnabled) {
                LOG.trace("Collection initialized");
            }
            if (source.getFactory().getStatistics().isStatisticsEnabled()) {
                source.getFactory().getStatisticsImplementor().fetchCollection(ce.getLoadedPersister().getRole());
            }
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 5 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class BatchFetchQueue method getCollectionBatch.

/**
	 * Get a batch of uninitialized collection keys for a given role
	 *
	 * @param collectionPersister The persister for the collection role.
	 * @param id A key that must be included in the batch fetch
	 * @param batchSize the maximum number of keys to return
	 * @return an array of collection keys, of length batchSize (padded with nulls)
	 */
public Serializable[] getCollectionBatch(final CollectionPersister collectionPersister, final Serializable id, final int batchSize) {
    Serializable[] keys = new Serializable[batchSize];
    keys[0] = id;
    int i = 1;
    int end = -1;
    boolean checkForEnd = false;
    final LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get(collectionPersister.getRole());
    if (map != null) {
        for (Entry<CollectionEntry, PersistentCollection> me : map.entrySet()) {
            final CollectionEntry ce = me.getKey();
            final PersistentCollection collection = me.getValue();
            if (ce.getLoadedKey() == null) {
                // against potentially null loadedKeys (which leads to various NPEs as demonstrated in HHH-7821).
                continue;
            }
            if (collection.wasInitialized()) {
                // should never happen
                LOG.warn("Encountered initialized collection in BatchFetchQueue, this should not happen.");
                continue;
            }
            if (checkForEnd && i == end) {
                //the first key found afterQuery the given key
                return keys;
            }
            final boolean isEqual = collectionPersister.getKeyType().isEqual(id, ce.getLoadedKey(), collectionPersister.getFactory());
            if (isEqual) {
                end = i;
            //checkForEnd = false;
            } else if (!isCached(ce.getLoadedKey(), collectionPersister)) {
                keys[i++] = ce.getLoadedKey();
            //count++;
            }
            if (i == batchSize) {
                //end of array, start filling again from start
                i = 1;
                if (end != -1) {
                    checkForEnd = true;
                }
            }
        }
    }
    //we ran out of keys to try
    return keys;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable)

Aggregations

PersistentCollection (org.hibernate.collection.spi.PersistentCollection)53 Session (org.hibernate.Session)20 Test (org.junit.Test)20 Transaction (org.hibernate.Transaction)16 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)15 Collection (java.util.Collection)13 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)11 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Serializable (java.io.Serializable)6 CollectionKey (org.hibernate.engine.spi.CollectionKey)6 HibernateException (org.hibernate.HibernateException)5 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)5 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)5 Map (java.util.Map)4 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3