forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyCollectionFetchTests.cs
163 lines (148 loc) · 5.4 KB
/
LazyCollectionFetchTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Linq;
using NUnit.Framework;
namespace NHibernate.Test.Stateless.FetchingLazyCollections
{
[TestFixture]
public class LazyCollectionFetchTests : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.BeforeMapClass += (mi, t, cm) => cm.Id(im => im.Generator(Generators.HighLow));
mapper.Class<Animal>(mc =>
{
mc.Id(x => x.Id);
mc.Discriminator(dm => dm.Column("kind"));
mc.Property(x => x.Description);
});
mapper.Subclass<Reptile>(mc => { mc.Property(x => x.BodyTemperature); });
mapper.Subclass<Human>(mc =>
{
mc.Property(x => x.Name);
mc.Property(x => x.NickName);
mc.Property(x => x.Birthdate, pm => pm.Type(NHibernateUtil.Date));
});
mapper.AddMapping<FamilyMap<Reptile>>();
mapper.AddMapping<FamilyMap<Human>>();
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mappings;
}
#region Nested type: FamilyMap
private class FamilyMap<T> : ClassMapping<Family<T>> where T : Animal
{
public FamilyMap()
{
string familyOf = typeof (T).Name;
Id(x => x.Id);
EntityName(familyOf + "Family");
Table("Families");
Discriminator(dm => dm.Column("familyKind"));
DiscriminatorValue(familyOf);
Where(string.Format("familyKind = '{0}'", familyOf));
ManyToOne(x => x.Father, map =>
{
map.Lazy(LazyRelation.NoLazy);
map.Class(typeof (T));
map.Cascade(Mapping.ByCode.Cascade.All);
});
ManyToOne(x => x.Mother, map =>
{
map.Lazy(LazyRelation.NoLazy);
map.Class(typeof (T));
map.Cascade(Mapping.ByCode.Cascade.All);
});
Set(x => x.Childs, cam =>
{
cam.Key(km => km.Column("familyId"));
cam.Cascade(Mapping.ByCode.Cascade.All);
},
rel => rel.OneToMany());
}
}
#endregion
[Test]
public void ShouldWorkLoadingComplexEntities()
{
const string crocodileFather = "Crocodile father";
const string crocodileMother = "Crocodile mother";
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var rf = new Reptile { Description = crocodileFather };
var rm = new Reptile { Description = crocodileMother };
var rc1 = new Reptile { Description = "Crocodile" };
var rc2 = new Reptile { Description = "Crocodile" };
var rfamily = new Family<Reptile>
{
Father = rf,
Mother = rm,
Childs = new HashSet<Reptile> { rc1, rc2 }
};
s.Save("ReptileFamily", rfamily);
tx.Commit();
}
const string humanFather = "Fred";
const string humanMother = "Wilma";
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var hf = new Human { Description = "Flinstone", Name = humanFather };
var hm = new Human { Description = "Flinstone", Name = humanMother };
var hc1 = new Human { Description = "Flinstone", Name = "Pebbles" };
var hfamily = new Family<Human>
{
Father = hf,
Mother = hm,
Childs = new HashSet<Human> { hc1 }
};
s.Save("HumanFamily", hfamily);
tx.Commit();
}
using (IStatelessSession s = Sfi.OpenStatelessSession())
using (ITransaction tx = s.BeginTransaction())
{
IList<Family<Human>> hf = s.CreateQuery("from HumanFamily").List<Family<Human>>();
Assert.That(hf.Count, Is.EqualTo(1));
Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather));
Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother));
Assert.That(NHibernateUtil.IsInitialized(hf[0].Childs), Is.False, "Lazy collection should NOT be initialized");
IList<Family<Reptile>> rf = s.CreateQuery("from ReptileFamily").List<Family<Reptile>>();
Assert.That(rf.Count, Is.EqualTo(1));
Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather));
Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother));
Assert.That(NHibernateUtil.IsInitialized(hf[0].Childs), Is.False, "Lazy collection should NOT be initialized");
tx.Commit();
}
using (IStatelessSession s = Sfi.OpenStatelessSession())
using (ITransaction tx = s.BeginTransaction())
{
IList<Family<Human>> hf = s.Query<Family<Human>>().FetchMany(f => f.Childs).ToList();
Assert.That(hf.Count, Is.EqualTo(1));
Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather));
Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother));
var initialized1 = NHibernateUtil.IsInitialized(hf[0].Childs);
Assert.That(initialized1, Is.True, "Lazy collection should be initialized");
IList<Family<Reptile>> rf = s.Query<Family<Reptile>>().FetchMany(f => f.Childs).ToList();
Assert.That(rf.Count, Is.EqualTo(1));
Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather));
Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother));
var initialized2 = NHibernateUtil.IsInitialized(hf[0].Childs);
Assert.That(initialized2, Is.True, "Lazy collection should be initialized");
tx.Commit();
}
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from HumanFamily");
s.Delete("from ReptileFamily");
tx.Commit();
}
}
}
}