use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class CollectionType method getCollection.
/**
* instantiate a collection wrapper (called when loading an object)
*
* @param key The collection owner key
* @param session The session from which the request is originating.
* @param owner The collection owner
* @return The collection
*/
public Object getCollection(Serializable key, SharedSessionContractImplementor session, Object owner) {
CollectionPersister persister = getPersister(session);
final PersistenceContext persistenceContext = session.getPersistenceContext();
final EntityMode entityMode = persister.getOwnerEntityPersister().getEntityMode();
// check if collection is currently being loaded
PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection(persister, key);
if (collection == null) {
// check if it is already completely loaded, but unowned
collection = persistenceContext.useUnownedCollection(new CollectionKey(persister, key, entityMode));
if (collection == null) {
collection = persistenceContext.getCollection(new CollectionKey(persister, key, entityMode));
if (collection == null) {
// create a new collection wrapper, to be initialized later
collection = instantiate(session, persister, key);
collection.setOwner(owner);
persistenceContext.addUninitializedCollection(persister, collection, key);
// some collections are not lazy:
if (initializeImmediately()) {
session.initializeCollection(collection, false);
} else if (!persister.isLazy()) {
persistenceContext.addNonLazyCollection(collection);
}
if (hasHolder()) {
session.getPersistenceContext().addCollectionHolder(collection);
}
}
}
if (LOG.isTraceEnabled()) {
LOG.tracef("Created collection wrapper: %s", MessageHelper.collectionInfoString(persister, collection, key, session));
}
}
collection.setOwner(owner);
return collection.getValue();
}
use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class EntityType method loadByUniqueKey.
/**
* Load an instance by a unique key that is not the primary key.
*
* @param entityName The name of the entity to load
* @param uniqueKeyPropertyName The name of the property defining the uniqie key.
* @param key The unique key property value.
* @param session The originating session.
*
* @return The loaded entity
*
* @throws HibernateException generally indicates problems performing the load.
*/
public Object loadByUniqueKey(String entityName, String uniqueKeyPropertyName, Object key, SharedSessionContractImplementor session) throws HibernateException {
final SessionFactoryImplementor factory = session.getFactory();
UniqueKeyLoadable persister = (UniqueKeyLoadable) factory.getMetamodel().entityPersister(entityName);
//TODO: implement caching?! proxies?!
EntityUniqueKey euk = new EntityUniqueKey(entityName, uniqueKeyPropertyName, key, getIdentifierOrUniqueKeyType(factory), persister.getEntityMode(), session.getFactory());
final PersistenceContext persistenceContext = session.getPersistenceContext();
Object result = persistenceContext.getEntity(euk);
if (result == null) {
result = persister.loadByUniqueKey(uniqueKeyPropertyName, key, session);
}
return result == null ? null : persistenceContext.proxyFor(result);
}
use of org.hibernate.engine.spi.PersistenceContext 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.PersistenceContext 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.PersistenceContext in project hibernate-orm by hibernate.
the class TwoPhaseLoad method initializeEntity.
/**
* Perform the second step of 2-phase load. Fully initialize the entity
* instance.
* <p/>
* After processing a JDBC result set, we "resolve" all the associations
* between the entities which were instantiated and had their state
* "hydrated" into an array
*
* @param entity The entity being loaded
* @param readOnly Is the entity being loaded as read-only
* @param session The Session
* @param preLoadEvent The (re-used) pre-load event
*/
public static void initializeEntity(final Object entity, final boolean readOnly, final SharedSessionContractImplementor session, final PreLoadEvent preLoadEvent) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final EntityEntry entityEntry = persistenceContext.getEntry(entity);
if (entityEntry == null) {
throw new AssertionFailure("possible non-threadsafe access to the session");
}
doInitializeEntity(entity, entityEntry, readOnly, session, preLoadEvent);
}
Aggregations