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;
}
}
}
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
}
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()
) );
}*/
}
}
}
}
}
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);
}
}
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");
}
}
Aggregations