1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using NHibernate . Cfg . MappingSchema ;
5
+ using NHibernate . Mapping . ByCode ;
6
+ using NHibernate . Mapping . ByCode . Conformist ;
7
+ using NHibernate . Linq ;
8
+ using NUnit . Framework ;
9
+ using SharpTestsEx ;
10
+
11
+ namespace NHibernate . Test . Stateless . FetchingLazyCollections
12
+ {
13
+ public class TreeFetchTests : TestCaseMappingByCode
14
+ {
15
+ protected override HbmMapping GetMappings ( )
16
+ {
17
+ var mapper = new ModelMapper ( ) ;
18
+ mapper . BeforeMapClass += ( mi , t , cm ) => cm . Id ( im => im . Generator ( Generators . HighLow ) ) ;
19
+ mapper . Class < TreeNode > (
20
+ mc =>
21
+ {
22
+ mc . Id ( x => x . Id ) ;
23
+ mc . Property ( x => x . Content ) ;
24
+ mc . Set ( x => x . Children , cam =>
25
+ {
26
+ cam . Key ( km => km . Column ( "parentId" ) ) ;
27
+ cam . Cascade ( Mapping . ByCode . Cascade . All ) ;
28
+ } , rel => rel . OneToMany ( ) ) ;
29
+ } ) ;
30
+ var mappings = mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
31
+ return mappings ;
32
+ }
33
+
34
+ [ Test ]
35
+ public void FetchMultipleHierarchies ( )
36
+ {
37
+ using ( ISession s = sessions . OpenSession ( ) )
38
+ using ( ITransaction tx = s . BeginTransaction ( ) )
39
+ {
40
+ var root = new TreeNode { Content = "Root" } ;
41
+ var child1 = new TreeNode { Content = "Child1" } ;
42
+ root . Children . Add ( child1 ) ;
43
+ root . Children . Add ( new TreeNode { Content = "Child2" } ) ;
44
+ child1 . Children . Add ( new TreeNode { Content = "Child1Child1" } ) ;
45
+ child1 . Children . Add ( new TreeNode { Content = "Child1Child2" } ) ;
46
+ s . Save ( root ) ;
47
+ tx . Commit ( ) ;
48
+ }
49
+
50
+ using ( IStatelessSession s = sessions . OpenStatelessSession ( ) )
51
+ using ( ITransaction tx = s . BeginTransaction ( ) )
52
+ {
53
+ IList < TreeNode > rootNodes = s . Query < TreeNode > ( ) . Where ( t => t . Content == "Root" )
54
+ . FetchMany ( f => f . Children )
55
+ . ThenFetchMany ( f => f . Children ) . ToList ( ) ;
56
+ Assert . That ( rootNodes . Count , Is . EqualTo ( 1 ) ) ;
57
+ Assert . That ( rootNodes . First ( ) . Children . Count , Is . EqualTo ( 2 ) ) ;
58
+
59
+ tx . Commit ( ) ;
60
+ }
61
+
62
+ using ( ISession s = sessions . OpenSession ( ) )
63
+ using ( ITransaction tx = s . BeginTransaction ( ) )
64
+ {
65
+ s . Delete ( "from TreeNode" ) ;
66
+ tx . Commit ( ) ;
67
+ }
68
+ }
69
+ }
70
+ }
0 commit comments