log.trace( "starting attempt to find loading collection [" + MessageHelper.collectionInfoString( persister.getRole(), key ) + "]" );
}
final LoadingCollectionEntry loadingCollectionEntry = loadContexts.locateLoadingCollectionEntry( collectionKey );
if ( loadingCollectionEntry == null ) {
// look for existing collection as part of the persistence context
PersistentCollection collection = loadContexts.getPersistenceContext().getCollection( collectionKey );
if ( collection != null ) {
if ( collection.wasInitialized() ) {
log.trace( "collection already initialized; ignoring" );
return null; // ignore this row of results! Note the early exit
}
else {
// initialize this collection
log.trace( "collection not yet initialized; initializing" );
}
}
else {
Object owner = loadContexts.getPersistenceContext().getCollectionOwner( key, persister );
final boolean newlySavedEntity = owner != null
&& loadContexts.getPersistenceContext().getEntry( owner ).getStatus() != Status.LOADING
&& em != EntityMode.DOM4J;
if ( newlySavedEntity ) {
// important, to account for newly saved entities in query
// todo : some kind of check for new status...
log.trace( "owning entity already loaded; ignoring" );
return null;
}
else {
// create one
if ( log.isTraceEnabled() ) {
log.trace( "instantiating new collection [key=" + key + ", rs=" + resultSet + "]" );
}
collection = persister.getCollectionType()
.instantiate( loadContexts.getPersistenceContext().getSession(), persister, key );
}
}
collection.beforeInitialize( persister, -1 );
collection.beginRead();
localLoadingCollectionKeys.add( collectionKey );
loadContexts.registerLoadingCollectionXRef( collectionKey, new LoadingCollectionEntry( resultSet, persister, key, collection ) );
return collection;
}
else {