Skip to content

Fix nullable entity projection in hql #2809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@


using System;
using System.Linq;
using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Test.Hql.EntityJoinHqlTestEntities;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.Hql
{
Expand Down Expand Up @@ -287,6 +289,24 @@ public async Task NullableOneToOneFetchQueryIsNotAffected2Async()
}
}

[Test(Description = "GH-2772")]
public async Task NullableEntityProjectionAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var nullableOwner1 = new NullableOwner {Name = "1", ManyToOne = await (session.LoadAsync<OneToOneEntity>(Guid.NewGuid()))};
var nullableOwner2 = new NullableOwner {Name = "2"};
await (session.SaveAsync(nullableOwner1));
await (session.SaveAsync(nullableOwner2));

var fullList = await (session.Query<NullableOwner>().Select(x => new {x.Name, ManyToOneId = (Guid?) x.ManyToOne.Id}).ToListAsync());
var withValidManyToOneList = await (session.Query<NullableOwner>().Where(x => x.ManyToOne != null).Select(x => new {x.Name, ManyToOneId = (Guid?) x.ManyToOne.Id}).ToListAsync());
Assert.That(fullList.Count, Is.EqualTo(2));
Assert.That(withValidManyToOneList.Count, Is.EqualTo(0));
}
}

[Test]
public async Task EntityJoinWithEntityComparisonInWithClausShouldNotAddJoinAsync()
{
Expand Down
19 changes: 19 additions & 0 deletions src/NHibernate.Test/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand Down Expand Up @@ -276,6 +277,24 @@ public void NullableOneToOneFetchQueryIsNotAffected2()
}
}

[Test(Description = "GH-2772")]
public void NullableEntityProjection()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var nullableOwner1 = new NullableOwner {Name = "1", ManyToOne = session.Load<OneToOneEntity>(Guid.NewGuid())};
var nullableOwner2 = new NullableOwner {Name = "2"};
session.Save(nullableOwner1);
session.Save(nullableOwner2);

var fullList = session.Query<NullableOwner>().Select(x => new {x.Name, ManyToOneId = (Guid?) x.ManyToOne.Id}).ToList();
var withValidManyToOneList = session.Query<NullableOwner>().Where(x => x.ManyToOne != null).Select(x => new {x.Name, ManyToOneId = (Guid?) x.ManyToOne.Id}).ToList();
Assert.That(fullList.Count, Is.EqualTo(2));
Assert.That(withValidManyToOneList.Count, Is.EqualTo(0));
}
}

[Test]
public void EntityJoinWithEntityComparisonInWithClausShouldNotAddJoin()
{
Expand Down
5 changes: 2 additions & 3 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
//For nullable entity comparisons we always need to add join (like not constrained one-to-one or not-found ignore associations)
//NOTE: This fix is not fully correct. It doesn't work for comparisons with null (where e.OneToOneProp is null)
// as by default implicit join is generated and to work propelry left join is required (see GH-2611)
bool comparisonWithNullableEntity = false;
bool comparisonWithNullableEntity = entityType.IsNullable && Walker.IsComparativeExpressionClause;

if ( IsDotNode( parent ) )
{
Expand All @@ -398,7 +398,7 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
// entity's PK (because 'our' table would know the FK).
parentAsDotNode = ( DotNode ) parent;
property = parentAsDotNode._propertyName;
joinIsNeeded = generateJoin && ((Walker.IsSelectStatement && entityType.IsNullable) || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
joinIsNeeded = generateJoin && ((Walker.IsSelectStatement && comparisonWithNullableEntity) || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
}
else if ( ! Walker.IsSelectStatement )
{
Expand All @@ -411,7 +411,6 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
}
else
{
comparisonWithNullableEntity = (Walker.IsComparativeExpressionClause && entityType.IsNullable);
joinIsNeeded = generateJoin || (Walker.IsInSelect && !Walker.IsInCase) || (Walker.IsInFrom && !Walker.IsComparativeExpressionClause)
|| comparisonWithNullableEntity;
}
Expand Down