Skip to content

Commit 3eb16b0

Browse files
bahusoidfredericDelaporte
authored andcommitted
Fix subclass filter on key columns (#2288)
1 parent 8c7444d commit 3eb16b0

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.GH2286
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
protected override void OnTearDown()
22+
{
23+
using (var session = OpenSession())
24+
using (var transaction = session.BeginTransaction())
25+
{
26+
session.Delete("from System.Object");
27+
28+
session.Flush();
29+
transaction.Commit();
30+
}
31+
}
32+
33+
[Test]
34+
public async Task FilterOnJoinedSubclassKeyAsync()
35+
{
36+
using (var session = OpenSession())
37+
using (var transaction = session.BeginTransaction())
38+
{
39+
var c1 = new IndividualCustomer {Id = 2, Name = "2"};
40+
var c2 = new IndividualCustomer {Id = 4, Name = "4"};
41+
await (session.SaveAsync(c1));
42+
await (session.SaveAsync(c2));
43+
44+
await (session.FlushAsync());
45+
await (transaction.CommitAsync());
46+
}
47+
48+
using (var s = OpenSession())
49+
{
50+
var count = (await (s.Query<IndividualCustomer>().Select(c => c.Id).ToListAsync())).Count;
51+
s.EnableFilter("filterName");
52+
var countFiltered = (await (s.Query<IndividualCustomer>().Select(c => c.Id).ToListAsync())).Count;
53+
54+
Assert.AreEqual(2, count);
55+
Assert.AreEqual(1, countFiltered);
56+
}
57+
}
58+
}
59+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2286
4+
{
5+
class Entity
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
11+
class Customer : Entity
12+
{
13+
}
14+
15+
class IndividualCustomer : Customer
16+
{
17+
}
18+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH2286
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
protected override void OnTearDown()
10+
{
11+
using (var session = OpenSession())
12+
using (var transaction = session.BeginTransaction())
13+
{
14+
session.Delete("from System.Object");
15+
16+
session.Flush();
17+
transaction.Commit();
18+
}
19+
}
20+
21+
[Test]
22+
public void FilterOnJoinedSubclassKey()
23+
{
24+
using (var session = OpenSession())
25+
using (var transaction = session.BeginTransaction())
26+
{
27+
var c1 = new IndividualCustomer {Id = 2, Name = "2"};
28+
var c2 = new IndividualCustomer {Id = 4, Name = "4"};
29+
session.Save(c1);
30+
session.Save(c2);
31+
32+
session.Flush();
33+
transaction.Commit();
34+
}
35+
36+
using (var s = OpenSession())
37+
{
38+
var count = s.Query<IndividualCustomer>().Select(c => c.Id).ToList().Count;
39+
s.EnableFilter("filterName");
40+
var countFiltered = s.Query<IndividualCustomer>().Select(c => c.Id).ToList().Count;
41+
42+
Assert.AreEqual(2, count);
43+
Assert.AreEqual(1, countFiltered);
44+
}
45+
}
46+
}
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH2286">
3+
4+
<class name="Entity">
5+
<id name="Id" generator="assigned" />
6+
<property name="Name" />
7+
8+
<joined-subclass name="Customer">
9+
<key column="customerId"/>
10+
</joined-subclass>
11+
</class>
12+
13+
<joined-subclass name="IndividualCustomer" extends="Customer">
14+
<key column="IndividualCustomerID" />
15+
<filter name="filterName" />
16+
</joined-subclass>
17+
18+
<filter-def name="filterName" condition="IndividualCustomerID in (2,3) AND customerId in (2, 3) AND Id in (2,3)">
19+
</filter-def>
20+
21+
</hibernate-mapping>

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,16 @@ private IDictionary<string, string> GetColumnsToTableAliasMap(string rootAlias)
37043704
}
37053705
}
37063706

3707+
//Reversed loop to prefer root columns in case same key names for subclasses
3708+
for (int i = SubclassTableSpan - 1; i >= 0; i--)
3709+
{
3710+
foreach (var key in GetSubclassTableKeyColumns(i))
3711+
{
3712+
var alias = i == 0 ? rootAlias : GenerateTableAlias(rootAlias, i);
3713+
dict[key] = $"{alias}.{key}";
3714+
}
3715+
}
3716+
37073717
return dict;
37083718
}
37093719

0 commit comments

Comments
 (0)