Skip to content

Commit ddf9362

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 92103ff commit ddf9362

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.hibernate.query.results.DomainResultCreationStateImpl;
2727
import org.hibernate.query.results.FetchBuilder;
2828
import org.hibernate.query.results.ResultsHelper;
29-
import org.hibernate.query.results.TableGroupImpl;
3029
import org.hibernate.query.results.complete.CompleteFetchBuilder;
3130
import org.hibernate.spi.NavigablePath;
3231
import org.hibernate.sql.ast.spi.FromClauseAccess;
@@ -194,15 +193,17 @@ private <T> T buildResultOrFetch(
194193
final TableGroup tableGroup = fromClauseAccess.resolveTableGroup(
195194
elementNavigablePath,
196195
np -> {
197-
final TableReference tableReference = entityMapping.createPrimaryTableReference(
198-
new SqlAliasBaseConstant( tableAlias ),
199-
creationState
200-
);
201-
202196
if ( lockMode != null ) {
203197
domainResultCreationState.getSqlAstCreationState().registerLockMode( tableAlias, lockMode );
204198
}
205-
return new TableGroupImpl( elementNavigablePath, tableAlias, tableReference, entityMapping );
199+
return entityMapping.createRootTableGroup(
200+
true,
201+
navigablePath,
202+
tableAlias,
203+
new SqlAliasBaseConstant( tableAlias ),
204+
null,
205+
creationState
206+
);
206207
}
207208
);
208209
final TableReference tableReference = tableGroup.getPrimaryTableReference();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hibernate.loader.internal.AliasConstantsHelper;
2424
import org.hibernate.metamodel.mapping.CollectionPart;
2525
import org.hibernate.metamodel.mapping.EntityMappingType;
26+
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
2627
import org.hibernate.persister.collection.CollectionPersister;
2728
import org.hibernate.persister.entity.EntityPersister;
2829
import org.hibernate.query.NativeQuery;
@@ -393,6 +394,17 @@ else if ( propertyType instanceof ComponentType ) {
393394
resultBuilderEntity.addFetchBuilder( propertyName, fetchBuilder );
394395
}
395396
else if ( columnAliases.length != 0 ) {
397+
if ( propertyType instanceof EntityType ) {
398+
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) loadable.findAttributeMapping( propertyName );
399+
if ( !toOne.getIdentifyingColumnsTableExpression().equals( loadable.getTableName() ) ) {
400+
// The to-one has a join-table, use the plain join column name instead of the alias
401+
assert columnAliases.length == 1;
402+
final String[] targetAliases = new String[1];
403+
targetAliases[0] = toOne.getTargetKeyPropertyName();
404+
resultBuilderEntity.addProperty( propertyName, targetAliases );
405+
return;
406+
}
407+
}
396408
resultBuilderEntity.addProperty( propertyName, columnAliases );
397409
}
398410
}

0 commit comments

Comments
 (0)