Search in sources :

Example 11 with CollectionEntry

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

the class StatefulPersistenceContext method addCollection.

/**
	 * Add a collection to the cache, creating a new collection entry for it
	 *
	 * @param collection The collection for which we are adding an entry.
	 * @param persister The collection persister
	 */
private void addCollection(PersistentCollection collection, CollectionPersister persister) {
    final CollectionEntry ce = new CollectionEntry(persister, collection);
    collectionEntries.put(collection, ce);
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 12 with CollectionEntry

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

the class StatefulPersistenceContext method addInitializedCollection.

@Override
public CollectionEntry addInitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id) throws HibernateException {
    final CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
    ce.postInitialize(collection);
    addCollection(collection, ce, id);
    return ce;
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 13 with CollectionEntry

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

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

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

the class SessionImpl method getFilterQueryPlan.

private FilterQueryPlan getFilterQueryPlan(Object collection, String filter, QueryParameters parameters, boolean shallow) throws HibernateException {
    if (collection == null) {
        throw new NullPointerException("null collection passed to filter");
    }
    CollectionEntry entry = persistenceContext.getCollectionEntryOrNull(collection);
    final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();
    FilterQueryPlan plan = null;
    if (roleBeforeFlush == null) {
        // if it was previously unreferenced, we need to flush in order to
        // get its state into the database in order to execute query
        flush();
        entry = persistenceContext.getCollectionEntryOrNull(collection);
        CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
        if (roleAfterFlush == null) {
            throw new QueryException("The collection was unreferenced");
        }
        plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
    } else {
        // otherwise, we only need to flush if there are in-memory changes
        // to the queried tables
        plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleBeforeFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
        if (autoFlushIfRequired(plan.getQuerySpaces())) {
            // might need to run a different filter entirely afterQuery the flush
            // because the collection role may have changed
            entry = persistenceContext.getCollectionEntryOrNull(collection);
            CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
            if (roleBeforeFlush != roleAfterFlush) {
                if (roleAfterFlush == null) {
                    throw new QueryException("The collection was dereferenced");
                }
                plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
            }
        }
    }
    if (parameters != null) {
        parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
        parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
    }
    return plan;
}
Also used : FilterQueryPlan(org.hibernate.engine.query.spi.FilterQueryPlan) QueryException(org.hibernate.QueryException) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

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