use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method reassociateProxy.
/**
* Associate a proxy that was instantiated by another session with this session
*
* @param li The proxy initializer.
* @param proxy The proxy to reassociate.
*/
private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) {
if (li.getSession() != this.getSession()) {
final EntityPersister persister = session.getFactory().getMetamodel().entityPersister(li.getEntityName());
final EntityKey key = session.generateEntityKey(li.getIdentifier(), persister);
// any earlier proxy takes precedence
proxiesByKey.putIfAbsent(key, proxy);
proxy.getHibernateLazyInitializer().setSession(session);
}
}
use of org.hibernate.engine.spi.EntityKey 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.EntityKey in project hibernate-orm by hibernate.
the class AbstractLazyInitializer method setReadOnly.
@Override
public final void setReadOnly(boolean readOnly) {
errorIfReadOnlySettingNotAvailable();
// only update if readOnly is different from current setting
if (this.readOnly != readOnly) {
final EntityPersister persister = session.getFactory().getEntityPersister(entityName);
if (!persister.isMutable() && !readOnly) {
throw new IllegalStateException("cannot make proxies for immutable entities modifiable");
}
this.readOnly = readOnly;
if (initialized) {
EntityKey key = generateEntityKeyOrNull(getIdentifier(), session, getEntityName());
if (key != null && session.getPersistenceContext().containsEntity(key)) {
session.getPersistenceContext().setReadOnly(target, readOnly);
}
}
}
}
Aggregations