【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-CollectionLoadContext.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
//$Id: CollectionLoadContext.java 7764 2005-08-05 16:16:46Z oneovthafew $
package org.hibernate.engine;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.CacheMode;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.cache.CacheKey;
import org.hibernate.cache.entry.CollectionCacheEntry;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.pretty.MessageHelper;
/**
* Represents the state of collections currently being loaded. Eventually, I
* would like to have multiple instances of this per session - one per JDBC
* result set, instead of the resultSetId being passed.
* @author Gavin King
*/
public class CollectionLoadContext {
private static final Log log = LogFactory.getLog(CollectionLoadContext.class);
// The collections we are currently loading
private final Map loadingCollections = new HashMap(8);
private final PersistenceContext context;
public CollectionLoadContext(PersistenceContext context) {
this.context = context;
}
private static final class LoadingCollectionEntry {
final PersistentCollection collection;
final Serializable key;
final Object resultSetId;
final CollectionPersister persister;
LoadingCollectionEntry(
final PersistentCollection collection,
final Serializable key,
final CollectionPersister persister,
final Object resultSetId
) {
this.collection = collection;
this.key = key;
this.persister = persister;
this.resultSetId = resultSetId;
}
}
/**
* 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
*/
public PersistentCollection getLoadingCollection(
final CollectionPersister persister,
final Serializable key,
final Object resultSetId,
final 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, res