From ddf936286f5207f3a97b0b8de5c696dfea39a891 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 4 Sep 2024 11:38:41 +0200 Subject: [PATCH] HHH-18494 Always use standard table group in result builder entity Also ignore placeholder aliases for to-one properties using join-tables, and use the target column name instead --- .../DynamicResultBuilderEntityStandard.java | 15 ++++++++------- .../sql/internal/ResultSetMappingProcessor.java | 12 ++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicResultBuilderEntityStandard.java b/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicResultBuilderEntityStandard.java index 6c647b9911b6..f9a192c49eab 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicResultBuilderEntityStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicResultBuilderEntityStandard.java @@ -26,7 +26,6 @@ import org.hibernate.query.results.DomainResultCreationStateImpl; import org.hibernate.query.results.FetchBuilder; import org.hibernate.query.results.ResultsHelper; -import org.hibernate.query.results.TableGroupImpl; import org.hibernate.query.results.complete.CompleteFetchBuilder; import org.hibernate.spi.NavigablePath; import org.hibernate.sql.ast.spi.FromClauseAccess; @@ -194,15 +193,17 @@ private T buildResultOrFetch( final TableGroup tableGroup = fromClauseAccess.resolveTableGroup( elementNavigablePath, np -> { - final TableReference tableReference = entityMapping.createPrimaryTableReference( - new SqlAliasBaseConstant( tableAlias ), - creationState - ); - if ( lockMode != null ) { domainResultCreationState.getSqlAstCreationState().registerLockMode( tableAlias, lockMode ); } - return new TableGroupImpl( elementNavigablePath, tableAlias, tableReference, entityMapping ); + return entityMapping.createRootTableGroup( + true, + navigablePath, + tableAlias, + new SqlAliasBaseConstant( tableAlias ), + null, + creationState + ); } ); final TableReference tableReference = tableGroup.getPrimaryTableReference(); 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 07497b9070a1..fc3f76a0a2a9 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 @@ -23,6 +23,7 @@ import org.hibernate.loader.internal.AliasConstantsHelper; import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.EntityMappingType; +import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.query.NativeQuery; @@ -393,6 +394,17 @@ else if ( propertyType instanceof ComponentType ) { resultBuilderEntity.addFetchBuilder( propertyName, fetchBuilder ); } else if ( columnAliases.length != 0 ) { + if ( propertyType instanceof EntityType ) { + final ToOneAttributeMapping toOne = (ToOneAttributeMapping) loadable.findAttributeMapping( propertyName ); + if ( !toOne.getIdentifyingColumnsTableExpression().equals( loadable.getTableName() ) ) { + // The to-one has a join-table, use the plain join column name instead of the alias + assert columnAliases.length == 1; + final String[] targetAliases = new String[1]; + targetAliases[0] = toOne.getTargetKeyPropertyName(); + resultBuilderEntity.addProperty( propertyName, targetAliases ); + return; + } + } resultBuilderEntity.addProperty( propertyName, columnAliases ); } }