Skip to content

Commit c030026

Browse files
authored
Fix referencing nullable entity in correlated subquery (nhibernate#3312)
Fixes nhibernate#3306
1 parent c49a2a9 commit c030026

File tree

5 files changed

+172
-1
lines changed

5 files changed

+172
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NUnit.Framework;
13+
using NHibernate.Linq;
14+
15+
namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
private const string NAME_JOE = "Joe";
22+
private const string NAME_ALLEN = "Allen";
23+
24+
protected override void OnSetUp()
25+
{
26+
using (var session = OpenSession())
27+
using (var tx = session.BeginTransaction())
28+
{
29+
var joe = new Customer { Name = NAME_JOE };
30+
session.Save(joe);
31+
32+
var allen = new Customer { Name = NAME_ALLEN };
33+
session.Save(allen);
34+
35+
var joeInvoice0 = new Invoice { Customer = joe, Number = 0 };
36+
session.Save(joeInvoice0);
37+
38+
var allenInvoice1 = new Invoice { Customer = allen, Number = 1 };
39+
session.Save(allenInvoice1);
40+
41+
tx.Commit();
42+
}
43+
}
44+
45+
protected override void OnTearDown()
46+
{
47+
using (var session = OpenSession())
48+
using (var tx = session.BeginTransaction())
49+
{
50+
session.Delete("from Invoice");
51+
session.Delete("from Customer");
52+
tx.Commit();
53+
}
54+
}
55+
56+
[Test]
57+
public async Task NullableEntityInCorrelatedSubqueryAsync()
58+
{
59+
using (var s = OpenSession())
60+
{
61+
var customers = s.Query<Customer>().Where(c => c.Name == NAME_JOE);
62+
var results = await (s.Query<Invoice>()
63+
.Where(i => customers.Any(c => c.Invoices.Any(ci => ci.Customer == i.Customer))).ToListAsync());
64+
65+
Assert.That(results.Count, Is.EqualTo(1));
66+
}
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
private const string NAME_JOE = "Joe";
10+
private const string NAME_ALLEN = "Allen";
11+
12+
protected override void OnSetUp()
13+
{
14+
using (var session = OpenSession())
15+
using (var tx = session.BeginTransaction())
16+
{
17+
var joe = new Customer { Name = NAME_JOE };
18+
session.Save(joe);
19+
20+
var allen = new Customer { Name = NAME_ALLEN };
21+
session.Save(allen);
22+
23+
var joeInvoice0 = new Invoice { Customer = joe, Number = 0 };
24+
session.Save(joeInvoice0);
25+
26+
var allenInvoice1 = new Invoice { Customer = allen, Number = 1 };
27+
session.Save(allenInvoice1);
28+
29+
tx.Commit();
30+
}
31+
}
32+
33+
protected override void OnTearDown()
34+
{
35+
using (var session = OpenSession())
36+
using (var tx = session.BeginTransaction())
37+
{
38+
session.Delete("from Invoice");
39+
session.Delete("from Customer");
40+
tx.Commit();
41+
}
42+
}
43+
44+
[Test]
45+
public void NullableEntityInCorrelatedSubquery()
46+
{
47+
using (var s = OpenSession())
48+
{
49+
var customers = s.Query<Customer>().Where(c => c.Name == NAME_JOE);
50+
var results = s.Query<Invoice>()
51+
.Where(i => customers.Any(c => c.Invoices.Any(ci => ci.Customer == i.Customer))).ToList();
52+
53+
Assert.That(results.Count, Is.EqualTo(1));
54+
}
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery">
5+
6+
<class name="Customer" >
7+
<id name="ID" type="Int32">
8+
<generator class="native" />
9+
</id>
10+
<property name="Name" type="String" />
11+
12+
<set name="Invoices" inverse="true" >
13+
<key column="CustomerID"/>
14+
<one-to-many class="Invoice"/>
15+
</set>
16+
</class>
17+
18+
<class name="Invoice">
19+
<id name="ID" type="Int32">
20+
<generator class="native" />
21+
</id>
22+
23+
<property name="Number" type="Int32" column="`Number`" />
24+
<many-to-one name="Customer" not-found="ignore" column="CustomerID" class="Customer" />
25+
</class>
26+
27+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3306NullableEntityCorrelatedSubquery
4+
{
5+
public class Customer
6+
{
7+
public virtual int ID { get; protected set; }
8+
public virtual ISet<Invoice> Invoices { get; set; }
9+
public virtual string Name { get; set; }
10+
}
11+
12+
public class Invoice
13+
{
14+
public virtual int ID { get; protected set; }
15+
public virtual Customer Customer { get; set; }
16+
public virtual int Number { get; set; }
17+
}
18+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
389389
bool joinIsNeeded;
390390

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

394394
if ( IsDotNode( parent ) )
395395
{

0 commit comments

Comments
 (0)