Skip to content

NH-2016 - Fix joining to association twice using criteria #287

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 9 commits into from
Nov 25, 2018
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
152 changes: 152 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3472/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3472
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var c = new Cat
{
Age = 6,
Children = new HashSet<Cat>
{
new Cat
{
Age = 4,
Children = new HashSet<Cat>
{
new Cat { Color = "Ginger", Age = 1 },
new Cat { Color = "Black", Age = 3 }
}
}
}
};
s.Save(c);
t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from Cat");
t.Commit();
}
}

[Test]
public async Task CriteriaQueryWithMultipleJoinsToSameAssociationAsync()
{
using (var s = OpenSession())
{
var list =
await (s
.CreateCriteria<Cat>("cat")
.CreateAlias(
"cat.Children",
"gingerCat",
JoinType.LeftOuterJoin,
Restrictions.Eq("Color", "Ginger"))
.CreateAlias(
"cat.Children",
"blackCat",
JoinType.LeftOuterJoin,
Restrictions.Eq("Color", "Black"))
.SetProjection(
Projections.Alias(Projections.Property("gingerCat.Age"), "gingerCatAge"),
Projections.Alias(Projections.Property("blackCat.Age"), "blackCatAge")
).AddOrder(new Order(Projections.Property("Age"), true)).ListAsync<object[]>());
Assert.That(list, Has.Count.EqualTo(4));
Assert.That(list[0], Is.EqualTo(new object[] { null, null }));
Assert.That(list[1], Is.EqualTo(new object[] { null, null }));
Assert.That(list[2], Is.EqualTo(new object[] { 1, 3 }));
Assert.That(list[3], Is.EqualTo(new object[] { null, null }));
}
}

[Test]
public async Task QueryWithFetchesAndAliasDoNotDuplicateJoinAsync()
{
using (var s = OpenSession())
{
Cat parent = null;
using (var spy = new SqlLogSpy())
{
var list =
await (s
.QueryOver<Cat>()
.Fetch(SelectMode.Fetch, o => o.Parent)
.Fetch(SelectMode.Fetch, o => o.Parent.Parent)
.JoinAlias(o => o.Parent, () => parent)
.Where(x => parent.Age == 4)
.ListAsync());

// Two joins to Cat are expected: one for the immediate parent, and a second for the grand-parent.
// So checking if it does not contain three joins or more. (The regex uses "[\s\S]" instead of "."
// because the SQL is formatted by default and contains "\n" which are not matched by ".".)
Assert.That(spy.GetWholeLog(), Does.Not.Match(@"(?:\bjoin\s*Cat\b[\s\S]*){3,}").IgnoreCase);
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(
NHibernateUtil.IsInitialized(list[0].Parent),
Is.True,
"first cat parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[1].Parent),
Is.True,
"second cat parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[0].Parent.Parent),
Is.True,
"first cat parent parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[1].Parent.Parent),
Is.True,
"second cat parent parent initialization status");
}
}
}

[Test, Explicit("Debatable use case")]
public async Task QueryWithFetchesAndMultipleJoinsToSameAssociationAsync()
{
using (var s = OpenSession())
{
Cat ginger = null;
Cat black = null;
var list =
await (s
.QueryOver<Cat>()
.Fetch(SelectMode.Fetch, o => o.Children)
.JoinAlias(o => o.Children, () => ginger)
.Where(x => ginger.Color == "Ginger")
.JoinAlias(o => o.Children, () => black)
.Where(x => black.Color == "Black")
.ListAsync());

Assert.That(list, Has.Count.EqualTo(1));
Assert.That(list[0].Children, Has.Count.EqualTo(2));
}
}
}
}
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3472/Domain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3472
{
public class Cat
{
public virtual int Id { get; set; }

public virtual string Color { get; set; }
public virtual int Age { get; set; }
public virtual Cat Parent { get; set; }

public virtual ISet<Cat> Children { get; set; } = new HashSet<Cat>();
}
}
141 changes: 141 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3472/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3472
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var c = new Cat
{
Age = 6,
Children = new HashSet<Cat>
{
new Cat
{
Age = 4,
Children = new HashSet<Cat>
{
new Cat { Color = "Ginger", Age = 1 },
new Cat { Color = "Black", Age = 3 }
}
}
}
};
s.Save(c);
t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from Cat");
t.Commit();
}
}

