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