Search in sources :

Example 11 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class WrapVisitor method processArrayOrNewCollection.

final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) throws HibernateException {
    final SessionImplementor session = getSession();
    if (collection == null) {
        //do nothing
        return null;
    } else {
        CollectionPersister persister = session.getFactory().getCollectionPersister(collectionType.getRole());
        final PersistenceContext persistenceContext = session.getPersistenceContext();
        //TODO: move into collection type, so we can use polymorphism!
        if (collectionType.hasHolder()) {
            if (collection == CollectionType.UNFETCHED_COLLECTION) {
                return null;
            }
            PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
            if (ah == null) {
                ah = collectionType.wrap(session, collection);
                persistenceContext.addNewCollection(persister, ah);
                persistenceContext.addCollectionHolder(ah);
            }
            return null;
        } else {
            PersistentCollection persistentCollection = collectionType.wrap(session, collection);
            persistenceContext.addNewCollection(persister, persistentCollection);
            if (LOG.isTraceEnabled()) {
                LOG.tracev("Wrapped collection in role: {0}", collectionType.getRole());
            }
            //Force a substitution!
            return persistentCollection;
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 12 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class Loader method readCollectionElement.

/**
	 * Read one collection element from the current row of the JDBC result set
	 */
private void readCollectionElement(final Object optionalOwner, final Serializable optionalKey, final CollectionPersister persister, final CollectionAliases descriptor, final ResultSet rs, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final Serializable collectionRowKey = (Serializable) persister.readKey(rs, descriptor.getSuffixedKeyAliases(), session);
    if (collectionRowKey != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Found row of collection: %s", MessageHelper.collectionInfoString(persister, collectionRowKey, getFactory()));
        }
        Object owner = optionalOwner;
        if (owner == null) {
            owner = persistenceContext.getCollectionOwner(collectionRowKey, persister);
            if (owner == null) {
            //TODO: This is assertion is disabled because there is a bug that means the
            //	  original owner of a transient, uninitialized collection is not known
            //	  if the collection is re-referenced by a different object associated
            //	  with the current Session
            //throw new AssertionFailure("bug loading unowned collection");
            }
        }
        PersistentCollection rowCollection = persistenceContext.getLoadContexts().getCollectionLoadContext(rs).getLoadingCollection(persister, collectionRowKey);
        if (rowCollection != null) {
            rowCollection.readFrom(rs, persister, descriptor, owner);
        }
    } else if (optionalKey != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Result set contains (possibly empty) collection: %s", MessageHelper.collectionInfoString(persister, optionalKey, getFactory()));
        }
        persistenceContext.getLoadContexts().getCollectionLoadContext(rs).getLoadingCollection(persister, // handle empty collection
        optionalKey);
    }
// else no collection element, but also no owner
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 13 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class Loader method registerNonExists.

/**
	 * For missing objects associated by one-to-one with another object in the
	 * result set, register the fact that the the object is missing with the
	 * session.
	 */
private void registerNonExists(final EntityKey[] keys, final Loadable[] persisters, final SharedSessionContractImplementor session) {
    final int[] owners = getOwners();
    if (owners != null) {
        EntityType[] ownerAssociationTypes = getOwnerAssociationTypes();
        for (int i = 0; i < keys.length; i++) {
            int owner = owners[i];
            if (owner > -1) {
                EntityKey ownerKey = keys[owner];
                if (keys[i] == null && ownerKey != null) {
                    final PersistenceContext persistenceContext = session.getPersistenceContext();
                    /*final boolean isPrimaryKey;
						final boolean isSpecialOneToOne;
						if ( ownerAssociationTypes == null || ownerAssociationTypes[i] == null ) {
							isPrimaryKey = true;
							isSpecialOneToOne = false;
						}
						else {
							isPrimaryKey = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName()==null;
							isSpecialOneToOne = ownerAssociationTypes[i].getLHSPropertyName()!=null;
						}*/
                    //TODO: can we *always* use the "null property" approach for everything?
                    /*if ( isPrimaryKey && !isSpecialOneToOne ) {
							persistenceContext.addNonExistantEntityKey(
									new EntityKey( ownerKey.getIdentifier(), persisters[i], session.getEntityMode() )
							);
						}
						else if ( isSpecialOneToOne ) {*/
                    boolean isOneToOneAssociation = ownerAssociationTypes != null && ownerAssociationTypes[i] != null && ownerAssociationTypes[i].isOneToOne();
                    if (isOneToOneAssociation) {
                        persistenceContext.addNullProperty(ownerKey, ownerAssociationTypes[i].getPropertyName());
                    }
                /*}
						else {
							persistenceContext.addNonExistantEntityUniqueKey( new EntityUniqueKey(
									persisters[i].getEntityName(),
									ownerAssociationTypes[i].getRHSUniqueKeyPropertyName(),
									ownerKey.getIdentifier(),
									persisters[owner].getIdentifierType(),
									session.getEntityMode()
							) );
						}*/
                }
            }
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) EntityKey(org.hibernate.engine.spi.EntityKey) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 14 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method getSubselectInitializer.

private CollectionInitializer getSubselectInitializer(Serializable key, SharedSessionContractImplementor session) {
    if (!isSubselectLoadable()) {
        return null;
    }
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    SubselectFetch subselect = persistenceContext.getBatchFetchQueue().getSubselect(session.generateEntityKey(key, getOwnerEntityPersister()));
    if (subselect == null) {
        return null;
    } else {
        // Take care of any entities that might have
        // been evicted!
        Iterator iter = subselect.getResult().iterator();
        while (iter.hasNext()) {
            if (!persistenceContext.containsEntity((EntityKey) iter.next())) {
                iter.remove();
            }
        }
        // Run a subquery loader
        return createSubselectInitializer(subselect, session);
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) Iterator(java.util.Iterator) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 15 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class CollectionReferenceInitializerImpl method finishUpRow.

@Override
public void finishUpRow(ResultSet resultSet, ResultSetProcessingContextImpl context) {
    try {
        // read the collection key for this reference for the current row.
        final PersistenceContext persistenceContext = context.getSession().getPersistenceContext();
        final Serializable collectionRowKey = (Serializable) collectionReference.getCollectionPersister().readKey(resultSet, aliases.getCollectionColumnAliases().getSuffixedKeyAliases(), context.getSession());
        if (collectionRowKey != null) {
            if (log.isDebugEnabled()) {
                log.debugf("Found row of collection: %s", MessageHelper.collectionInfoString(collectionReference.getCollectionPersister(), collectionRowKey, context.getSession().getFactory()));
            }
            Object collectionOwner = findCollectionOwner(collectionRowKey, resultSet, context);
            PersistentCollection rowCollection = persistenceContext.getLoadContexts().getCollectionLoadContext(resultSet).getLoadingCollection(collectionReference.getCollectionPersister(), collectionRowKey);
            if (rowCollection != null) {
                rowCollection.readFrom(resultSet, collectionReference.getCollectionPersister(), aliases.getCollectionColumnAliases(), collectionOwner);
            }
        } else {
            final Serializable optionalKey = findCollectionOwnerKey(context);
            if (optionalKey != null) {
                // since what we have is an empty collection
                if (log.isDebugEnabled()) {
                    log.debugf("Result set contains (possibly empty) collection: %s", MessageHelper.collectionInfoString(collectionReference.getCollectionPersister(), optionalKey, context.getSession().getFactory()));
                }
                // handle empty collection
                persistenceContext.getLoadContexts().getCollectionLoadContext(resultSet).getLoadingCollection(collectionReference.getCollectionPersister(), optionalKey);
            }
        }
    // else no collection element, but also no owner
    } catch (SQLException sqle) {
        // TODO: would be nice to have the SQL string that failed...
        throw context.getSession().getFactory().getSQLExceptionHelper().convert(sqle, "could not read next row of results");
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

PersistenceContext (org.hibernate.engine.spi.PersistenceContext)31 Serializable (java.io.Serializable)8 EntityEntry (org.hibernate.engine.spi.EntityEntry)8 EntityKey (org.hibernate.engine.spi.EntityKey)7 EventSource (org.hibernate.event.spi.EventSource)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 List (java.util.List)5 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)5 AssertionFailure (org.hibernate.AssertionFailure)4 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)4 Type (org.hibernate.type.Type)4 ArrayList (java.util.ArrayList)3 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)3 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 EventType (org.hibernate.event.spi.EventType)3 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3