From d6ab2fd110f0f2b3133f2a552ffb991eac522f74 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 27 Aug 2024 10:58:40 +0200 Subject: [PATCH] HHH-18511 ArrayIndexOutOfBoundsException in ImmutableFetchList --- .../internal/CompoundNaturalIdMapping.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/CompoundNaturalIdMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/CompoundNaturalIdMapping.java index 37c931e949..0e942286af 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/CompoundNaturalIdMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/CompoundNaturalIdMapping.java @@ -61,6 +61,11 @@ public class CompoundNaturalIdMapping extends AbstractNaturalIdMapping implement private final List attributes; private List jdbcMappings; + /* + This value is used to determine the size of the array used to create the ImmutableFetchList (see org.hibernate.sql.results.graph.internal.ImmutableFetchList#Builder) + The Fetch is inserted into the array at a position corresponding to its Fetchable key value. + */ + private final int maxFetchableKeyIndex; public CompoundNaturalIdMapping( EntityMappingType declaringType, @@ -69,6 +74,14 @@ public class CompoundNaturalIdMapping extends AbstractNaturalIdMapping implement super( declaringType, isMutable( attributes ) ); this.attributes = attributes; + int maxIndex = 0; + for ( SingularAttributeMapping attribute : attributes ) { + if ( attribute.getFetchableKey() > maxIndex ) { + maxIndex = attribute.getFetchableKey(); + } + } + this.maxFetchableKeyIndex = maxIndex + 1; + creationProcess.registerInitializationCallback( "Determine compound natural-id JDBC mappings ( " + declaringType.getEntityName() + ")", () -> { @@ -531,6 +544,10 @@ public class CompoundNaturalIdMapping extends AbstractNaturalIdMapping implement attributes.forEach( consumer ); } + @Override + public int getNumberOfFetchableKeys() { + return maxFetchableKeyIndex; + } public static class DomainResultImpl implements DomainResult, FetchParent { private final NavigablePath navigablePath;