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