Skip to content

Fix missing subclass discriminator in ON clause for entity joins #2600

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 3 commits into from
Nov 13, 2020
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
56 changes: 51 additions & 5 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2463/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


using System.Linq;
using NHibernate.Criterion;
using NHibernate.DomainModel;
using NUnit.Framework;
using NHibernate.Linq;
Expand All @@ -19,23 +20,68 @@ namespace NHibernate.Test.NHSpecificTest.GH2463
[TestFixture]
public class FixtureAsync : TestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return Dialect.SupportsScalarSubSelects;
}

protected override string[] Mappings
{
get { return new[] {"ABC.hbm.xml"}; }
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a = new A {Name = "A", AnotherName = "X"};
session.Save(a);

var b = new B {Name = "B", AnotherName = "X"};
session.Save(b);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

//Also see GH-2599
[Test]
public async Task CanJoinOnEntityWithDiscriminatorAsync()
public async Task CanJoinOnEntityWithDiscriminatorLinqAsync()
{
using (var s = OpenSession())
{
await (s.Query<A>().Join(
s.Query<A>(),
a => a.Id,
b => b.Id,
var list = await (s.Query<A>().Join(
s.Query<B>(),
a => a.AnotherName,
b => b.AnotherName,
(a, b) =>
new {a, b}).ToListAsync());
}
}

[Test]
public async Task CanJoinOnEntityWithDiscriminatorQueryOverAsync()
{
using (var s = OpenSession())
{
A a = null;
B b = null;
var list = await (s.QueryOver<A>(() => a)
.JoinEntityAlias(() => b, () => a.AnotherName == b.AnotherName)
.Select((x) => a.AsEntity(), (x) => b.AsEntity()).ListAsync<object[]>());
}
}
}
}
56 changes: 51 additions & 5 deletions src/NHibernate.Test/NHSpecificTest/GH2463/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using NHibernate.Criterion;
using NHibernate.DomainModel;
using NUnit.Framework;

Expand All @@ -7,23 +8,68 @@ namespace NHibernate.Test.NHSpecificTest.GH2463
[TestFixture]
public class Fixture : TestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return Dialect.SupportsScalarSubSelects;
}

protected override string[] Mappings
{
get { return new[] {"ABC.hbm.xml"}; }
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a = new A {Name = "A", AnotherName = "X"};
session.Save(a);

var b = new B {Name = "B", AnotherName = "X"};
session.Save(b);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

//Also see GH-2599
[Test]
public void CanJoinOnEntityWithDiscriminator()
public void CanJoinOnEntityWithDiscriminatorLinq()
{
using (var s = OpenSession())
{
s.Query<A>().Join(
s.Query<A>(),
a => a.Id,
b => b.Id,
var list = s.Query<A>().Join(
s.Query<B>(),
a => a.AnotherName,
b => b.AnotherName,
(a, b) =>
new {a, b}).ToList();
}
}

[Test]
public void CanJoinOnEntityWithDiscriminatorQueryOver()
{
using (var s = OpenSession())
{
A a = null;
B b = null;
var list = s.QueryOver<A>(() => a)
.JoinEntityAlias(() => b, () => a.AnotherName == b.AnotherName)
.Select((x) => a.AsEntity(), (x) => b.AsEntity()).List<object[]>();
}
}
}
}
6 changes: 4 additions & 2 deletions src/NHibernate/Engine/JoinSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ internal virtual JoinFragment ToJoinFragment(
else
{
// NH Different behavior : NH1179 and NH1293
// Apply filters in Many-To-One association
// Apply filters for entity joins and Many-To-One association
var enabledForManyToOne = FilterHelper.GetEnabledForManyToOne(enabledFilters);
condition = new SqlString(string.IsNullOrEmpty(on) && enabledForManyToOne.Count > 0
condition = new SqlString(string.IsNullOrEmpty(on) && (ForceFilter || enabledForManyToOne.Count > 0)
? join.Joinable.FilterFragment(join.Alias, enabledForManyToOne)
: on);
Comment on lines +196 to 200
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahusoid,
I think we have missed something here, which could have fixed #3046 if done similarly in other places having that ForceFilter: when true, shouldn't it filter on all enabled filters, not just on many-to-one enabled ones ?

					// Apply filters for entity joins and Many-To-One association
					var enabledFiltersForJoin = ForceFilter ? enabledFilters : FilterHelper.GetEnabledForManyToOne(enabledFilters);
					condition = new SqlString(string.IsNullOrEmpty(on) && (enabledFiltersForJoin.Count > 0)
					            	? join.Joinable.FilterFragment(join.Alias, enabledFiltersForJoin)
					            	: on);

But it may not be enough, because the on variable construction does exclude filters not enabled for many-to-one too.

}
Expand Down Expand Up @@ -327,5 +327,7 @@ public interface ISelector
internal string RootAlias => rootAlias;

public ISessionFactoryImplementor Factory => factory;

internal bool ForceFilter { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Tree/EntityJoinFromElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public EntityJoinFromElement(FromClause fromClause, IQueryable entityPersister,
InitializeEntity(fromClause, entityPersister.EntityName, entityPersister, entityType, alias, tableAlias);

//NH Specific: hibernate uses special class EntityJoinJoinSequenceImpl
JoinSequence = new JoinSequence(SessionFactoryHelper.Factory)
JoinSequence = new JoinSequence(SessionFactoryHelper.Factory) {ForceFilter = true}
.AddJoin(entityType, tableAlias, joinType, Array.Empty<string>());

fromClause.Walker.AddQuerySpaces(entityPersister.QuerySpaces);
Expand Down
7 changes: 4 additions & 3 deletions src/NHibernate/Loader/JoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ internal void AddExplicitEntityJoinAssociation(
GetWithClause(path, pathAlias),
Factory,
enabledFilters,
GetSelectMode(path)), path);
GetSelectMode(path)) {ForceFilter = true},
path);
AddAssociation(assoc);
}

Expand Down Expand Up @@ -851,8 +852,8 @@ protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associati
{
oj.AddJoins(outerjoin);
// NH Different behavior : NH1179 and NH1293
// Apply filters in Many-To-One association
if (enabledFiltersForManyToOne.Count > 0)
// Apply filters for entity joins and Many-To-One associations
if (oj.ForceFilter || enabledFiltersForManyToOne.Count > 0)
{
string manyToOneFilterFragment = oj.Joinable.FilterFragment(oj.RHSAlias, enabledFiltersForManyToOne);
Comment on lines +855 to 858
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly:

					// Apply filters for entity joins and Many-To-One associations
					var enabledFiltersForJoin = oj.ForceFilter ? enabledFilters : enabledFiltersForManyToOne;
					if (oj.ForceFilter || enabledFiltersForJoin.Count > 0)
					{
						string manyToOneFilterFragment = oj.Joinable.FilterFragment(oj.RHSAlias, enabledFiltersForJoin);

bool joinClauseDoesNotContainsFilterAlready =
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Loader/OuterJoinableAssociation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public SelectMode SelectMode

public ISet<string> EntityFetchLazyProperties { get; set; }

internal bool ForceFilter { get; set; }

public int GetOwner(IList<OuterJoinableAssociation> associations)
{
if (IsEntityType || IsCollection)
Expand Down