Fix issue with SimpleForeignKeyDescriptor DomainResult creation

This commit is contained in:
Andrea Boriero 2020-02-28 11:51:45 +00:00
parent 681c3fa9cf
commit 52b126d4cf
4 changed files with 56 additions and 3 deletions

View File

@ -25,7 +25,15 @@ public interface ForeignKeyDescriptor extends VirtualModelPart {
ForeignKeyDirection getDirection(); ForeignKeyDirection getDirection();
DomainResult createDomainResult(NavigablePath collectionPath, TableGroup tableGroup, DomainResultCreationState creationState); DomainResult createCollectionFecthDomainResult(
NavigablePath collectionPath,
TableGroup tableGroup,
DomainResultCreationState creationState);
DomainResult createDomainResult(
NavigablePath collectionPath,
TableGroup tableGroup,
DomainResultCreationState creationState);
Predicate generateJoinPredicate( Predicate generateJoinPredicate(
TableGroup lhs, TableGroup lhs,

View File

@ -87,6 +87,46 @@ public ForeignKeyDirection getDirection() {
return fKeyDirection; return fKeyDirection;
} }
@Override
public DomainResult createCollectionFecthDomainResult(
NavigablePath collectionPath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
if ( targetColumnContainingTable.equals( keyColumnContainingTable ) ) {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final TableReference tableReference = tableGroup.resolveTableReference( keyColumnContainingTable );
final String identificationVariable = tableReference.getIdentificationVariable();
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(
sqlExpressionResolver.resolveSqlExpression(
SqlExpressionResolver.createColumnReferenceKey(
tableReference,
targetColumnExpression
),
s -> {
return new ColumnReference(
identificationVariable,
targetColumnExpression,
jdbcMapping,
creationState.getSqlAstCreationState().getCreationContext().getSessionFactory()
);
}
),
jdbcMapping.getJavaTypeDescriptor(),
sqlAstCreationState.getCreationContext().getDomainModel().getTypeConfiguration()
);
//noinspection unchecked
return new BasicResult(
sqlSelection.getValuesArrayPosition(),
null,
jdbcMapping.getJavaTypeDescriptor()
);
}else {
return createDomainResult( collectionPath, tableGroup, creationState );
}
}
@Override @Override
public DomainResult createDomainResult( public DomainResult createDomainResult(
NavigablePath collectionPath, NavigablePath collectionPath,

View File

@ -177,6 +177,11 @@ else if ( keyCollectionValue != null ) {
LoggingHelper.toLoggableString( getNavigablePath(), this.collectionKey.getKey() ) LoggingHelper.toLoggableString( getNavigablePath(), this.collectionKey.getKey() )
); );
} }
}else {
this.collectionKey = new CollectionKey(
collectionAttributeMapping.getCollectionDescriptor(),
parentAccess.getParentKey()
);
} }
} }

View File

@ -61,13 +61,13 @@ public EagerCollectionFetch(
final ForeignKeyDescriptor keyDescriptor = fetchedAttribute.getKeyDescriptor(); final ForeignKeyDescriptor keyDescriptor = fetchedAttribute.getKeyDescriptor();
if ( parentTableGroup != null ) { if ( parentTableGroup != null ) {
// join fetch // join fetch
keyContainerResult = keyDescriptor.createDomainResult( fetchedPath, parentTableGroup, creationState ); keyContainerResult = keyDescriptor.createCollectionFecthDomainResult( fetchedPath, parentTableGroup, creationState );
keyCollectionResult = keyDescriptor.createDomainResult( fetchedPath, collectionTableGroup, creationState ); keyCollectionResult = keyDescriptor.createDomainResult( fetchedPath, collectionTableGroup, creationState );
} }
else { else {
// select fetch // select fetch
// todo (6.0) : we could potentially leverage batch fetching for performance // todo (6.0) : we could potentially leverage batch fetching for performance
keyContainerResult = keyDescriptor.createDomainResult( fetchedPath, collectionTableGroup, creationState ); keyContainerResult = keyDescriptor.createCollectionFecthDomainResult( fetchedPath, collectionTableGroup, creationState );
// use null for `keyCollectionResult`... the initializer will see that as trigger to use // use null for `keyCollectionResult`... the initializer will see that as trigger to use
// the assembled container-key value as the collection-key value. // the assembled container-key value as the collection-key value.