Fix issue with SimpleForeignKeyDescriptor#generateJoinPredicate() using wrong columns

This commit is contained in:
Andrea Boriero 2020-02-27 10:56:25 +00:00
parent 1fea05742c
commit 2e285f891d
1 changed files with 20 additions and 50 deletions

View File

@ -130,8 +130,10 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
SqlAstJoinType sqlAstJoinType, SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver, SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
if ( lhs.getTableExpression().equals( keyColumnContainingTable ) ) { final String rhsTableExpression = rhs.getTableExpression();
assert rhs.getTableExpression().equals( targetColumnContainingTable ); final String lhsTableExpression = lhs.getTableExpression();
if ( lhsTableExpression.equals( keyColumnContainingTable ) ) {
assert rhsTableExpression.equals( targetColumnContainingTable );
return new ComparisonPredicate( return new ComparisonPredicate(
new ColumnReference( new ColumnReference(
lhs, lhs,
@ -149,7 +151,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
); );
} }
else { else {
assert rhs.getTableExpression().equals( keyColumnContainingTable ); assert rhsTableExpression.equals( keyColumnContainingTable );
return new ComparisonPredicate( return new ComparisonPredicate(
new ColumnReference( new ColumnReference(
lhs, lhs,
@ -175,65 +177,33 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
SqlAstJoinType sqlAstJoinType, SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver, SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
final TableReference keyTableReference = getTableReference( lhs, tableGroup, keyColumnContainingTable ); TableReference lhsTableReference;
ColumnReference keyColumnReference; TableReference rhsTableKeyReference;
keyColumnReference = new ColumnReference( if ( targetColumnContainingTable.equals( keyColumnContainingTable ) ) {
keyTableReference, lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keyColumnContainingTable );
keyColumnExpression,
jdbcMapping,
creationContext.getSessionFactory()
);
ColumnReference targetColumnReference; rhsTableKeyReference = getTableReference(
if ( targetColumnContainingTable.equals( keyColumnContainingTable ) ) {
final TableReference targetTableKeyReference = getTableReferenceWhenTargetEqualsKey(
lhs, lhs,
tableGroup, tableGroup,
targetColumnContainingTable targetColumnContainingTable
); );
targetColumnReference = (ColumnReference) sqlExpressionResolver.resolveSqlExpression(
SqlExpressionResolver.createColumnReferenceKey(
targetTableKeyReference,
targetColumnExpression
),
s -> new ColumnReference(
targetTableKeyReference.getIdentificationVariable(),
targetColumnExpression,
jdbcMapping,
creationContext.getSessionFactory()
)
);
} }
else { else {
final TableReference targetTableKeyReference = getTableReference( lhsTableReference = getTableReference( lhs, tableGroup, keyColumnContainingTable );
rhsTableKeyReference = getTableReference(
lhs, lhs,
tableGroup, tableGroup,
targetColumnContainingTable targetColumnContainingTable
); );
targetColumnReference = (ColumnReference) sqlExpressionResolver.resolveSqlExpression(
SqlExpressionResolver.createColumnReferenceKey(
targetTableKeyReference,
targetColumnExpression
),
s -> new ColumnReference(
targetTableKeyReference.getIdentificationVariable(),
targetColumnExpression,
jdbcMapping,
creationContext.getSessionFactory()
)
);
} }
if ( fKeyDirection == ForeignKeyDirection.FROM_PARENT ) {
return new ComparisonPredicate( return generateJoinPredicate(
targetColumnReference, lhsTableReference,
ComparisonOperator.EQUAL, rhsTableKeyReference,
keyColumnReference sqlAstJoinType,
); sqlExpressionResolver,
} creationContext
return new ComparisonPredicate(
keyColumnReference,
ComparisonOperator.EQUAL,
targetColumnReference
); );
} }