1
+ using System . Linq ;
2
+ using NHibernate . Cfg . MappingSchema ;
3
+ using NHibernate . Linq ;
4
+ using NHibernate . Mapping . ByCode ;
5
+ using NUnit . Framework ;
6
+
7
+ namespace NHibernate . Test . NHSpecificTest . NH3604
8
+ {
9
+ /// <summary>
10
+ /// Tests ability to map a non-public property by code via expressions to access the hidden properties
11
+ /// </summary>
12
+ public class ByCodeFixture : TestCaseMappingByCode
13
+ {
14
+ protected override HbmMapping GetMappings ( )
15
+ {
16
+ var mapper = new ModelMapper ( ) ;
17
+ mapper . Class < Entity > ( rc =>
18
+ {
19
+ rc . Id ( Entity . PropertyAccessExpressions . Id , m => m . Generator ( Generators . GuidComb ) ) ;
20
+ rc . Property ( x => x . Name ) ;
21
+ rc . OneToOne ( x => x . Detail , m => m . Cascade ( Mapping . ByCode . Cascade . All ) ) ;
22
+ } ) ;
23
+
24
+ mapper . Class < EntityDetail > ( rc =>
25
+ {
26
+ rc . Id ( x => x . Id , m => m . Generator ( new ForeignGeneratorDef ( ReflectionHelper . GetProperty ( EntityDetail . PropertyAccessExpressions . Entity ) ) ) ) ;
27
+ rc . OneToOne ( EntityDetail . PropertyAccessExpressions . Entity , m => m . Constrained ( true ) ) ;
28
+ rc . Property ( x => x . ExtraInfo ) ;
29
+ } ) ;
30
+
31
+ return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
32
+ }
33
+
34
+ protected override void OnSetUp ( )
35
+ {
36
+ using ( ISession session = OpenSession ( ) )
37
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
38
+ {
39
+ var e1 = new Entity { Name = "Bob" } ;
40
+ session . Save ( e1 ) ;
41
+
42
+
43
+ var e2 = new Entity { Name = "Sally" } ;
44
+ var ed2 = new EntityDetail ( e2 ) { ExtraInfo = "Jo" } ;
45
+ e2 . Detail = ed2 ;
46
+
47
+ session . Save ( e2 ) ;
48
+
49
+ session . Flush ( ) ;
50
+ transaction . Commit ( ) ;
51
+ }
52
+ }
53
+
54
+ protected override void OnTearDown ( )
55
+ {
56
+ using ( ISession session = OpenSession ( ) )
57
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
58
+ {
59
+ session . Delete ( "from System.Object" ) ;
60
+
61
+ session . Flush ( ) ;
62
+ transaction . Commit ( ) ;
63
+ }
64
+ }
65
+
66
+ [ Test ]
67
+ public void PerformQuery ( )
68
+ {
69
+ using ( ISession session = OpenSession ( ) )
70
+ using ( session . BeginTransaction ( ) )
71
+ {
72
+ var result = from e in session . Query < Entity > ( )
73
+ where e . Name == "Sally"
74
+ select e ;
75
+
76
+ var entities = result . ToList ( ) ;
77
+ Assert . AreEqual ( 1 , entities . Count ) ;
78
+ Assert . AreEqual ( "Jo" , entities [ 0 ] . Detail . ExtraInfo ) ;
79
+ }
80
+ }
81
+
82
+ [ Test ]
83
+ public void WriteXmlMappings ( )
84
+ {
85
+ var mapper = new ModelMapper ( ) ;
86
+ mapper . Class < Entity > ( rc =>
87
+ {
88
+ rc . Id ( Entity . PropertyAccessExpressions . Id , m => m . Generator ( Generators . GuidComb ) ) ;
89
+ rc . Property ( x => x . Name ) ;
90
+ } ) ;
91
+ mapper . CompileMappingForEachExplicitlyAddedEntity ( ) . WriteAllXmlMapping ( ) ;
92
+ }
93
+ }
94
+ }
0 commit comments