diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index 5a3ccd8f6b..065f354bb2 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -896,6 +896,9 @@ public class StatefulPersistenceContext implements PersistenceContext { public void addUninitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id) { CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing); addCollection(collection, ce, id); + if ( persister.getBatchSize() > 1 ) { + getBatchFetchQueue().addBatchLoadableCollection( collection, ce ); + } } /** @@ -905,6 +908,9 @@ public class StatefulPersistenceContext implements PersistenceContext { public void addUninitializedDetachedCollection(CollectionPersister persister, PersistentCollection collection) { CollectionEntry ce = new CollectionEntry( persister, collection.getKey() ); addCollection( collection, ce, collection.getKey() ); + if ( persister.getBatchSize() > 1 ) { + getBatchFetchQueue().addBatchLoadableCollection( collection, ce ); + } } /** diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CollectionEntry.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CollectionEntry.java index 1edb372459..ba8b819be5 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/CollectionEntry.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CollectionEntry.java @@ -34,6 +34,7 @@ import org.jboss.logging.Logger; import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.MappingException; +import org.hibernate.collection.internal.AbstractPersistentCollection; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.persister.collection.CollectionPersister; @@ -215,6 +216,12 @@ public final class CollectionEntry implements Serializable { collection.getSnapshot( getLoadedPersister() ) : null; collection.setSnapshot(loadedKey, role, snapshot); + if ( getLoadedPersister().getBatchSize() > 1 ) { + ( (AbstractPersistentCollection) collection ).getSession() + .getPersistenceContext() + .getBatchFetchQueue() + .removeBatchLoadableCollection( this ); + } } /** diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/EvictVisitor.java b/hibernate-core/src/main/java/org/hibernate/event/internal/EvictVisitor.java index 14a0f6ffae..3382a4688a 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/EvictVisitor.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/EvictVisitor.java @@ -83,6 +83,9 @@ public class EvictVisitor extends AbstractVisitor { ce.getLoadedKey(), getSession() ) ); } + if ( ce.getLoadedPersister() != null && ce.getLoadedPersister().getBatchSize() > 1 ) { + getSession().getPersistenceContext().getBatchFetchQueue().removeBatchLoadableCollection( ce ); + } if ( ce.getLoadedPersister() != null && ce.getLoadedKey() != null ) { //TODO: is this 100% correct? getSession().getPersistenceContext().getCollectionsByKey().remove( diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index fc77d3ff05..ffdb03674f 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -1913,6 +1913,11 @@ public abstract class AbstractCollectionPersister return initializer; } + @Override + public int getBatchSize() { + return batchSize; + } + private class StandardOrderByAliasResolver implements OrderByAliasResolver { private final String rootAlias; diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/CollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/CollectionPersister.java index 463372d9a3..2e6541f21f 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/CollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/CollectionPersister.java @@ -307,4 +307,6 @@ public interface CollectionPersister { public boolean indexExists(Serializable key, Object index, SessionImplementor session); public boolean elementExists(Serializable key, Object element, SessionImplementor session); public Object getElementByIndex(Serializable key, Object index, SessionImplementor session, Object owner); + + public int getBatchSize(); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/ManyToOneType.java b/hibernate-core/src/main/java/org/hibernate/type/ManyToOneType.java index 80b8fa6ba4..446dbf3467 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/ManyToOneType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/ManyToOneType.java @@ -169,7 +169,7 @@ public class ManyToOneType extends EntityType { if ( uniqueKeyPropertyName == null && id != null ) { final EntityPersister persister = session.getFactory().getEntityPersister( getAssociatedEntityName() ); final EntityKey entityKey = session.generateEntityKey( id, persister ); - if ( !session.getPersistenceContext().containsEntity( entityKey ) ) { + if ( entityKey.isBatchLoadable() && !session.getPersistenceContext().containsEntity( entityKey ) ) { session.getPersistenceContext().getBatchFetchQueue().addBatchLoadableEntityKey( entityKey ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/GoofyPersisterClassProvider.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/GoofyPersisterClassProvider.java index f142ec1f6f..ee64cfb2d7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/GoofyPersisterClassProvider.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cfg/persister/GoofyPersisterClassProvider.java @@ -802,5 +802,10 @@ public class GoofyPersisterClassProvider implements PersisterClassResolver { public Object getElementByIndex(Serializable key, Object index, SessionImplementor session, Object owner) { return null; //To change body of implemented methods use File | Settings | File Templates. } + + @Override + public int getBatchSize() { + return 0; + } } }