Skip to content

Fixes #3334 #3335

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

Closed
wants to merge 2 commits into from
Closed
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
112 changes: 112 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3334/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//------------------------------------------------------------------------------
// <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 NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3334
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
Sfi.Statistics.IsStatisticsEnabled = true;
}

protected override void OnTearDown()
{
Sfi.Statistics.IsStatisticsEnabled = false;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from ChildEntity").ExecuteUpdate();
session.CreateQuery("delete from GrandChildEntity").ExecuteUpdate();
session.CreateQuery("delete from Entity").ExecuteUpdate();
session.CreateQuery("delete from OtherEntity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task NoExceptionOnExecuteQueryAsync()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var parent = new Entity
{
Name = "Parent1",
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "GrandChild" } } }
};
await (session.SaveAsync(parent));
parent = new Entity
{
Name = "Parent2",
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "XGrandChild" } } }
};
var other = new OtherEntity { Name = "ABC", Entities = {parent}};
parent.OtherEntity = other;
await (session.SaveAsync(parent));
await (session.SaveAsync(other));
await (t.CommitAsync());
}

using (var session = OpenSession())
using (var _ = session.BeginTransaction())
{
var q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
LEFT JOIN child.Child AS grandChild
LEFT JOIN ROOT.OtherEntity AS otherEntity
WHERE
grandChild.Name like 'G%'
OR otherEntity.Name like 'G%'
)");
Assert.AreEqual(1, (await (q.ListAsync())).Count);

q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
LEFT JOIN child.Child AS grandChild
LEFT JOIN ROOT.OtherEntity AS otherEntity
WHERE
grandChild.Name like 'A%'
OR otherEntity.Name like 'A%'
)");
Assert.AreEqual(1, (await (q.ListAsync())).Count);

/* does not work because of inner join or theta join created for many-to-one
q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
WHERE
child.Child.Name like 'G%'
OR ROOT.OtherEntity.Name like 'A%'
)");
Assert.AreEqual(1, q.List().Count);*/
}
}
}
}
32 changes: 32 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3334/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3334
{
public class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ISet<ChildEntity> Children { get; set; } = new HashSet<ChildEntity>();
Copy link
Member

Choose a reason for hiding this comment

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

nit: whitespace

public virtual OtherEntity OtherEntity { get; set; }
}

public class ChildEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual GrandChildEntity Child { get; set; }
}

public class GrandChildEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

public class OtherEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ISet<Entity> Entities { get; set; } = new HashSet<Entity>();
}
}
101 changes: 101 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3334/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3334
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
Sfi.Statistics.IsStatisticsEnabled = true;
}

protected override void OnTearDown()
{
Sfi.Statistics.IsStatisticsEnabled = false;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from ChildEntity").ExecuteUpdate();
session.CreateQuery("delete from GrandChildEntity").ExecuteUpdate();
session.CreateQuery("delete from Entity").ExecuteUpdate();
session.CreateQuery("delete from OtherEntity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void NoExceptionOnExecuteQuery()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var parent = new Entity
{
Name = "Parent1",
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "GrandChild" } } }
};
session.Save(parent);
parent = new Entity
{
Name = "Parent2",
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "XGrandChild" } } }
};
var other = new OtherEntity { Name = "ABC", Entities = {parent}};
parent.OtherEntity = other;
session.Save(parent);
session.Save(other);
t.Commit();
}

using (var session = OpenSession())
using (var _ = session.BeginTransaction())
{
var q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
LEFT JOIN child.Child AS grandChild
LEFT JOIN ROOT.OtherEntity AS otherEntity
WHERE
grandChild.Name like 'G%'
OR otherEntity.Name like 'G%'
)");
Assert.AreEqual(1, q.List().Count);

q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
LEFT JOIN child.Child AS grandChild
LEFT JOIN ROOT.OtherEntity AS otherEntity
WHERE
grandChild.Name like 'A%'
OR otherEntity.Name like 'A%'
)");
Assert.AreEqual(1, q.List().Count);

/* does not work because of inner join or theta join created for many-to-one
q = session.CreateQuery(
@"
SELECT ROOT
FROM Entity AS ROOT
WHERE
EXISTS
(FROM ELEMENTS(ROOT.Children) AS child
WHERE
child.Child.Name like 'G%'
OR ROOT.OtherEntity.Name like 'A%'
)");
Assert.AreEqual(1, q.List().Count);*/
}
}
}
}
35 changes: 35 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3334/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH3334">

<class name="Entity">
<id name="Id" generator="identity" />
<property name="Name"/>
<set name="Children" cascade="persist,delete,save-update,evict,lock,replicate,delete-orphan">
<key column="Parent" />
<one-to-many class="ChildEntity"/>
</set>
<many-to-one name="OtherEntity" class="OtherEntity"/>
</class>

<class name="ChildEntity">
<id name="Id" generator="identity" />
<property name="Name"/>
<many-to-one name="Child" class="GrandChildEntity" cascade="persist,delete,save-update,evict,lock,replicate,delete-orphan"/>
</class>

<class name="GrandChildEntity">
<id name="Id" generator="identity" />
<property name="Name"/>
</class>

<class name="OtherEntity">
<id name="Id" generator="identity" />
<property name="Name"/>
<set name="Entities" inverse="true" lazy="true">
<key column="OtherEntity" />
<one-to-many class="Entity" />
</set>
</class>

</hibernate-mapping>
1 change: 1 addition & 0 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ private void DereferenceEntityJoin(string classAlias, EntityType propertyType, b
associatedEntityName,
tableAlias,
joinSequence,
_joinType,
_fetch,
Walker.IsInFrom,
propertyType
Expand Down
16 changes: 8 additions & 8 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/FromElementFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ public FromElement CreateElementJoin(IQueryableCollection queryableCollection)
return elem;
}

public FromElement CreateEntityJoin(
string entityClass,
string tableAlias,
JoinSequence joinSequence,
bool fetchFlag,
bool inFrom,
EntityType type)
public FromElement CreateEntityJoin(string entityClass,
Copy link
Member

Choose a reason for hiding this comment

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

This would require a method overload

string tableAlias,
JoinSequence joinSequence,
JoinType joinType,
bool fetchFlag,
bool inFrom,
EntityType type)
{
FromElement elem = CreateJoin(entityClass, tableAlias, joinSequence, type, false);
elem.Fetch = fetchFlag;
Expand All @@ -315,7 +315,7 @@ public FromElement CreateEntityJoin(
elem.UseFromFragment = true;
elem.SetImpliedInFromClause(true);
}
if (elem.Walker.IsSubQuery)
if (elem.Walker.IsSubQuery && joinType != JoinType.LeftOuterJoin && joinType != JoinType.RightOuterJoin && joinType != JoinType.FullJoin)
{
// two conditions where we need to transform this to a theta-join syntax:
// 1) 'elem' is the "root from-element" in correlated subqueries
Expand Down