From 5b24b171da69b403971eba6878fabad0e96fe1fa Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 17 Aug 2021 19:10:15 +0200 Subject: [PATCH] Resolve collection key selection expressions for delayed collection fetches of dynamic fetch builders with the suffixed column alias of the referenced property name --- .../dynamic/DynamicFetchBuilderStandard.java | 19 +++++++++--- .../internal/ResultSetMappingProcessor.java | 30 +++++++++++++------ .../test/propertyref/basic/Account.java | 2 +- .../test/propertyref/basic/Address.java | 2 +- .../test/propertyref/basic/Group.java | 2 +- .../test/propertyref/basic/Person.hbm.xml | 2 +- .../test/propertyref/basic/Person.java | 2 +- .../propertyref/basic/PropertyRefTest.java | 4 +-- 8 files changed, 43 insertions(+), 20 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/Account.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/Address.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/Group.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/Person.hbm.xml (97%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/Person.java (97%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/propertyref/basic/PropertyRefTest.java (98%) diff --git a/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicFetchBuilderStandard.java b/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicFetchBuilderStandard.java index 0d27b1a4d3..acbb4663c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicFetchBuilderStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicFetchBuilderStandard.java @@ -12,6 +12,7 @@ import java.util.function.BiFunction; import org.hibernate.engine.FetchTiming; import org.hibernate.metamodel.mapping.BasicValuedMapping; +import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.SelectableConsumer; import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping; import org.hibernate.query.NativeQuery; @@ -96,10 +97,7 @@ public class DynamicFetchBuilderStandard creationStateImpl ); } - else { - // Not sure if this fetch builder can also be used with other attribute mappings - assert attributeMapping instanceof ToOneAttributeMapping; - + else if ( attributeMapping instanceof ToOneAttributeMapping ) { final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping; toOneAttributeMapping.getForeignKeyDescriptor().visitKeySelectables( selectableConsumer ); return parent.generateFetchableFetch( @@ -111,6 +109,19 @@ public class DynamicFetchBuilderStandard creationStateImpl ); } + else { + assert attributeMapping instanceof PluralAttributeMapping; + final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping; + pluralAttributeMapping.getKeyDescriptor().visitTargetSelectables( selectableConsumer ); + return parent.generateFetchableFetch( + attributeMapping, + fetchPath, + FetchTiming.DELAYED, + false, + null, + creationStateImpl + ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java index 6581529dbc..ccaadda318 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java @@ -39,6 +39,7 @@ import org.hibernate.query.results.ResultSetMappingImpl; import org.hibernate.query.results.complete.CompleteResultBuilderCollectionStandard; import org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy; import org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard; +import org.hibernate.type.CollectionType; import org.hibernate.type.EntityType; import org.hibernate.type.Type; @@ -319,15 +320,26 @@ public class ResultSetMappingProcessor implements SQLQueryParser.ParserContext { } for ( String propertyName : loadable.getPropertyNames() ) { - final String[] columnAliases = loadable.getSubclassPropertyColumnAliases( - propertyName, - suffix - ); - if ( columnAliases.length != 0 ) { - resultBuilderEntity.addProperty( - propertyName, - columnAliases - ); + final String[] columnAliases = loadable.getSubclassPropertyColumnAliases( propertyName, suffix ); + if ( columnAliases.length == 0 ) { + final Type propertyType = loadable.getPropertyType( propertyName ); + if ( propertyType instanceof CollectionType ) { + final CollectionType collectionType = (CollectionType) propertyType; + final String[] keyColumnAliases; + if ( collectionType.useLHSPrimaryKey() ) { + keyColumnAliases = identifierAliases; + } + else { + keyColumnAliases = loadable.getSubclassPropertyColumnAliases( + collectionType.getLHSPropertyName(), + suffix + ); + } + resultBuilderEntity.addProperty( propertyName, keyColumnAliases ); + } + } + else { + resultBuilderEntity.addProperty( propertyName, columnAliases ); } } return resultBuilderEntity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Account.java b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Account.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Account.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Account.java index 0adbf1152b..5a1b4346ea 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Account.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Account.java @@ -6,7 +6,7 @@ */ //$Id: Account.java 4399 2004-08-21 08:43:20Z oneovthafew $ -package org.hibernate.test.propertyref.basic; +package org.hibernate.orm.test.propertyref.basic; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Address.java b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Address.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Address.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Address.java index 4839dab85d..469b053963 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Address.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Address.java @@ -6,7 +6,7 @@ */ //$Id: Address.java 4390 2004-08-20 07:54:48Z oneovthafew $ -package org.hibernate.test.propertyref.basic; +package org.hibernate.orm.test.propertyref.basic; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Group.java b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Group.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Group.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Group.java index a2ab51baf3..1f1385cab3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Group.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Group.java @@ -6,7 +6,7 @@ */ //$Id: Group.java 7589 2005-07-21 01:56:42Z oneovthafew $ -package org.hibernate.test.propertyref.basic; +package org.hibernate.orm.test.propertyref.basic; import java.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.hbm.xml similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.hbm.xml index e7de69946a..4dd7b392f4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.hbm.xml @@ -24,7 +24,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.java index 4ba1a6b07b..57b9f20796 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/Person.java @@ -6,7 +6,7 @@ */ //$Id: Person.java 10396 2006-09-01 08:48:02 -0500 (Fri, 01 Sep 2006) steve.ebersole@jboss.com $ -package org.hibernate.test.propertyref.basic; +package org.hibernate.orm.test.propertyref.basic; import java.util.ArrayList; import java.util.HashSet; import java.util.List; diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/PropertyRefTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/PropertyRefTest.java index 5b91485870..5890f875ef 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/propertyref/basic/PropertyRefTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.propertyref.basic; +package org.hibernate.orm.test.propertyref.basic; import java.util.Iterator; import java.util.List; @@ -37,7 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; * @author Gavin King */ @DomainModel( - xmlMappings = "org/hibernate/test/propertyref/basic/Person.hbm.xml" + xmlMappings = "org/hibernate/orm/test/propertyref/basic/Person.hbm.xml" ) @SessionFactory(