diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH2286/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH2286/Fixture.cs new file mode 100644 index 00000000000..92e17faa6a5 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH2286/Fixture.cs @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +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().Select(c => c.Id).ToListAsync())).Count; + s.EnableFilter("filterName"); + var countFiltered = (await (s.Query().Select(c => c.Id).ToListAsync())).Count; + + Assert.AreEqual(2, count); + Assert.AreEqual(1, countFiltered); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH2286/Entities.cs b/src/NHibernate.Test/NHSpecificTest/GH2286/Entities.cs new file mode 100644 index 00000000000..29712c0f79b --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH2286/Entities.cs @@ -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 + { + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH2286/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH2286/Fixture.cs new file mode 100644 index 00000000000..12c99d8b4ab --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH2286/Fixture.cs @@ -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().Select(c => c.Id).ToList().Count; + s.EnableFilter("filterName"); + var countFiltered = s.Query().Select(c => c.Id).ToList().Count; + + Assert.AreEqual(2, count); + Assert.AreEqual(1, countFiltered); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH2286/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/GH2286/Mappings.hbm.xml new file mode 100644 index 00000000000..b3e4dcb4db9 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH2286/Mappings.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs b/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs index 843cc2b0cb6..4c429ab928e 100644 --- a/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs +++ b/src/NHibernate/Persister/Entity/AbstractEntityPersister.cs @@ -3704,6 +3704,16 @@ private IDictionary 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; }