From 838fcf9bae78beb01c3df559b099ff07563f544d Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 8 Jun 2009 15:55:03 +0000 Subject: [PATCH] HHH-2980 - Error "org.hibernate.HibernateException: cannot simultaneously fetch multiple bags" not specific enough git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16717 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/loader/BasicLoader.java | 19 +++++-- .../loader/MultipleBagFetchException.java | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/org/hibernate/loader/MultipleBagFetchException.java diff --git a/core/src/main/java/org/hibernate/loader/BasicLoader.java b/core/src/main/java/org/hibernate/loader/BasicLoader.java index 426bfea1e1..2d196046f6 100644 --- a/core/src/main/java/org/hibernate/loader/BasicLoader.java +++ b/core/src/main/java/org/hibernate/loader/BasicLoader.java @@ -24,11 +24,13 @@ */ package org.hibernate.loader; +import java.util.Set; +import java.util.HashSet; + import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.persister.entity.Loadable; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.type.BagType; -import org.hibernate.HibernateException; /** * Uses the default mapping from property to result set column @@ -68,13 +70,16 @@ public abstract class BasicLoader extends Loader { } CollectionPersister[] collectionPersisters = getCollectionPersisters(); - int bagCount = 0; + Set bagRoles = null; if ( collectionPersisters != null ) { String[] collectionSuffixes = getCollectionSuffixes(); collectionDescriptors = new CollectionAliases[collectionPersisters.length]; for ( int i = 0; i < collectionPersisters.length; i++ ) { if ( isBag( collectionPersisters[i] ) ) { - bagCount++; + if ( bagRoles == null ) { + bagRoles = new HashSet(); + } + bagRoles.add( collectionPersisters[i].getRole() ); } collectionDescriptors[i] = new GeneratedCollectionAliases( collectionPersisters[i], @@ -85,8 +90,8 @@ public abstract class BasicLoader extends Loader { else { collectionDescriptors = null; } - if ( bagCount > 1 ) { - throw new HibernateException( "cannot simultaneously fetch multiple bags" ); + if ( bagRoles != null && bagRoles.size() > 1 ) { + throw new MultipleBagFetchException( bagRoles ); } } @@ -98,6 +103,10 @@ public abstract class BasicLoader extends Loader { * Utility method that generates 0_, 1_ suffixes. Subclasses don't * necessarily need to use this algorithm, but it is intended that * they will in most cases. + * + * @param length The number of suffixes to generate + * + * @return The array of generated suffixes (with length=length). */ public static String[] generateSuffixes(int length) { return generateSuffixes( 0, length ); diff --git a/core/src/main/java/org/hibernate/loader/MultipleBagFetchException.java b/core/src/main/java/org/hibernate/loader/MultipleBagFetchException.java new file mode 100644 index 0000000000..ba191a7215 --- /dev/null +++ b/core/src/main/java/org/hibernate/loader/MultipleBagFetchException.java @@ -0,0 +1,52 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.loader; + +import java.util.Set; + +import org.hibernate.HibernateException; + +/** + * Exception used to indicate that a query is attempting to simultaneously fetch multiple + * {@link org.hibernate.type.BagType bags} +* +* @author Steve Ebersole +*/ +public class MultipleBagFetchException extends HibernateException { + private final Set bagRoles; + + public MultipleBagFetchException(Set bagRoles) { + super( "cannot simultaneously fetch multiple bags" ); + this.bagRoles = bagRoles; + } + + /** + * Retrieves the set of collection roles for the bags encountered. + * + * @return The bag collection roles. + */ + public Set getBagRoles() { + return bagRoles; + } +}