Skip to content

Commit b81e422

Browse files
hailtondecastrofredericDelaportemaca88
authored
Fix custom sql loader with composite id (#452)
Co-authored-by: Frédéric Delaporte <[email protected]> Co-authored-by: maca88 <[email protected]>
1 parent 3d92bf4 commit b81e422

File tree

14 files changed

+856
-9
lines changed

14 files changed

+856
-9
lines changed

src/AsyncGenerator.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
- conversion: Ignore
111111
name: GetEnumerator
112112
containingTypeName: IFutureEnumerable
113+
# TODO 6.0: Consider if ComputeFlattenedParameters should remain ignored or not
114+
- conversion: Ignore
115+
name: ComputeFlattenedParameters
116+
containingTypeName: SqlQueryImpl
113117
- conversion: ToAsync
114118
name: ExecuteReader
115119
containingTypeName: IBatcher
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.NH3079
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
// Disable second level cache
22+
protected override string CacheConcurrencyStrategy => null;
23+
24+
protected override void OnTearDown()
25+
{
26+
using (var s = OpenSession())
27+
using (var t = s.BeginTransaction())
28+
{
29+
s.CreateQuery("delete from Employment").ExecuteUpdate();
30+
31+
s.CreateQuery("delete from System.Object").ExecuteUpdate();
32+
33+
t.Commit();
34+
}
35+
}
36+
37+
protected override void OnSetUp()
38+
{
39+
using (var s = OpenSession())
40+
using (var t = s.BeginTransaction())
41+
{
42+
var personList = new List<Person>();
43+
var employerList = new List<Employer>();
44+
45+
// Global id to avoid false positive assertion with positional parameter
46+
var gId = 1;
47+
48+
for (var i = 0; i < 3; i++)
49+
{
50+
var personCpId = new PersonCpId { IdA = gId++, IdB = gId++ };
51+
var personObj = new Person
52+
{ CpId = personCpId, Name = "PERSON_" + personCpId.IdA + "_" + personCpId.IdB };
53+
s.Save(personObj);
54+
personList.Add(personObj);
55+
}
56+
57+
for (var i = 0; i < 3; i++)
58+
{
59+
var employerCpId = new EmployerCpId { IdA = gId++, IdB = gId++ };
60+
var employerObj = new Employer
61+
{ CpId = employerCpId, Name = "EMPLOYER_" + employerCpId.IdA + "_" + employerCpId.IdB };
62+
s.Save(employerObj);
63+
employerList.Add(employerObj);
64+
}
65+
66+
var employmentIds = new[]
67+
{
68+
gId++,
69+
gId++,
70+
gId++,
71+
gId++,
72+
};
73+
var employmentNames = new[]
74+
{
75+
//P1 + E1
76+
"EMPLOYMENT_" + employmentIds[0] + "_" +
77+
personList[0].CpId.IdA + "_" + personList[0].CpId.IdA + "_" +
78+
employerList[0].CpId.IdA + "_" + employerList[0].CpId.IdB,
79+
//P1 + E2
80+
"EMPLOYMENT_" + employmentIds[1] + "_" +
81+
personList[0].CpId.IdA + "_" + personList[0].CpId.IdA + "_" +
82+
employerList[1].CpId.IdA + "_" + employerList[1].CpId.IdB,
83+
//P2 + E2
84+
"EMPLOYMENT_" + employmentIds[2] + "_" +
85+
personList[1].CpId.IdA + "_" + personList[1].CpId.IdA + "_" +
86+
employerList[1].CpId.IdA + "_" + employerList[1].CpId.IdB,
87+
//P2 + E3
88+
"EMPLOYMENT_" + employmentIds[2] + "_" +
89+
personList[1].CpId.IdA + "_" + personList[1].CpId.IdA + "_" +
90+
employerList[2].CpId.IdA + "_" + employerList[2].CpId.IdB
91+
};
92+
var employmentPersons = new[]
93+
{
94+
personList[0],
95+
personList[0],
96+
personList[1],
97+
personList[1]
98+
};
99+
var employmentEmployers = new[]
100+
{
101+
employerList[0],
102+
employerList[1],
103+
employerList[1],
104+
employerList[2]
105+
};
106+
107+
for (var k = 0; k < employmentIds.Length; k++)
108+
{
109+
var employmentCpId = new EmploymentCpId
110+
{
111+
Id = employmentIds[k],
112+
PersonObj = employmentPersons[k],
113+
EmployerObj = employmentEmployers[k]
114+
};
115+
var employmentObj = new Employment { CpId = employmentCpId, Name = employmentNames[k] };
116+
s.Save(employmentObj);
117+
}
118+
119+
for (var i = 0; i < 3; i++)
120+
{
121+
var personNoComponentObj = new PersonNoComponent { IdA = gId++, IdB = gId++ };
122+
personNoComponentObj.Name = "PERSON_NO_COMPONENT_" + personNoComponentObj.IdA + "_" +
123+
personNoComponentObj.IdB;
124+
s.Save(personNoComponentObj);
125+
}
126+
127+
t.Commit();
128+
}
129+
}
130+
131+
// Test reproducing the problem.
132+
[Test]
133+
public async Task GetPersonTestAsync()
134+
{
135+
using (var session = OpenSession())
136+
{
137+
var person1_2 = await (session.GetAsync<Person>(new PersonCpId { IdA = 1, IdB = 2 }));
138+
Assert.That(person1_2.Name, Is.EqualTo("PERSON_1_2"));
139+
Assert.That(
140+
person1_2.EmploymentList.Select(e => e.Name),
141+
Is.EquivalentTo(new[] { "EMPLOYMENT_13_1_1_7_8", "EMPLOYMENT_14_1_1_9_10" }));
142+
}
143+
}
144+
145+
// Test reproducing the problem.
146+
[Test]
147+
public async Task GetEmployerTestAsync()
148+
{
149+
using (var session = OpenSession())
150+
{
151+
var employer7_8 = await (session.GetAsync<Employer>(new EmployerCpId { IdA = 7, IdB = 8 }));
152+
Assert.That(employer7_8.Name, Is.EqualTo("EMPLOYER_7_8"));
153+
Assert.That(
154+
employer7_8.EmploymentList.Select(e => e.Name),
155+
Is.EquivalentTo(new[] { "EMPLOYMENT_13_1_1_7_8" }));
156+
}
157+
}
158+
159+
[Test]
160+
public async Task GetEmploymentTestAsync()
161+
{
162+
using (var session = OpenSession())
163+
{
164+
var employment_13_1_2_7_8 =
165+
await (session.GetAsync<Employment>(
166+
new EmploymentCpId
167+
{
168+
Id = 13,
169+
PersonObj =
170+
new Person
171+
{
172+
CpId = new PersonCpId { IdA = 1, IdB = 2 }
173+
},
174+
EmployerObj =
175+
new Employer
176+
{
177+
CpId = new EmployerCpId { IdA = 7, IdB = 8 }
178+
}
179+
}));
180+
Assert.That(employment_13_1_2_7_8.Name, Is.EqualTo("EMPLOYMENT_13_1_1_7_8"));
181+
}
182+
}
183+
184+
[Test]
185+
public async Task HqlPersonPositionalAsync()
186+
{
187+
using (var session = OpenSession())
188+
{
189+
var personList =
190+
await (session
191+
.GetNamedQuery("personPositional")
192+
.SetParameter(0, new PersonCpId { IdA = 1, IdB = 2 })
193+
.SetParameter(1, new PersonCpId { IdA = 3, IdB = 4 })
194+
.ListAsync<Person>());
195+
Assert.That(
196+
personList.Select(e => e.Name),
197+
Is.EquivalentTo(new[] { "PERSON_1_2", "PERSON_3_4" }));
198+
}
199+
}
200+
201+
[Test]
202+
public async Task HqlPersonNamedAsync()
203+
{
204+
using (var session = OpenSession())
205+
{
206+
var personList =
207+
await (session
208+
.GetNamedQuery("personNamed")
209+
.SetParameter("id1", new PersonCpId { IdA = 1, IdB = 2 })
210+
.SetParameter("id2", new PersonCpId { IdA = 3, IdB = 4 })
211+
.ListAsync<Person>());
212+
Assert.That(
213+
personList.Select(e => e.Name),
214+
Is.EquivalentTo(new[] { "PERSON_1_2", "PERSON_3_4" }));
215+
}
216+
}
217+
218+
[Test]
219+
public async Task GetPersonNoComponentAsync()
220+
{
221+
using (var session = OpenSession())
222+
{
223+
var person17_18 =
224+
await (session.GetAsync<PersonNoComponent>(new PersonNoComponent { IdA = 17, IdB = 18 }));
225+
Assert.That(person17_18.Name, Is.EqualTo("PERSON_NO_COMPONENT_17_18"));
226+
}
227+
}
228+
229+
[Test]
230+
public async Task SqlPersonNoComponentAsync()
231+
{
232+
using (var session = OpenSession())
233+
{
234+
var personList =
235+
await (session
236+
.GetNamedQuery("personNoComponentSql")
237+
.SetParameter(0, new PersonNoComponent { IdA = 17, IdB = 18 })
238+
.SetParameter(1, new PersonNoComponent { IdA = 19, IdB = 20 })
239+
.ListAsync<PersonNoComponent>());
240+
Assert.That(
241+
personList.Select(e => e.Name),
242+
Is.EquivalentTo(new[] { "PERSON_NO_COMPONENT_17_18", "PERSON_NO_COMPONENT_19_20" }));
243+
}
244+
}
245+
}
246+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3079
4+
{
5+
public class Employer
6+
{
7+
public virtual EmployerCpId CpId { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
11+
public virtual ICollection<Employment> EmploymentList { get; set; }
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3079
2+
{
3+
public class EmployerCpId
4+
{
5+
public virtual int IdA { get; set; }
6+
7+
public virtual int IdB { get; set; }
8+
9+
public override bool Equals(object obj)
10+
{
11+
if (!(obj is EmployerCpId objCpId))
12+
return false;
13+
14+
return IdA == objCpId.IdA && IdB == objCpId.IdB;
15+
}
16+
17+
public override int GetHashCode()
18+
{
19+
return IdA.GetHashCode() ^ IdB.GetHashCode();
20+
}
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3079
2+
{
3+
public class Employment
4+
{
5+
public virtual EmploymentCpId CpId { get; set; }
6+
7+
public virtual string Name { get; set; }
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3079
2+
{
3+
public class EmploymentCpId
4+
{
5+
public virtual int Id { get; set; }
6+
7+
public virtual Person PersonObj { get; set; }
8+
9+
public virtual Employer EmployerObj { get; set; }
10+
11+
public override bool Equals(object obj)
12+
{
13+
if (!(obj is EmploymentCpId objCpId))
14+
return false;
15+
16+
return Id == objCpId.Id && PersonObj.CpId == objCpId.PersonObj.CpId &&
17+
EmployerObj.CpId == objCpId.EmployerObj.CpId;
18+
}
19+
20+
public override int GetHashCode()
21+
{
22+
return Id.GetHashCode() ^ PersonObj.CpId.GetHashCode() ^ EmployerObj.CpId.GetHashCode();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)