package org.hibernate.loader.plan.exec.process.internal;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.internal.CoreLogging;
import org.hibernate.loader.plan.exec.process.spi.CollectionReferenceInitializer;
import org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases;
import org.hibernate.loader.plan.spi.CollectionReference;
import org.hibernate.loader.plan.spi.Fetch;
import org.hibernate.pretty.MessageHelper;
import org.jboss.logging.Logger;
public class CollectionReferenceInitializerImpl implements CollectionReferenceInitializer {
private static final Logger log = CoreLogging.logger( CollectionReferenceInitializerImpl.class );
private final CollectionReference collectionReference;
private final CollectionReferenceAliases aliases;
public CollectionReferenceInitializerImpl(CollectionReference collectionReference, CollectionReferenceAliases aliases) {
this.collectionReference = collectionReference;
this.aliases = aliases;
}
@Override
public CollectionReference getCollectionReference() {
return collectionReference;
}
@Override
public void finishUpRow(ResultSet resultSet, ResultSetProcessingContextImpl context) {
try {
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 ) {
if ( log.isDebugEnabled() ) {
log.debugf(
"Result set contains (possibly empty) collection: %s",
MessageHelper.collectionInfoString(
collectionReference.getCollectionPersister(),
optionalKey,
context.getSession().getFactory()
)
);
}
persistenceContext.getLoadContexts()
.getCollectionLoadContext( resultSet )
.getLoadingCollection( collectionReference.getCollectionPersister(), optionalKey );
}
}
}
catch ( SQLException sqle ) {
throw context.getSession().getFactory().getSQLExceptionHelper().convert(
sqle,
"could not read next row of results"
);
}
}
protected Object findCollectionOwner(
Serializable collectionRowKey,
ResultSet resultSet,
ResultSetProcessingContextImpl context) {
final Object collectionOwner = context.getSession().getPersistenceContext().getCollectionOwner(
collectionRowKey,
collectionReference.getCollectionPersister()
);
if ( collectionOwner == null ) {
}
return collectionOwner;
}
protected Serializable findCollectionOwnerKey(ResultSetProcessingContextImpl context) {
Object owner = context.getOwnerProcessingState( (Fetch) collectionReference ).getEntityInstance();
return collectionReference.getCollectionPersister().getCollectionType().getKeyOfOwner(
owner,
context.getSession()
);
}
@Override
public void endLoading(ResultSetProcessingContextImpl context) {
context.getSession().getPersistenceContext()
.getLoadContexts()
.getCollectionLoadContext( context.getResultSet() )
.endLoadingCollections( collectionReference.getCollectionPersister() );
}
}