public void endLoadingCollections(CollectionPersister persister,
Object resultSetId,
SessionImplementor session) throws HibernateException {
// scan the loading collections for collections from this result set
// put them in a new temp collection so that we are safe from concurrent
// modification when the call to endRead() causes a proxy to be
// initialized
List resultSetCollections = null; //TODO: make this the resultSetId?
Iterator iter = loadingCollections.values().iterator();
while ( iter.hasNext() ) {
LoadingCollectionEntry lce = (LoadingCollectionEntry) iter.next();
if ( lce.resultSetId == resultSetId && lce.persister==persister) {
if ( resultSetCollections == null ) {
resultSetCollections = new ArrayList();
}
resultSetCollections.add(lce);
if ( lce.collection.getOwner()==null ) {
session.getPersistenceContext()
.addUnownedCollection(
new CollectionKey( persister, lce.key, session.getEntityMode() ),
lce.collection
);
}
iter.remove();
}
}
endLoadingCollections( persister, resultSetCollections, session.getEntityMode() );
}
Finish the process of loading collections for a particular result set |
public PersistentCollection getLoadingCollection(CollectionPersister persister,
Serializable id,
EntityMode em) {
LoadingCollectionEntry lce = getLoadingCollectionEntry( new CollectionKey(persister, id, em) );
if ( lce != null ) {
if ( log.isTraceEnabled() ) {
log.trace(
"returning loading collection:" +
MessageHelper.collectionInfoString(persister, id, context.getSession().getFactory())
);
}
return lce.collection;
}
else {
if ( log.isTraceEnabled() ) {
log.trace(
"creating collection wrapper:" +
MessageHelper.collectionInfoString(persister, id, context.getSession().getFactory())
);
}
return null;
}
}
Retrieve a collection that is in the process of being loaded, returning null
if there is no loading collection with the given id |
public PersistentCollection getLoadingCollection(CollectionPersister persister,
Serializable key,
Object resultSetId,
EntityMode em) throws HibernateException {
CollectionKey ckey = new CollectionKey(persister, key, em);
LoadingCollectionEntry lce = getLoadingCollectionEntry(ckey);
if ( lce == null ) {
//look for existing collection
PersistentCollection collection = context.getCollection(ckey);
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( "uninitialized collection: initializing" );
}
}
else {
Object entity = context.getCollectionOwner(key, persister);
final boolean newlySavedEntity = entity != null &&
context.getEntry(entity).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
log.trace( "new collection: instantiating" );
collection = persister.getCollectionType()
.instantiate( context.getSession(), persister, key );
}
}
collection.beforeInitialize(persister);
collection.beginRead();
addLoadingCollectionEntry(ckey, collection, persister, resultSetId);
return collection;
}
else {
if ( lce.resultSetId == resultSetId ) {
log.trace( "reading row" );
return lce.collection;
}
else {
// ignore this row, the collection is in process of
// being loaded somewhere further "up" the stack
log.trace( "collection is already being initialized: ignoring row" );
return null;
}
}
}
Retrieve a collection that is in the process of being loaded, instantiating
a new collection if there is nothing for the given id, or returning null
if the collection with the given id is already fully loaded in the session |