Skip to content

Commit d1b1daf

Browse files
Improve the fix attempt
1 parent 5bc3083 commit d1b1daf

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,20 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
403403
bool joinIsNeeded;
404404

405405
// For nullable entity comparisons we always need to add join (like not constrained one-to-one or not-found ignore associations).
406-
// For property-ref associations, we also need this unless finding a way in the IdentNode for the other hand of the comparison
407-
// to detect it should yield the property-ref columns instead of the primary key columns.
408-
bool comparisonWithNullableEntityOrThroughPropertyRef = Walker.IsComparativeExpressionClause
409-
&& (entityType.IsNullable || entityType.IsUniqueKeyReference);
406+
var comparisonWithNullableEntity = Walker.IsComparativeExpressionClause && entityType.IsNullable;
407+
// For property-ref association comparison, we also need to join unless finding a way in the node for the other hand of the comparison
408+
// to detect it should yield the property-ref columns instead of the primary key columns. And if the other hand is an association too,
409+
// it may be a reference to the primary key, so we would need to join anyway.
410+
var comparisonThroughPropertyRef = Walker.IsComparativeExpressionClause && !entityType.IsReferenceToPrimaryKey;
410411

411-
if ( IsDotNode( parent ) )
412+
if (IsDotNode(parent))
412413
{
413414
// our parent is another dot node, meaning we are being further dereferenced.
414415
// thus we need to generate a join unless the parent refers to the associated
415416
// entity's PK (because 'our' table would know the FK).
416417
parentAsDotNode = ( DotNode ) parent;
417418
property = parentAsDotNode._propertyName;
418-
joinIsNeeded = generateJoin && ((Walker.IsSelectStatement && comparisonWithNullableEntityOrThroughPropertyRef) || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
419+
joinIsNeeded = generateJoin && ((Walker.IsSelectStatement && comparisonWithNullableEntity) || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
419420
}
420421
else if ( ! Walker.IsSelectStatement )
421422
{
@@ -429,14 +430,15 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
429430
else
430431
{
431432
joinIsNeeded = generateJoin || (Walker.IsInSelect && !Walker.IsInCase) || (Walker.IsInFrom && !Walker.IsComparativeExpressionClause)
432-
|| comparisonWithNullableEntityOrThroughPropertyRef;
433+
|| comparisonWithNullableEntity || comparisonThroughPropertyRef;
433434
}
434435

435436
if ( joinIsNeeded )
436437
{
437-
var forceLeftJoin = comparisonWithNullableEntityOrThroughPropertyRef && !IsCorrelatedSubselect;
438+
// Subselect queries use theta style joins, which cannot be forced to left outer joins.
439+
var forceLeftJoin = comparisonWithNullableEntity && !IsCorrelatedSubselect;
438440
DereferenceEntityJoin(classAlias, entityType, implicitJoin, parent, forceLeftJoin);
439-
if (comparisonWithNullableEntityOrThroughPropertyRef)
441+
if (comparisonWithNullableEntity || comparisonThroughPropertyRef)
440442
{
441443
_columns = FromElement.GetIdentityColumns();
442444
}

0 commit comments

Comments
 (0)