Skip to content

Commit

Permalink
HHH-18494 Always use standard table group in result builder entity
Browse files Browse the repository at this point in the history
Also ignore placeholder aliases for to-one properties using join-tables, and use the target column name instead
  • Loading branch information
mbladel committed Oct 25, 2024
1 parent 30b0c55 commit 89629bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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;
Expand Down Expand Up @@ -196,15 +195,17 @@ private <T> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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.collection.QueryableCollection;
import org.hibernate.persister.collection.SQLLoadableCollection;
Expand Down Expand Up @@ -399,6 +400,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.getMappedTableDetails().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 );
}
}
Expand Down

0 comments on commit 89629bc

Please sign in to comment.