Skip to content

Commit 932ffff

Browse files
Merge pull request #1872 from fredericDelaporte/GH1214
Fix property ref handling * Revert #378 - NH-3480 * Fix component equality failure * Resolve collection owner correctly in case of property-ref * Resolve owner's key correctly in case of property-ref
2 parents f096548 + 8124cb5 commit 932ffff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1229
-239
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3480/Fixture.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ protected override void OnSetUp()
2626
{
2727
using (ITransaction transaction = session.BeginTransaction())
2828
{
29-
var parent1 = new Entity { Id = new Entity.Key() { Id = Guid.NewGuid() }, Name = "Bob", OtherId = 20 };
29+
var parent1 = new Entity
30+
{
31+
Id = new Entity.Key { Id = Guid.NewGuid() },
32+
Name = "Bob",
33+
OtherId = 20,
34+
YetAnotherOtherId = 21
35+
};
36+
parent1.Elements.Add(1);
37+
parent1.Elements.Add(2);
3038
session.Save(parent1);
3139

3240
var child1 = new Child { Name = "Bob1", Parent = parent1 };
@@ -68,8 +76,25 @@ public async Task TestAsync()
6876
var entity = await (result.SingleAsync());
6977

7078
await (NHibernateUtil.InitializeAsync(entity.Children));
79+
Assert.That(entity.Children, Has.Count.GreaterThan(0));
7180
}
7281
}
7382
}
83+
84+
[Test]
85+
public async Task TestOwnerAsync()
86+
{
87+
using (var session = OpenSession())
88+
using (var t = session.BeginTransaction())
89+
{
90+
var entity = await (session.Query<Entity>().SingleAsync(e => e.Name == "Bob"));
91+
92+
// The Elements collection is mapped with a custom type which assert the owner
93+
// is not null.
94+
await (NHibernateUtil.InitializeAsync(entity.Elements));
95+
Assert.That(entity.Elements, Has.Count.GreaterThan(0));
96+
await (t.CommitAsync());
97+
}
98+
}
7499
}
75-
}
100+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.Linq;
12+
using NHibernate.Linq;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.PropertyRef
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class KeyEntityPropertyRefFixtureAsync : TestCase
20+
{
21+
protected override string[] Mappings => new string[] { "PropertyRef.KeyEntityPropertyRef.hbm.xml" };
22+
23+
protected override string MappingsAssembly => "NHibernate.Test";
24+
25+
private int _aWithItemsId;
26+
27+
protected override void OnSetUp()
28+
{
29+
using (var s = OpenSession())
30+
using (var t = s.BeginTransaction())
31+
{
32+
var c1 = new C { Name = "Third" };
33+
s.Save(c1);
34+
var c2 = new C { Name = "ThirdBis" };
35+
s.Save(c2);
36+
37+
var b1 = new B { Id = 1, Name = "SecondC1" };
38+
var b2 = new B { Id = 2, Name = "SecondC2" };
39+
s.Save(b1);
40+
s.Save(b2);
41+
var a = new A { Id = 3, Name = "First", C = c1 };
42+
a.CItems.Add(b1);
43+
a.CItems.Add(b2);
44+
_aWithItemsId = (int) s.Save(a);
45+
46+
s.Save(new A { Id = 4, Name = "FirstBis", C = c2 });
47+
48+
t.Commit();
49+
}
50+
}
51+
52+
protected override void OnTearDown()
53+
{
54+
using (var s = OpenSession())
55+
using (var t = s.BeginTransaction())
56+
{
57+
s.CreateQuery("delete from B").ExecuteUpdate();
58+
s.CreateQuery("delete from A").ExecuteUpdate();
59+
s.CreateQuery("delete from C").ExecuteUpdate();
60+
t.Commit();
61+
}
62+
}
63+
64+
[Test]
65+
public async Task PropertyRefLazyLoadAsync()
66+
{
67+
using (var s = OpenSession())
68+
using (var t = s.BeginTransaction())
69+
{
70+
var a = await (s.GetAsync<A>(_aWithItemsId));
71+
72+
Assert.That(NHibernateUtil.IsInitialized(a.CItems), Is.False, "a with items");
73+
Assert.That(a.CItems, Has.Count.EqualTo(2), "a with items");
74+
await (t.CommitAsync());
75+
}
76+
77+
using (var s = OpenSession())
78+
using (var t = s.BeginTransaction())
79+
{
80+
var entity =
81+
await (s
82+
.Query<A>()
83+
.SingleAsync(a => a.Id != _aWithItemsId));
84+
85+
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.False, "a without items");
86+
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
87+
await (t.CommitAsync());
88+
}
89+
}
90+
91+
[Test]
92+
public async Task PropertyRefEagerLoadAsync()
93+
{
94+
using (var s = OpenSession())
95+
using (var t = s.BeginTransaction())
96+
{
97+
var entity =
98+
await (s
99+
.Query<A>()
100+
.Where(a => a.Id == _aWithItemsId)
101+
.FetchMany(a => a.CItems)
102+
.SingleAsync());
103+
104+
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a with items");
105+
Assert.That(entity.CItems, Has.Count.EqualTo(2), "a with items");
106+
await (t.CommitAsync());
107+
}
108+
109+
using (var s = OpenSession())
110+
using (var t = s.BeginTransaction())
111+
{
112+
var entity =
113+
await (s
114+
.Query<A>()
115+
.Where(a => a.Id != _aWithItemsId)
116+
.FetchMany(a => a.CItems)
117+
.SingleAsync());
118+
119+
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a without items");
120+
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
121+
await (t.CommitAsync());
122+
}
123+
}
124+
}
125+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 NHibernate.Criterion;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.PropertyRef
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class ManyToManyPropertyRefFixtureAsync : TestCase
19+
{
20+
protected override string[] Mappings => new[] { "PropertyRef.ManyToManyWithPropertyRef.hbm.xml" };
21+
22+
protected override string MappingsAssembly => "NHibernate.Test";
23+
24+
private object _manyAId;
25+
26+
protected override void OnSetUp()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
var manyA = new ManyA { Number = 6, Value = "a value" };
32+
33+
var manyB1 = new ManyB { Number = 4, Value = "a value of b1" };
34+
var manyB2 = new ManyB { Number = 8, Value = "a value of b2" };
35+
var manyB3 = new ManyB { Number = 12, Value = "a value of b3" };
36+
37+
_manyAId = session.Save(manyA);
38+
session.Save(manyB1);
39+
session.Save(manyB2);
40+
session.Save(manyB3);
41+
42+
manyA.ManyBs.Add(manyB1);
43+
manyA.ManyBs.Add(manyB2);
44+
manyA.ManyBs.Add(manyB3);
45+
transaction.Commit();
46+
}
47+
}
48+
49+
protected override void OnTearDown()
50+
{
51+
using (var s = OpenSession())
52+
using (var t = s.BeginTransaction())
53+
{
54+
s.Delete("from System.Object");
55+
s.Flush();
56+
t.Commit();
57+
}
58+
}
59+
60+
[Test]
61+
public async Task Getting_a_ManyA_object_with_fetchmode_select_will_workAsync()
62+
{
63+
ManyA loadedManyA;
64+
using (var session = OpenSession())
65+
using (var transaction = session.BeginTransaction())
66+
{
67+
loadedManyA =
68+
await (session
69+
.CreateCriteria<ManyA>()
70+
.Add(Restrictions.IdEq(_manyAId))
71+
// Below Fetch is a no-op indeed, provided the mapping does not ask for eager fetching.
72+
// It is the equivalent of obsoleted SetFetchMode Select, which was also no-op, contrary
73+
// to what the SelectMode xml comment could let think. (This comment was valid only when
74+
// the enum was used for mapping, but not when used ins queries.)
75+
.Fetch(SelectMode.Skip, "ManyBs")
76+
.UniqueResultAsync<ManyA>());
77+
await (NHibernateUtil.InitializeAsync(loadedManyA.ManyBs));
78+
await (transaction.CommitAsync());
79+
}
80+
81+
/******************the select statements *************************************************************
82+
SELECT this_.Id as Id0_0_,
83+
this_.Number as Number0_0_,
84+
this_.Value as Value0_0_
85+
FROM ManyA this_
86+
WHERE this_.Id = 1 /# ?p0 /#
87+
88+
SELECT manybs0_.ManyBNumber as ManyBNum1_1_,
89+
manybs0_.ManyANumber as ManyANum2_1_,
90+
manyb1_.Id as Id2_0_,
91+
manyb1_.Number as Number2_0_,
92+
manyb1_.Value as Value2_0_
93+
FROM ManyBs manybs0_
94+
left outer join ManyB manyb1_ on manybs0_.ManyANumber = manyb1_.Number
95+
WHERE manybs0_.ManyBNumber =6 /# ?p0 #/
96+
*/
97+
98+
Assert.That(loadedManyA.ManyBs.Count, Is.EqualTo(3));
99+
}
100+
101+
[Test, Ignore("Not fixed yet")]
102+
public async Task Getting_a_ManyA_object_with_fetchmode_join_will_workAsync()
103+
{
104+
ManyA loadedManyA;
105+
using (var session = OpenSession())
106+
{
107+
using (var transaction = session.BeginTransaction())
108+
{
109+
loadedManyA =
110+
await (session
111+
.CreateCriteria<ManyA>()
112+
.Add(Restrictions.IdEq(_manyAId))
113+
.Fetch(SelectMode.Fetch, "ManyBs")
114+
.UniqueResultAsync<ManyA>());
115+
await (transaction.CommitAsync());
116+
}
117+
}
118+
119+
/******************the select statments *************************************************************
120+
SELECT this_.Id as Id0_1_,
121+
this_.Number as Number0_1_,
122+
this_.Value as Value0_1_,
123+
124+
manybs2_.ManyBNumber as ManyBNum1_3_,
125+
manyb3_.Id as ManyANum2_3_,
126+
manyb3_.Id as Id2_0_,
127+
manyb3_.Number as Number2_0_,
128+
manyb3_.Value as Value2_0_
129+
130+
FROM ManyA this_
131+
132+
left outer join ManyBs manybs2_ on this_.Number=manybs2_.ManyBNumber
133+
left outer join ManyB manyb3_ on manybs2_.ManyANumber=manyb3_.Number
134+
135+
WHERE this_.Id = 1 /# ?p0 #/
136+
Exception:
137+
System.Collections.Generic.KeyNotFoundException: Der angegebene Schlüssel war nicht im Wörterbuch angegeben.
138+
bei System.ThrowHelper.ThrowKeyNotFoundException()
139+
bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
140+
bei NHibernate.Persister.Entity.AbstractEntityPersister.GetAppropriateUniqueKeyLoader(String propertyName, IDictionary`2 enabledFilters) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 2047.
141+
bei NHibernate.Persister.Entity.AbstractEntityPersister.LoadByUniqueKey(String propertyName, Object uniqueKey, ISessionImplementor session) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 2037.
142+
bei NHibernate.Type.EntityType.LoadByUniqueKey(String entityName, String uniqueKeyPropertyName, Object key, ISessionImplementor session) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Type\EntityType.cs:Zeile 552.
143+
*/
144+
145+
Assert.That(loadedManyA.ManyBs.Count, Is.EqualTo(3));
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)