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);
}
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);
}
}
}
}
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);
}
}
}
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());
}
}
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();
}
Aggregations