Skip to content

Commit 89629bc

Browse files
committed
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
1 parent 30b0c55 commit 89629bc

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

hibernate-core/src/main/java/org/hibernate/query/results/dynamic/DynamicResultBuilderEntityStandard.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.hibernate.query.results.DomainResultCreationStateImpl;
2929
import org.hibernate.query.results.FetchBuilder;
3030
import org.hibernate.query.results.ResultsHelper;
31-
import org.hibernate.query.results.TableGroupImpl;
3231
import org.hibernate.query.results.complete.CompleteFetchBuilder;
3332
import org.hibernate.spi.NavigablePath;
3433
import org.hibernate.sql.ast.spi.FromClauseAccess;
@@ -196,15 +195,17 @@ private <T> T buildResultOrFetch(
196195
final TableGroup tableGroup = fromClauseAccess.resolveTableGroup(
197196
elementNavigablePath,
198197
np -> {
199-
final TableReference tableReference = entityMapping.createPrimaryTableReference(
200-
new SqlAliasBaseConstant( tableAlias ),
201-
creationState
202-
);
203-
204198
if ( lockMode != null ) {
205199
domainResultCreationState.getSqlAstCreationState().registerLockMode( tableAlias, lockMode );
206200
}
207-
return new TableGroupImpl( elementNavigablePath, tableAlias, tableReference, entityMapping );
201+
return entityMapping.createRootTableGroup(
202+
true,
203+
navigablePath,
204+
tableAlias,
205+
new SqlAliasBaseConstant( tableAlias ),
206+
null,
207+
creationState
208+
);
208209
}
209210
);
210211
final TableReference tableReference = tableGroup.getPrimaryTableReference();

hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.hibernate.loader.internal.AliasConstantsHelper;
2626
import org.hibernate.metamodel.mapping.CollectionPart;
2727
import org.hibernate.metamodel.mapping.EntityMappingType;
28+
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
2829
import org.hibernate.persister.collection.CollectionPersister;
2930
import org.hibernate.persister.collection.QueryableCollection;
3031
import org.hibernate.persister.collection.SQLLoadableCollection;
@@ -399,6 +400,17 @@ else if ( propertyType instanceof ComponentType ) {
399400
resultBuilderEntity.addFetchBuilder( propertyName, fetchBuilder );
400401
}
401402
else if ( columnAliases.length != 0 ) {
403+
if ( propertyType instanceof EntityType ) {
404+
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) loadable.findAttributeMapping( propertyName );
405+
if ( !toOne.getIdentifyingColumnsTableExpression().equals( loadable.getMappedTableDetails().getTableName() ) ) {
406+
// The to-one has a join-table, use the plain join column name instead of the alias
407+
assert columnAliases.length == 1;
408+
final String[] targetAliases = new String[1];
409+
targetAliases[0] = toOne.getTargetKeyPropertyName();
410+
resultBuilderEntity.addProperty( propertyName, targetAliases );
411+
return;
412+
}
413+
}
402414
resultBuilderEntity.addProperty( propertyName, columnAliases );
403415
}
404416
}

0 commit comments

Comments
 (0)