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 String entityName;
private final LoadQueryDetails staticLoadQuery;
private final EntityLoadQueryDetails staticLoadQuery;
public AbstractLoadPlanBasedEntityLoader(
OuterJoinLoadable entityPersister,
@ -189,7 +189,7 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
false,
null
);
result = extractEntityResult( results );
result = extractEntityResult( results, id );
}
catch ( SQLException sqle ) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(
@ -208,14 +208,22 @@ public abstract class AbstractLoadPlanBasedEntityLoader extends AbstractLoadPlan
return result;
}
/**
* @deprecated {@link #extractEntityResult(List, Serializable)} should be used instead.
*/
@Deprecated
protected Object extractEntityResult(List results) {
return extractEntityResult( results, null );
}
protected Object extractEntityResult(List results, Serializable id) {
if ( results.size() == 0 ) {
return null;
}
else if ( results.size() == 1 ) {
return results.get( 0 );
}
else {
else if ( staticLoadQuery.hasCollectionInitializers() ) {
final Object row = results.get( 0 );
if ( row.getClass().isArray() ) {
// 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) {

View File

@ -41,7 +41,7 @@ public class BatchingLoadQueryDetailsFactory {
*
* @return The EntityLoadQueryDetails
*/
public LoadQueryDetails makeEntityLoadQueryDetails(
public EntityLoadQueryDetails makeEntityLoadQueryDetails(
LoadPlan loadPlan,
String[] keyColumnNames,
QueryBuildingParameters buildingParameters,
@ -76,7 +76,7 @@ public class BatchingLoadQueryDetailsFactory {
* that add additional joins here)
* @return The EntityLoadQueryDetails
*/
public LoadQueryDetails makeEntityLoadQueryDetails(
public EntityLoadQueryDetails makeEntityLoadQueryDetails(
EntityLoadQueryDetails entityLoadQueryDetailsTemplate,
QueryBuildingParameters buildingParameters) {
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.SessionFactoryImplementor;
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.EntityReferenceInitializerImpl;
import org.hibernate.loader.plan.exec.process.internal.EntityReturnReader;
@ -104,6 +105,11 @@ public class EntityLoadQueryDetails extends AbstractLoadQueryDetails {
generate();
}
public boolean hasCollectionInitializers() {
return CollectionHelper.isNotEmpty( readerCollector.getArrayReferenceInitializers() ) ||
CollectionHelper.isNotEmpty( readerCollector.getNonArrayCollectionReferenceInitializers() );
}
private EntityReturn getRootEntityReturn() {
return (EntityReturn) getRootReturn();
}