forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeFetchTests.cs
68 lines (63 loc) · 1.95 KB
/
TreeFetchTests.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
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Linq;
using NUnit.Framework;
namespace NHibernate.Test.Stateless.FetchingLazyCollections
{
[TestFixture]
public class TreeFetchTests : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.BeforeMapClass += (mi, t, cm) => cm.Id(im => im.Generator(Generators.HighLow));
mapper.Class<TreeNode>(
mc =>
{
mc.Id(x => x.Id);
mc.Property(x => x.Content);
mc.Set(x => x.Children, cam =>
{
cam.Key(km => km.Column("parentId"));
cam.Cascade(Mapping.ByCode.Cascade.All);
}, rel => rel.OneToMany());
});
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mappings;
}
[Test]
public void FetchMultipleHierarchies()
{
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var root = new TreeNode {Content = "Root"};
var child1 = new TreeNode {Content = "Child1"};
root.Children.Add(child1);
root.Children.Add(new TreeNode {Content = "Child2"});
child1.Children.Add(new TreeNode {Content = "Child1Child1"});
child1.Children.Add(new TreeNode {Content = "Child1Child2"});
s.Save(root);
tx.Commit();
}
using (IStatelessSession s = Sfi.OpenStatelessSession())
using (ITransaction tx = s.BeginTransaction())
{
IList<TreeNode> rootNodes = s.Query<TreeNode>().Where(t => t.Content == "Root")
.FetchMany(f => f.Children)
.ThenFetchMany(f => f.Children).ToList();
Assert.That(rootNodes.Count, Is.EqualTo(1));
Assert.That(rootNodes.First().Children.Count, Is.EqualTo(2));
tx.Commit();
}
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from TreeNode");
tx.Commit();
}
}
}
}