use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class Collections method processDereferencedCollection.
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
final CollectionPersister loadedPersister = entry.getLoadedPersister();
if (loadedPersister != null && LOG.isDebugEnabled()) {
LOG.debugf("Collection dereferenced: %s", MessageHelper.collectionInfoString(loadedPersister, coll, entry.getLoadedKey(), session));
}
// do a check
final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
if (hasOrphanDelete) {
Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier(coll.getOwner(), session);
if (ownerId == null) {
// the persistence context
if (session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled()) {
final EntityEntry ownerEntry = persistenceContext.getEntry(coll.getOwner());
if (ownerEntry != null) {
ownerId = ownerEntry.getId();
}
}
if (ownerId == null) {
throw new AssertionFailure("Unable to determine collection owner identifier for orphan-delete processing");
}
}
final EntityKey key = session.generateEntityKey(ownerId, loadedPersister.getOwnerEntityPersister());
final Object owner = persistenceContext.getEntity(key);
if (owner == null) {
throw new AssertionFailure("collection owner not associated with session: " + loadedPersister.getRole());
}
final EntityEntry e = persistenceContext.getEntry(owner);
//only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
if (e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE) {
throw new HibernateException("A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " + loadedPersister.getRole());
}
}
// do the work
entry.setCurrentPersister(null);
entry.setCurrentKey(null);
prepareCollectionForUpdate(coll, entry, session.getFactory());
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class Collections method processNeverReferencedCollection.
private static void processNeverReferencedCollection(PersistentCollection coll, SessionImplementor session) throws HibernateException {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
if (LOG.isDebugEnabled()) {
LOG.debugf("Found collection with unloaded owner: %s", MessageHelper.collectionInfoString(entry.getLoadedPersister(), coll, entry.getLoadedKey(), session));
}
entry.setCurrentPersister(entry.getLoadedPersister());
entry.setCurrentKey(entry.getLoadedKey());
prepareCollectionForUpdate(coll, entry, session.getFactory());
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class Collections method processReachableCollection.
/**
* Initialize the role of the collection.
*
* @param collection The collection to be updated by reachability.
* @param type The type of the collection.
* @param entity The owner of the collection.
* @param session The session from which this request originates
*/
public static void processReachableCollection(PersistentCollection collection, CollectionType type, Object entity, SessionImplementor session) {
collection.setOwner(entity);
final CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(collection);
if (ce == null) {
// refer to comment in StatefulPersistenceContext.addCollection()
throw new HibernateException("Found two representations of same collection: " + type.getRole());
}
final SessionFactoryImplementor factory = session.getFactory();
final CollectionPersister persister = factory.getMetamodel().collectionPersister(type.getRole());
ce.setCurrentPersister(persister);
//TODO: better to pass the id in as an argument?
ce.setCurrentKey(type.getKeyOfOwner(entity, session));
final boolean isBytecodeEnhanced = persister.getOwnerEntityPersister().getInstrumentationMetadata().isEnhancedForLazyLoading();
if (isBytecodeEnhanced && !collection.wasInitialized()) {
// skip it
LOG.debugf("Skipping uninitialized bytecode-lazy collection: %s", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session));
ce.setReached(true);
ce.setProcessed(true);
} else {
// who set up circular or shared references between/to collections.
if (ce.isReached()) {
// We've been here beforeQuery
throw new HibernateException("Found shared references to a collection: " + type.getRole());
}
ce.setReached(true);
if (LOG.isDebugEnabled()) {
if (collection.wasInitialized()) {
LOG.debugf("Collection found: %s, was: %s (initialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
} else {
LOG.debugf("Collection found: %s, was: %s (uninitialized)", MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session), MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
}
}
prepareCollectionForUpdate(collection, ce, factory);
}
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method addInitializedDetachedCollection.
@Override
public void addInitializedDetachedCollection(CollectionPersister collectionPersister, PersistentCollection collection) throws HibernateException {
if (collection.isUnreferenced()) {
//treat it just like a new collection
addCollection(collection, collectionPersister);
} else {
final CollectionEntry ce = new CollectionEntry(collection, session.getFactory());
addCollection(collection, ce, collection.getKey());
}
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method addUninitializedCollection.
@Override
public void addUninitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id) {
final CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
addCollection(collection, ce, id);
if (persister.getBatchSize() > 1) {
getBatchFetchQueue().addBatchLoadableCollection(collection, ce);
}
}
Aggregations