Skip to content

Commit e33120c

Browse files
csharper2010bahusoid
authored andcommitted
Test case
1 parent 90d50c0 commit e33120c

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3334
4+
{
5+
public class Entity
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual ISet<ChildEntity> Children { get; set; } = new HashSet<ChildEntity>();
10+
public virtual OtherEntity OtherEntity { get; set; }
11+
}
12+
13+
public class ChildEntity
14+
{
15+
public virtual int Id { get; set; }
16+
public virtual Entity Parent { get; set; }
17+
public virtual string Name { get; set; }
18+
public virtual GrandChildEntity Child { get; set; }
19+
}
20+
21+
public class GrandChildEntity
22+
{
23+
public virtual int Id { get; set; }
24+
public virtual string Name { get; set; }
25+
}
26+
27+
public class OtherEntity
28+
{
29+
public virtual int Id { get; set; }
30+
public virtual string Name { get; set; }
31+
public virtual ISet<Entity> Entities { get; set; } = new HashSet<Entity>();
32+
}
33+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH3334
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
[OneTimeSetUp]
11+
public void OneTimeSetUp()
12+
{
13+
using (var session = OpenSession())
14+
using (var t = session.BeginTransaction())
15+
{
16+
var parent = new Entity
17+
{
18+
Name = "Parent1",
19+
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "GrandChild" } } }
20+
};
21+
session.Save(parent);
22+
parent = new Entity
23+
{
24+
Name = "Parent2",
25+
Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "XGrandChild" } } }
26+
};
27+
var other = new OtherEntity { Name = "ABC", Entities = {parent}};
28+
parent.OtherEntity = other;
29+
session.Save(parent);
30+
session.Save(other);
31+
t.Commit();
32+
}
33+
34+
Sfi.Statistics.IsStatisticsEnabled = true;
35+
}
36+
37+
[OneTimeTearDown]
38+
public void OneTimeTearDown()
39+
{
40+
Sfi.Statistics.IsStatisticsEnabled = false;
41+
42+
using var session = OpenSession();
43+
using var transaction = session.BeginTransaction();
44+
45+
session.CreateQuery("delete from ChildEntity").ExecuteUpdate();
46+
session.CreateQuery("delete from GrandChildEntity").ExecuteUpdate();
47+
session.CreateQuery("delete from Entity").ExecuteUpdate();
48+
session.CreateQuery("delete from OtherEntity").ExecuteUpdate();
49+
50+
transaction.Commit();
51+
}
52+
53+
public class TestCaseItem
54+
{
55+
public string Name { get; }
56+
public string Hql { get; }
57+
public int LineNumber { get; }
58+
59+
public TestCaseItem(string name, string hql, [CallerLineNumber] int lineNumber = 0)
60+
{
61+
Name = name;
62+
Hql = hql;
63+
LineNumber = lineNumber;
64+
}
65+
66+
public override string ToString() => $"{LineNumber:0000}: {Name}";
67+
}
68+
69+
public static IEnumerable<TestCaseItem> GetNoExceptionOnExecuteQueryTestCases()
70+
{
71+
/* does not work because of inner join or theta join created for many-to-one
72+
@"
73+
SELECT ROOT
74+
FROM Entity AS ROOT
75+
WHERE
76+
EXISTS
77+
(FROM ELEMENTS(ROOT.Children) AS child
78+
WHERE
79+
child.Child.Name like 'G%'
80+
OR ROOT.OtherEntity.Name like 'A%'
81+
)");*/
82+
83+
yield return new("Basic Elements case 1 FoundViaGrandChildG", @"
84+
SELECT ROOT
85+
FROM Entity AS ROOT
86+
WHERE
87+
EXISTS
88+
(FROM ELEMENTS(ROOT.Children) AS child
89+
LEFT JOIN child.Child AS grandChild
90+
WHERE
91+
grandChild.Name like 'G%'
92+
)");
93+
yield return new("Basic Elements case 2 FoundViaOtherEntityA", @"
94+
SELECT ROOT
95+
FROM Entity AS ROOT
96+
WHERE
97+
EXISTS
98+
(FROM ELEMENTS(ROOT.OtherEntity) AS otherEntity
99+
WHERE
100+
otherEntity.Name like 'A%'
101+
)");
102+
yield return new("HQL Elements FoundViaGrandChildG", @"
103+
SELECT ROOT
104+
FROM Entity AS ROOT
105+
WHERE
106+
EXISTS
107+
(FROM ELEMENTS(ROOT.Children) AS child
108+
LEFT JOIN child.Child AS grandChild
109+
LEFT JOIN ROOT.OtherEntity AS otherEntity
110+
WHERE
111+
grandChild.Name like 'G%'
112+
OR otherEntity.Name like 'G%'
113+
)");
114+
yield return new("HQL Elements FoundViaOtherEntityA", @"
115+
SELECT ROOT
116+
FROM Entity AS ROOT
117+
WHERE
118+
EXISTS
119+
(FROM ELEMENTS(ROOT.Children) AS child
120+
LEFT JOIN child.Child AS grandChild
121+
LEFT JOIN ROOT.OtherEntity AS otherEntity
122+
WHERE
123+
grandChild.Name like 'A%'
124+
OR otherEntity.Name like 'A%'
125+
)");
126+
yield return new("HQL Entity FoundViaGrandChildG", @"
127+
SELECT ROOT
128+
FROM Entity AS ROOT
129+
WHERE
130+
EXISTS
131+
(FROM ChildEntity AS child
132+
LEFT JOIN child.Child AS grandChild
133+
LEFT JOIN ROOT.OtherEntity AS otherEntity
134+
WHERE
135+
child.Parent = ROOT
136+
AND (
137+
grandChild.Name like 'G%'
138+
OR otherEntity.Name like 'G%'
139+
)
140+
)");
141+
yield return new("HQL Entity FoundViaOtherEntityA", @"
142+
SELECT ROOT
143+
FROM Entity AS ROOT
144+
WHERE
145+
EXISTS
146+
(FROM ChildEntity AS child
147+
LEFT JOIN child.Child AS grandChild
148+
LEFT JOIN ROOT.OtherEntity AS otherEntity
149+
WHERE
150+
child.Parent = ROOT
151+
AND (
152+
grandChild.Name like 'A%'
153+
OR otherEntity.Name like 'A%'
154+
)
155+
)");
156+
}
157+
158+
[Test, TestCaseSource(nameof(GetNoExceptionOnExecuteQueryTestCases))]
159+
public void NoExceptionOnExecuteQuery(TestCaseItem testCase)
160+
{
161+
using var session = OpenSession();
162+
using var _ = session.BeginTransaction();
163+
164+
var q = session.CreateQuery(testCase.Hql);
165+
Assert.That(q.List(), Has.Count.EqualTo(1));
166+
}
167+
168+
protected override bool CheckDatabaseWasCleaned()
169+
{
170+
// same set of objects for each test
171+
return true;
172+
}
173+
}
174+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH3334">
4+
5+
<class name="Entity">
6+
<id name="Id" generator="identity" />
7+
<property name="Name"/>
8+
<set name="Children" cascade="persist,delete,save-update,evict,lock,replicate,delete-orphan">
9+
<key column="Parent" />
10+
<one-to-many class="ChildEntity"/>
11+
</set>
12+
<many-to-one name="OtherEntity" class="OtherEntity"/>
13+
</class>
14+
15+
<class name="ChildEntity">
16+
<id name="Id" generator="identity" />
17+
<many-to-one name="Parent" class="Entity" column="Parent" />
18+
<property name="Name"/>
19+
<many-to-one name="Child" class="GrandChildEntity" cascade="persist,delete,save-update,evict,lock,replicate,delete-orphan"/>
20+
</class>
21+
22+
<class name="GrandChildEntity">
23+
<id name="Id" generator="identity" />
24+
<property name="Name"/>
25+
</class>
26+
27+
<class name="OtherEntity">
28+
<id name="Id" generator="identity" />
29+
<property name="Name"/>
30+
<set name="Entities" inverse="true" lazy="true">
31+
<key column="OtherEntity" />
32+
<one-to-many class="Entity" />
33+
</set>
34+
</class>
35+
36+
</hibernate-mapping>

0 commit comments

Comments
 (0)