Search in sources :

Example 41 with EntityKey

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);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey)

Example 42 with EntityKey

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());
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 43 with EntityKey

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);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey)

Aggregations

EntityKey (org.hibernate.engine.spi.EntityKey)43 Serializable (java.io.Serializable)13 EntityEntry (org.hibernate.engine.spi.EntityEntry)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 EventSource (org.hibernate.event.spi.EventSource)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)7 List (java.util.List)6 EntityType (org.hibernate.type.EntityType)6 ArrayList (java.util.ArrayList)5 SQLException (java.sql.SQLException)4 EntityReferenceProcessingState (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HibernateException (org.hibernate.HibernateException)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3 Type (org.hibernate.type.Type)3 HashSet (java.util.HashSet)2