Skip to content
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

Fix referencing nullable entity in correlated subquery #3312

Merged
merged 4 commits into from
Jun 11, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private const string NAME_JOE = "Joe";
private const string NAME_ALLEN = "Allen";

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var joe = new Customer { Name = NAME_JOE };
session.Save(joe);

var allen = new Customer { Name = NAME_ALLEN };
session.Save(allen);

var joeInvoice0 = new Invoice { Customer = joe, Number = 0 };
session.Save(joeInvoice0);

var allenInvoice1 = new Invoice { Customer = allen, Number = 1 };
session.Save(allenInvoice1);

tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.Delete("from Invoice");
session.Delete("from Customer");
tx.Commit();
}
}

[Test]
public async Task NullableEntityInCorrelatedSubqueryAsync()
{
using (var s = OpenSession())
{
var customers = s.Query<Customer>().Where(c => c.Name == NAME_JOE);
var results = await (s.Query<Invoice>()
.Where(i => customers.Any(c => c.Invoices.Any(ci => ci.Customer == i.Customer))).ToListAsync());

Assert.That(results.Count, Is.EqualTo(1));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
{
[TestFixture]
public class Fixture : BugTestCase
{
private const string NAME_JOE = "Joe";
private const string NAME_ALLEN = "Allen";

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var joe = new Customer { Name = NAME_JOE };
session.Save(joe);

var allen = new Customer { Name = NAME_ALLEN };
session.Save(allen);

var joeInvoice0 = new Invoice { Customer = joe, Number = 0 };
session.Save(joeInvoice0);

var allenInvoice1 = new Invoice { Customer = allen, Number = 1 };
session.Save(allenInvoice1);

tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.Delete("from Invoice");
session.Delete("from Customer");
tx.Commit();
}
}

[Test]
public void NullableEntityInCorrelatedSubquery()
{
using (var s = OpenSession())
{
var customers = s.Query<Customer>().Where(c => c.Name == NAME_JOE);
var results = s.Query<Invoice>()
.Where(i => customers.Any(c => c.Invoices.Any(ci => ci.Customer == i.Customer))).ToList();

Assert.That(results.Count, Is.EqualTo(1));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery">

<class name="Customer" >
<id name="ID" type="Int32">
<generator class="native" />
</id>
<property name="Name" type="String" />

<set name="Invoices" inverse="true" >
<key column="CustomerID"/>
<one-to-many class="Invoice"/>
</set>
</class>

<class name="Invoice">
<id name="ID" type="Int32">
<generator class="native" />
</id>

<property name="Number" type="Int32" column="`Number`" />
<many-to-one name="Customer" not-found="ignore" column="CustomerID" class="Customer" />
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
{
public class Customer
{
public virtual int ID { get; protected set; }
public virtual ISet<Invoice> Invoices { get; set; }
public virtual string Name { get; set; }
}

public class Invoice
{
public virtual int ID { get; protected set; }
public virtual Customer Customer { get; set; }
public virtual int Number { get; set; }
}
}
2 changes: 1 addition & 1 deletion 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
bool joinIsNeeded;

//For nullable entity comparisons we always need to add join (like not constrained one-to-one or not-found ignore associations)
bool comparisonWithNullableEntity = entityType.IsNullable && Walker.IsComparativeExpressionClause;
bool comparisonWithNullableEntity = entityType.IsNullable && Walker.IsComparativeExpressionClause && !IsCorrelatedSubselect;

if ( IsDotNode( parent ) )
{
Expand Down