[Test]
public void CriteriaQueryWithMultipleJoinsToSameAssociation()
{
using (var s = OpenSession())
{
var list =
s
.CreateCriteria<Cat>("cat")
.CreateAlias(
"cat.Children",
"gingerCat",
JoinType.LeftOuterJoin,
Restrictions.Eq("Color", "Ginger"))
.CreateAlias(
"cat.Children",
"blackCat",
JoinType.LeftOuterJoin,
Restrictions.Eq("Color", "Black"))
.SetProjection(
Projections.Alias(Projections.Property("gingerCat.Age"), "gingerCatAge"),
Projections.Alias(Projections.Property("blackCat.Age"), "blackCatAge")
).AddOrder(new Order(Projections.Property("Age"), true)).List<object[]>();
Assert.That(list, Has.Count.EqualTo(4));
Assert.That(list[0], Is.EqualTo(new object[] { null, null }));
Assert.That(list[1], Is.EqualTo(new object[] { null, null }));
Assert.That(list[2], Is.EqualTo(new object[] { 1, 3 }));
Assert.That(list[3], Is.EqualTo(new object[] { null, null }));
}
}

[Test]
public void QueryWithFetchesAndAliasDoNotDuplicateJoin()
{
using (var s = OpenSession())
{
Cat parent = null;
using (var spy = new SqlLogSpy())
{
var list =
s
.QueryOver<Cat>()
.Fetch(SelectMode.Fetch, o => o.Parent)
.Fetch(SelectMode.Fetch, o => o.Parent.Parent)
.JoinAlias(o => o.Parent, () => parent)
.Where(x => parent.Age == 4)
.List();

// Two joins to Cat are expected: one for the immediate parent, and a second for the grand-parent.
// So checking if it does not contain three joins or more. (The regex uses "[\s\S]" instead of "."
// because the SQL is formatted by default and contains "\n" which are not matched by ".".)
Assert.That(spy.GetWholeLog(), Does.Not.Match(@"(?:\bjoin\s*Cat\b[\s\S]*){3,}").IgnoreCase);
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(
NHibernateUtil.IsInitialized(list[0].Parent),
Is.True,
"first cat parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[1].Parent),
Is.True,
"second cat parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[0].Parent.Parent),
Is.True,
"first cat parent parent initialization status");
Assert.That(
NHibernateUtil.IsInitialized(list[1].Parent.Parent),
Is.True,
"second cat parent parent initialization status");
}
}
}

[Test, Explicit("Debatable use case")]
public void QueryWithFetchesAndMultipleJoinsToSameAssociation()
{
using (var s = OpenSession())
{
Cat ginger = null;
Cat black = null;
var list =
s
.QueryOver<Cat>()
.Fetch(SelectMode.Fetch, o => o.Children)
.JoinAlias(o => o.Children, () => ginger)
.Where(x => ginger.Color == "Ginger")
.JoinAlias(o => o.Children, () => black)
.Where(x => black.Color == "Black")
.List();

Assert.That(list, Has.Count.EqualTo(1));
Assert.That(list[0].Children, Has.Count.EqualTo(2));
}
}
}
}
19 changes: 19 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3472/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3472">

<class name="Cat">
<id name="Id" generator="native"/>

<property name="Color"/>
<property name="Age"/>
<many-to-one name="Parent" column="parentId" insert="false" update="false"/>

<set name="Children" cascade="all">
<key column="parentId"/>
<one-to-many class="Cat"/>
</set>
</class>

</hibernate-mapping>
Loading