HHH-12802 : Hibernate does not thrown an exception when more than entity is loaded with the same ID

(cherry picked from commit 926ad5a133)
This commit is contained in:
Gail Badner 2018-07-13 14:45:11 -07:00
parent ff18d904e8
commit 3af8b6235a
3 changed files with 33 additions and 6 deletions

View File

@ -49,7 +49,7 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
private final Type uniqueKeyType; private final Type uniqueKeyType;
private final String entityName; private final String entityName;
private final LoadQueryDetails staticLoadQuery; private final EntityLoadQueryDetails staticLoadQuery;
public AbstractLoadPlanBasedEntityLoader( public AbstractLoadPlanBasedEntityLoader(
OuterJoinLoadable entityPersister, OuterJoinLoadable entityPersister,
@ -189,7 +189,7 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
false, false,
null null
); );
result = extractEntityResult( results ); result = extractEntityResult( results, id );
} }
catch ( SQLException sqle ) { catch ( SQLException sqle ) {
throw session.getJdbcServices().getSqlExceptionHelper().convert( throw session.getJdbcServices().getSqlExceptionHelper().convert(
@ -208,14 +208,22 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
return result; return result;
} }
/**
* @deprecated {@link #extractEntityResult(List, Serializable)} should be used instead.
*/
@Deprecated
protected Object extractEntityResult(List results) { protected Object extractEntityResult(List results) {
return extractEntityResult( results, null );
}
protected Object extractEntityResult(List results, Serializable id) {
if ( results.size() == 0 ) { if ( results.size() == 0 ) {
return null; return null;
} }
else if ( results.size() == 1 ) { else if ( results.size() == 1 ) {
return results.get( 0 ); return results.get( 0 );
} }
else { else if ( staticLoadQuery.hasCollectionInitializers() ) {
final Object row = results.get( 0 ); final Object row = results.get( 0 );
if ( row.getClass().isArray() ) { if ( row.getClass().isArray() ) {
// the logical type of the result list is List<Object[]>. See if the contained // the logical type of the result list is List<Object[]>. See if the contained
@ -230,7 +238,20 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
} }
} }
throw new HibernateException( "Unable to interpret given query results in terms of a load-entity query" ); if ( id == null ) {
throw new HibernateException(
"Unable to interpret given query results in terms of a load-entity query for " +
entityName
);
}
else {
throw new HibernateException(
"More than one row with the given identifier was found: " +
id +
", for class: " +
entityName
);
}
} }
protected int[] getNamedParameterLocs(String name) { protected int[] getNamedParameterLocs(String name) {

View File

@ -41,7 +41,7 @@ public class BatchingLoadQueryDetailsFactory {
* *
* @return The EntityLoadQueryDetails * @return The EntityLoadQueryDetails
*/ */
public LoadQueryDetails makeEntityLoadQueryDetails( public EntityLoadQueryDetails makeEntityLoadQueryDetails(
LoadPlan loadPlan, LoadPlan loadPlan,
String[] keyColumnNames, String[] keyColumnNames,
QueryBuildingParameters buildingParameters, QueryBuildingParameters buildingParameters,
@ -76,7 +76,7 @@ public class BatchingLoadQueryDetailsFactory {
* that add additional joins here) * that add additional joins here)
* @return The EntityLoadQueryDetails * @return The EntityLoadQueryDetails
*/ */
public LoadQueryDetails makeEntityLoadQueryDetails( public EntityLoadQueryDetails makeEntityLoadQueryDetails(
EntityLoadQueryDetails entityLoadQueryDetailsTemplate, EntityLoadQueryDetails entityLoadQueryDetailsTemplate,
QueryBuildingParameters buildingParameters) { QueryBuildingParameters buildingParameters) {
return new EntityLoadQueryDetails( return new EntityLoadQueryDetails(

View File

@ -14,6 +14,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.loader.plan.exec.process.internal.AbstractRowReader; import org.hibernate.loader.plan.exec.process.internal.AbstractRowReader;
import org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl; import org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl;
import org.hibernate.loader.plan.exec.process.internal.EntityReturnReader; import org.hibernate.loader.plan.exec.process.internal.EntityReturnReader;
@ -104,6 +105,11 @@ public class EntityLoadQueryDetails extends AbstractLoadQueryDetails {
generate(); generate();
} }
public boolean hasCollectionInitializers() {
return CollectionHelper.isNotEmpty( readerCollector.getArrayReferenceInitializers() ) ||
CollectionHelper.isNotEmpty( readerCollector.getNonArrayCollectionReferenceInitializers() );
}
private EntityReturn getRootEntityReturn() { private EntityReturn getRootEntityReturn() {
return (EntityReturn) getRootReturn(); return (EntityReturn) getRootReturn();
} }