Search in sources :

Example 16 with CollectionEntry

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

the class MultipleCollectionListeners method addEvent.

public void addEvent(AbstractCollectionEvent event, Listener listener) {
    CollectionEntry collectionEntry = event.getSession().getPersistenceContext().getCollectionEntry(event.getCollection());
    Serializable snapshot = collectionEntry.getSnapshot();
    log.debug("add Event: " + event.getClass() + "; listener = " + listener.getClass() + "; snapshot = " + snapshot);
    listenersCalled.add(listener);
    events.add(event);
    snapshots.add(snapshot);
}
Also used : Serializable(java.io.Serializable) CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 17 with CollectionEntry

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

the class EnversPreCollectionRemoveEventListenerImpl method onPreRemoveCollection.

@Override
public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
    final CollectionEntry collectionEntry = getCollectionEntry(event);
    if (collectionEntry != null) {
        if (!collectionEntry.getLoadedPersister().isInverse()) {
            Serializable oldColl = collectionEntry.getSnapshot();
            if (!event.getCollection().wasInitialized() && shouldGenerateRevision(event)) {
                // In case of uninitialized collection we need a fresh snapshot to properly calculate audit data.
                oldColl = initializeCollection(event);
            }
            onCollectionAction(event, null, oldColl, collectionEntry);
        } else {
            // HHH-7510 - Avoid LazyInitializationException when global_with_modified_flag = true
            if (getEnversService().getGlobalConfiguration().isGlobalWithModifiedFlag()) {
                initializeCollection(event);
            }
        }
    }
}
Also used : Serializable(java.io.Serializable) CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 18 with CollectionEntry

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

the class AbstractFlushingEventListener method postFlush.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Post-flushing section
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
	 * 1. Recreate the collection key -> collection map
	 * 2. rebuild the collection entries
	 * 3. call Interceptor.postFlush()
	 */
protected void postFlush(SessionImplementor session) throws HibernateException {
    LOG.trace("Post flush");
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    persistenceContext.getCollectionsByKey().clear();
    // the database has changed now, so the subselect results need to be invalidated
    // the batch fetching queues should also be cleared - especially the collection batch fetching one
    persistenceContext.getBatchFetchQueue().clear();
    for (Map.Entry<PersistentCollection, CollectionEntry> me : IdentityMap.concurrentEntries(persistenceContext.getCollectionEntries())) {
        CollectionEntry collectionEntry = me.getValue();
        PersistentCollection persistentCollection = me.getKey();
        collectionEntry.postFlush(persistentCollection);
        if (collectionEntry.getLoadedPersister() == null) {
            //if the collection is dereferenced, unset its session reference and remove from the session cache
            //iter.remove(); //does not work, since the entrySet is not backed by the set
            persistentCollection.unsetSession(session);
            persistenceContext.getCollectionEntries().remove(persistentCollection);
        } else {
            //otherwise recreate the mapping between the collection and its key
            CollectionKey collectionKey = new CollectionKey(collectionEntry.getLoadedPersister(), collectionEntry.getLoadedKey());
            persistenceContext.getCollectionsByKey().put(collectionKey, persistentCollection);
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CollectionKey(org.hibernate.engine.spi.CollectionKey) Map(java.util.Map) IdentityMap(org.hibernate.internal.util.collections.IdentityMap)

Example 19 with CollectionEntry

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

the class CollectionLoadContext method endLoadingCollection.

private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersister persister) {
    LOG.tracev("Ending loading collection [{0}]", lce);
    final SharedSessionContractImplementor session = getLoadContext().getPersistenceContext().getSession();
    // warning: can cause a recursive calls! (proxy initialization)
    final boolean hasNoQueuedAdds = lce.getCollection().endRead();
    if (persister.getCollectionType().hasHolder()) {
        getLoadContext().getPersistenceContext().addCollectionHolder(lce.getCollection());
    }
    CollectionEntry ce = getLoadContext().getPersistenceContext().getCollectionEntry(lce.getCollection());
    if (ce == null) {
        ce = getLoadContext().getPersistenceContext().addInitializedCollection(persister, lce.getCollection(), lce.getKey());
    } else {
        ce.postInitialize(lce.getCollection());
    //			if (ce.getLoadedPersister().getBatchSize() > 1) { // not the best place for doing this, moved into ce.postInitialize
    //				getLoadContext().getPersistenceContext().getBatchFetchQueue().removeBatchLoadableCollection(ce); 
    //			}
    }
    // add to cache if:
    boolean addToCache = // there were no queued additions
    hasNoQueuedAdds && // and the role has a cache
    persister.hasCache() && // and this is not a forced initialization during flush
    session.getCacheMode().isPutEnabled() && !ce.isDoremove();
    if (addToCache) {
        addCollectionToCache(lce, persister);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Collection fully initialized: %s", MessageHelper.collectionInfoString(persister, lce.getCollection(), lce.getKey(), session));
    }
    if (session.getFactory().getStatistics().isStatisticsEnabled()) {
        session.getFactory().getStatistics().loadCollection(persister.getRole());
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 20 with CollectionEntry

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

the class AbstractPersistentCollection method generateUnexpectedSessionStateMessage.

private String generateUnexpectedSessionStateMessage(SharedSessionContractImplementor session) {
    // NOTE: If this.session != null, this.session may be operating on this collection
    // (e.g., by changing this.role, this.key, or even this.session) in a different thread.
    // Grab the current role and key (it can still get changed by this.session...)
    // If this collection is connected to this.session, then this.role and this.key should
    // be consistent with the CollectionEntry in this.session (as long as this.session doesn't
    // change it). Don't access the CollectionEntry in this.session because that could result
    // in multi-threaded access to this.session.
    final String roleCurrent = role;
    final Serializable keyCurrent = key;
    final StringBuilder sb = new StringBuilder("Collection : ");
    if (roleCurrent != null) {
        sb.append(MessageHelper.collectionInfoString(roleCurrent, keyCurrent));
    } else {
        final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(this);
        if (ce != null) {
            sb.append(MessageHelper.collectionInfoString(ce.getLoadedPersister(), this, ce.getLoadedKey(), session));
        } else {
            sb.append("<unknown>");
        }
    }
    // only include the collection contents if debug logging
    if (LOG.isDebugEnabled()) {
        final String collectionContents = wasInitialized() ? toString() : "<uninitialized>";
        sb.append("\nCollection contents: [").append(collectionContents).append("]");
    }
    return sb.toString();
}
Also used : Serializable(java.io.Serializable) 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