1
+ using System ;
2
+ using System . Collections ;
3
+ using NUnit . Framework ;
4
+
5
+ namespace NHibernate . Test . CompositeId
6
+ {
7
+ [ TestFixture ]
8
+ public class CompositeIdFixture : TestCase
9
+ {
10
+ protected override string MappingsAssembly
11
+ {
12
+ get { return "NHibernate.Test" ; }
13
+ }
14
+
15
+ protected override IList Mappings
16
+ {
17
+ get
18
+ {
19
+ return new string [ ]
20
+ {
21
+ "CompositeId.Customer.hbm.xml" , "CompositeId.Order.hbm.xml" , "CompositeId.LineItem.hbm.xml" ,
22
+ "CompositeId.Product.hbm.xml"
23
+ } ;
24
+ }
25
+ }
26
+
27
+ protected override string CacheConcurrencyStrategy
28
+ {
29
+ get { return null ; }
30
+ }
31
+
32
+ [ Test ]
33
+ public void CompositeIds ( )
34
+ {
35
+ ISession s ;
36
+ ITransaction t ;
37
+ Product p2 ;
38
+ using ( s = OpenSession ( ) )
39
+ {
40
+ t = s . BeginTransaction ( ) ;
41
+
42
+ Product p = new Product ( ) ;
43
+ p . ProductId = "A123" ;
44
+ p . Description = "nipple ring" ;
45
+ p . Price = 1.0m ;
46
+ p . NumberAvailable = 1004 ;
47
+ s . Persist ( p ) ;
48
+
49
+ p2 = new Product ( ) ;
50
+ p2 . ProductId = "X525" ;
51
+ p2 . Description = "nose stud" ;
52
+ p2 . Price = 3.0m ;
53
+ p2 . NumberAvailable = 105 ;
54
+ s . Persist ( p2 ) ;
55
+
56
+ Customer c = new Customer ( ) ;
57
+ c . Address = "St Kilda Rd, MEL, 3000" ;
58
+ c . Name = "Virginia" ;
59
+ c . CustomerId = "C111" ;
60
+ s . Persist ( c ) ;
61
+
62
+ Order o = new Order ( c ) ;
63
+ o . OrderDate = DateTime . Today ;
64
+ LineItem li = new LineItem ( o , p ) ;
65
+ li . Quantity = 2 ;
66
+
67
+ t . Commit ( ) ;
68
+ }
69
+
70
+ using ( s = OpenSession ( ) )
71
+ {
72
+ t = s . BeginTransaction ( ) ;
73
+ Order o = s . Get < Order > ( new Order . ID ( "C111" , 0 ) ) ;
74
+ Assert . That ( o . Total == 2m ) ;
75
+ t . Commit ( ) ;
76
+ }
77
+
78
+ using ( s = OpenSession ( ) )
79
+ {
80
+ t = s . BeginTransaction ( ) ;
81
+ s . CreateQuery (
82
+ "from Customer c left join fetch c.Orders o left join fetch o.LineItems li left join fetch li.Product p" ) . List ( ) ;
83
+ t . Commit ( ) ;
84
+ }
85
+
86
+ using ( s = OpenSession ( ) )
87
+ {
88
+ t = s . BeginTransaction ( ) ;
89
+ s . CreateQuery ( "from Order o left join fetch o.LineItems li left join fetch li.Product p" ) . List ( ) ;
90
+ t . Commit ( ) ;
91
+ }
92
+
93
+ using ( s = OpenSession ( ) )
94
+ {
95
+ t = s . BeginTransaction ( ) ;
96
+ IEnumerable iter = s . CreateQuery ( "select o.id, li.id from Order o join o.LineItems li" ) . List ( ) ;
97
+ foreach ( object [ ] stuff in iter )
98
+ {
99
+ Assert . AreEqual ( 2 , stuff . Length ) ;
100
+ }
101
+ iter = s . CreateQuery ( "from Order o join o.LineItems li" ) . Enumerable ( ) ;
102
+ foreach ( object [ ] stuff in iter )
103
+ {
104
+ Assert . AreEqual ( 2 , stuff . Length ) ;
105
+ }
106
+ t . Commit ( ) ;
107
+ }
108
+
109
+ using ( s = OpenSession ( ) )
110
+ {
111
+ t = s . BeginTransaction ( ) ;
112
+ Customer c = s . Get < Customer > ( "C111" ) ;
113
+ Order o2 = new Order ( c ) ;
114
+ o2 . OrderDate = DateTime . Today ;
115
+ s . Flush ( ) ;
116
+ LineItem li2 = new LineItem ( o2 , p2 ) ;
117
+ li2 . Quantity = 5 ;
118
+ IList bigOrders = s . CreateQuery ( "from Order o where o.Total>10.0" ) . List ( ) ;
119
+ Assert . AreEqual ( 1 , bigOrders . Count ) ;
120
+ t . Commit ( ) ;
121
+ }
122
+
123
+
124
+ using ( s = OpenSession ( ) )
125
+ {
126
+ t = s . BeginTransaction ( ) ;
127
+ s . Delete ( "from LineItem" ) ;
128
+ s . Delete ( "from Order" ) ;
129
+ s . Delete ( "from Customer" ) ;
130
+ s . Delete ( "from Product" ) ;
131
+ t . Commit ( ) ;
132
+ }
133
+ }
134
+
135
+ [ Test ]
136
+ public void MultipleCollectionFetch ( )
137
+ {
138
+ ISession s = OpenSession ( ) ;
139
+ ITransaction t = s . BeginTransaction ( ) ;
140
+ Product p = new Product ( ) ;
141
+ p . ProductId = "A123" ;
142
+ p . Description = "nipple ring" ;
143
+ p . Price = 1.0m ;
144
+ p . NumberAvailable = 1004 ;
145
+ s . Persist ( p ) ;
146
+
147
+ Product p2 = new Product ( ) ;
148
+ p2 . ProductId = "X525" ;
149
+ p2 . Description = "nose stud" ;
150
+ p2 . Price = 3.0m ;
151
+ p2 . NumberAvailable = 105 ;
152
+ s . Persist ( p2 ) ;
153
+
154
+ Customer c = new Customer ( ) ;
155
+ c . Address = "St Kilda Rd, MEL, 3000" ;
156
+ c . Name = "Virginia" ;
157
+ c . CustomerId = "C111" ;
158
+ s . Persist ( c ) ;
159
+
160
+ Order o = new Order ( c ) ;
161
+ o . OrderDate = DateTime . Today ;
162
+ LineItem li = new LineItem ( o , p ) ;
163
+ li . Quantity = 2 ;
164
+ LineItem li2 = new LineItem ( o , p2 ) ;
165
+ li2 . Quantity = 3 ;
166
+
167
+ Order o2 = new Order ( c ) ;
168
+ o2 . OrderDate = DateTime . Today ;
169
+ LineItem li3 = new LineItem ( o2 , p ) ;
170
+ li3 . Quantity = 1 ;
171
+ LineItem li4 = new LineItem ( o2 , p2 ) ;
172
+ li4 . Quantity = 1 ;
173
+
174
+ t . Commit ( ) ;
175
+ s . Close ( ) ;
176
+
177
+ s = OpenSession ( ) ;
178
+ t = s . BeginTransaction ( ) ;
179
+ c =
180
+ ( Customer )
181
+ s . CreateQuery (
182
+ "from Customer c left join fetch c.Orders o left join fetch o.LineItems li left join fetch li.Product p" ) .
183
+ UniqueResult ( ) ;
184
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( c . Orders ) ) ;
185
+ Assert . AreEqual ( 2 , c . Orders . Count ) ;
186
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( ( ( Order ) c . Orders [ 0 ] ) . LineItems ) ) ;
187
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( ( ( Order ) c . Orders [ 1 ] ) . LineItems ) ) ;
188
+ Assert . AreEqual ( ( ( Order ) c . Orders [ 0 ] ) . LineItems . Count , 2 ) ;
189
+ Assert . AreEqual ( ( ( Order ) c . Orders [ 1 ] ) . LineItems . Count , 2 ) ;
190
+ t . Commit ( ) ;
191
+ s . Close ( ) ;
192
+
193
+ s = OpenSession ( ) ;
194
+ t = s . BeginTransaction ( ) ;
195
+ s . Delete ( "from LineItem" ) ;
196
+ s . Delete ( "from Order" ) ;
197
+ s . Delete ( "from Customer" ) ;
198
+ s . Delete ( "from Product" ) ;
199
+ t . Commit ( ) ;
200
+ s . Close ( ) ;
201
+ }
202
+
203
+ [ Test ]
204
+ public void NonLazyFetch ( )
205
+ {
206
+ ISession s = OpenSession ( ) ;
207
+ ITransaction t = s . BeginTransaction ( ) ;
208
+ Product p = new Product ( ) ;
209
+ p . ProductId = "A123" ;
210
+ p . Description = "nipple ring" ;
211
+ p . Price = 1.0m ;
212
+ p . NumberAvailable = 1004 ;
213
+ s . Persist ( p ) ;
214
+
215
+ Product p2 = new Product ( ) ;
216
+ p2 . ProductId = "X525" ;
217
+ p2 . Description = "nose stud" ;
218
+ p2 . Price = 3.0m ;
219
+ p2 . NumberAvailable = 105 ;
220
+ s . Persist ( p2 ) ;
221
+
222
+ Customer c = new Customer ( ) ;
223
+ c . Address = "St Kilda Rd, MEL, 3000" ;
224
+ c . Name = "Virginia" ;
225
+ c . CustomerId = "C111" ;
226
+ s . Persist ( c ) ;
227
+
228
+ Order o = new Order ( c ) ;
229
+ o . OrderDate = DateTime . Today ;
230
+ LineItem li = new LineItem ( o , p ) ;
231
+ li . Quantity = 2 ;
232
+
233
+ t . Commit ( ) ;
234
+ s . Close ( ) ;
235
+
236
+ s = OpenSession ( ) ;
237
+ t = s . BeginTransaction ( ) ;
238
+ o = s . Get < Order > ( new Order . ID ( "C111" , 0 ) ) ;
239
+ Assert . AreEqual ( 2m , o . Total ) ;
240
+ t . Commit ( ) ;
241
+ s . Close ( ) ;
242
+
243
+ s = OpenSession ( ) ;
244
+ t = s . BeginTransaction ( ) ;
245
+ o = ( Order ) s . CreateQuery ( "from Order o left join fetch o.LineItems li left join fetch li.Product p" ) . UniqueResult ( ) ;
246
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( o . LineItems ) ) ;
247
+ li = ( LineItem ) o . LineItems [ 0 ] ;
248
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( li ) ) ;
249
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( li . Product ) ) ;
250
+ t . Commit ( ) ;
251
+ s . Close ( ) ;
252
+
253
+ s = OpenSession ( ) ;
254
+ t = s . BeginTransaction ( ) ;
255
+ o = ( Order ) s . CreateQuery ( "from Order o" ) . UniqueResult ( ) ;
256
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( o . LineItems ) ) ;
257
+ li = ( LineItem ) o . LineItems [ 0 ] ;
258
+ Assert . IsTrue ( NHibernateUtil . IsInitialized ( li ) ) ;
259
+ Assert . IsFalse ( NHibernateUtil . IsInitialized ( li . Product ) ) ;
260
+ t . Commit ( ) ;
261
+ s . Close ( ) ;
262
+
263
+ s = OpenSession ( ) ;
264
+ t = s . BeginTransaction ( ) ;
265
+ s . Delete ( "from LineItem" ) ;
266
+ s . Delete ( "from Order" ) ;
267
+ s . Delete ( "from Customer" ) ;
268
+ s . Delete ( "from Product" ) ;
269
+ t . Commit ( ) ;
270
+ s . Close ( ) ;
271
+ }
272
+
273
+ [ Test ]
274
+ public void Query ( )
275
+ {
276
+ ISession s = OpenSession ( ) ;
277
+ ITransaction t = s . BeginTransaction ( ) ;
278
+ s . CreateQuery ( "from LineItem ol where ol.Order.Id.CustomerId = 'C111'" ) . List ( ) ;
279
+ t . Commit ( ) ;
280
+ s . Close ( ) ;
281
+ }
282
+ }
283
+ }
0 commit comments