Skip to content

Fix subclass filter on key columns #2288

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 2 commits into from
Dec 22, 2019
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
59 changes: 59 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2286/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//------------------------------------------------------------------------------
// <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.GH2286
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
public async Task FilterOnJoinedSubclassKeyAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var c1 = new IndividualCustomer {Id = 2, Name = "2"};
var c2 = new IndividualCustomer {Id = 4, Name = "4"};
await (session.SaveAsync(c1));
await (session.SaveAsync(c2));

await (session.FlushAsync());
await (transaction.CommitAsync());
}

using (var s = OpenSession())
{
var count = (await (s.Query<IndividualCustomer>().Select(c => c.Id).ToListAsync())).Count;
s.EnableFilter("filterName");
var countFiltered = (await (s.Query<IndividualCustomer>().Select(c => c.Id).ToListAsync())).Count;

Assert.AreEqual(2, count);
Assert.AreEqual(1, countFiltered);
}
}
}
}
18 changes: 18 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2286/Entities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2286
{
class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

class Customer : Entity
{
}

class IndividualCustomer : Customer
{
}
}
47 changes: 47 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2286/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2286
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
public void FilterOnJoinedSubclassKey()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var c1 = new IndividualCustomer {Id = 2, Name = "2"};
var c2 = new IndividualCustomer {Id = 4, Name = "4"};
session.Save(c1);
session.Save(c2);

session.Flush();
transaction.Commit();
}

using (var s = OpenSession())
{
var count = s.Query<IndividualCustomer>().Select(c => c.Id).ToList().Count;
s.EnableFilter("filterName");
var countFiltered = s.Query<IndividualCustomer>().Select(c => c.Id).ToList().Count;

Assert.AreEqual(2, count);
Assert.AreEqual(1, countFiltered);
}
}
}
}
21 changes: 21 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2286/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH2286">

<class name="Entity">
<id name="Id" generator="assigned" />
<property name="Name" />

<joined-subclass name="Customer">
<key column="customerId"/>
</joined-subclass>
</class>

<joined-subclass name="IndividualCustomer" extends="Customer">
<key column="IndividualCustomerID" />
<filter name="filterName" />
</joined-subclass>

<filter-def name="filterName" condition="IndividualCustomerID in (2,3) AND customerId in (2, 3) AND Id in (2,3)">
</filter-def>

</hibernate-mapping>
10 changes: 10 additions & 0 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3704,6 +3704,16 @@ private IDictionary<string, string> GetColumnsToTableAliasMap(string rootAlias)
}
}

//Reversed loop to prefer root columns in case same key names for subclasses
for (int i = SubclassTableSpan - 1; i >= 0; i--)
{
foreach (var key in GetSubclassTableKeyColumns(i))
{
var alias = i == 0 ? rootAlias : GenerateTableAlias(rootAlias, i);
dict[key] = $"{alias}.{key}";
}
}

return dict;
}

Expand Down