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