@@ -76,97 +76,99 @@ protected override void OnTearDown()
76
76
[ Test ]
77
77
public void TestSimpleCRUD ( )
78
78
{
79
- // insert the new objects
80
- ISession s = OpenSession ( ) ;
81
- ITransaction t = s . BeginTransaction ( ) ;
79
+ ClassWithCompositeId theClass ;
80
+ ClassWithCompositeId theSecondClass ;
82
81
83
- ClassWithCompositeId theClass = new ClassWithCompositeId ( id ) ;
84
- theClass . OneProperty = 5 ;
82
+ // insert the new objects
83
+ using ( ISession s = OpenSession ( ) )
84
+ using ( ITransaction t = s . BeginTransaction ( ) )
85
+ {
86
+ theClass = new ClassWithCompositeId ( id ) ;
87
+ theClass . OneProperty = 5 ;
85
88
86
- ClassWithCompositeId theSecondClass = new ClassWithCompositeId ( secondId ) ;
87
- theSecondClass . OneProperty = 10 ;
89
+ theSecondClass = new ClassWithCompositeId ( secondId ) ;
90
+ theSecondClass . OneProperty = 10 ;
88
91
89
- s . Save ( theClass ) ;
90
- s . Save ( theSecondClass ) ;
92
+ s . Save ( theClass ) ;
93
+ s . Save ( theSecondClass ) ;
91
94
92
- t . Commit ( ) ;
93
- s . Close ( ) ;
95
+ t . Commit ( ) ;
96
+ }
94
97
95
98
// verify they were inserted and test the SELECT
99
+ ClassWithCompositeId theClass2 ;
100
+ ClassWithCompositeId theSecondClass2 ;
101
+ using ( ISession s2 = OpenSession ( ) )
102
+ using ( ITransaction t2 = s2 . BeginTransaction ( ) )
103
+ {
104
+ theClass2 = ( ClassWithCompositeId ) s2 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
105
+ Assert . AreEqual ( id , theClass2 . Id ) ;
96
106
97
- ISession s2 = OpenSession ( ) ;
98
- ITransaction t2 = s2 . BeginTransaction ( ) ;
99
-
100
- ClassWithCompositeId theClass2 = ( ClassWithCompositeId ) s2 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
101
- Assert . AreEqual ( id , theClass2 . Id ) ;
102
-
103
- IList results2 = s2 . CreateCriteria ( typeof ( ClassWithCompositeId ) )
104
- . Add ( Expression . Eq ( "Id" , secondId ) )
105
- . List ( ) ;
107
+ IList results2 = s2 . CreateCriteria ( typeof ( ClassWithCompositeId ) )
108
+ . Add ( Expression . Eq ( "Id" , secondId ) )
109
+ . List ( ) ;
106
110
107
- Assert . AreEqual ( 1 , results2 . Count ) ;
108
- ClassWithCompositeId theSecondClass2 = ( ClassWithCompositeId ) results2 [ 0 ] ;
111
+ Assert . AreEqual ( 1 , results2 . Count ) ;
112
+ theSecondClass2 = ( ClassWithCompositeId ) results2 [ 0 ] ;
109
113
110
- ClassWithCompositeId theClass2Copy = ( ClassWithCompositeId ) s2 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
114
+ ClassWithCompositeId theClass2Copy = ( ClassWithCompositeId ) s2 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
111
115
112
- // verify the same results through Criteria & Load were achieved
113
- Assert . AreSame ( theClass2 , theClass2Copy ) ;
116
+ // verify the same results through Criteria & Load were achieved
117
+ Assert . AreSame ( theClass2 , theClass2Copy ) ;
114
118
115
- // compare them to the objects created in the first session
116
- Assert . AreEqual ( theClass . Id , theClass2 . Id ) ;
117
- Assert . AreEqual ( theClass . OneProperty , theClass2 . OneProperty ) ;
119
+ // compare them to the objects created in the first session
120
+ Assert . AreEqual ( theClass . Id , theClass2 . Id ) ;
121
+ Assert . AreEqual ( theClass . OneProperty , theClass2 . OneProperty ) ;
118
122
119
- Assert . AreEqual ( theSecondClass . Id , theSecondClass2 . Id ) ;
120
- Assert . AreEqual ( theSecondClass . OneProperty , theSecondClass2 . OneProperty ) ;
123
+ Assert . AreEqual ( theSecondClass . Id , theSecondClass2 . Id ) ;
124
+ Assert . AreEqual ( theSecondClass . OneProperty , theSecondClass2 . OneProperty ) ;
121
125
122
- // test the update functionallity
123
- theClass2 . OneProperty = 6 ;
124
- theSecondClass2 . OneProperty = 11 ;
126
+ // test the update functionallity
127
+ theClass2 . OneProperty = 6 ;
128
+ theSecondClass2 . OneProperty = 11 ;
125
129
126
- s2 . Update ( theClass2 ) ;
127
- s2 . Update ( theSecondClass2 ) ;
130
+ s2 . Update ( theClass2 ) ;
131
+ s2 . Update ( theSecondClass2 ) ;
128
132
129
- t2 . Commit ( ) ;
130
- s2 . Close ( ) ;
133
+ t2 . Commit ( ) ;
134
+ }
131
135
132
136
// lets verify the update went through
133
- ISession s3 = OpenSession ( ) ;
134
- ITransaction t3 = s3 . BeginTransaction ( ) ;
135
-
136
- ClassWithCompositeId theClass3 = ( ClassWithCompositeId ) s3 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
137
- ClassWithCompositeId theSecondClass3 = ( ClassWithCompositeId ) s3 . Load ( typeof ( ClassWithCompositeId ) , secondId ) ;
137
+ using ( ISession s3 = OpenSession ( ) )
138
+ using ( ITransaction t3 = s3 . BeginTransaction ( ) )
139
+ {
140
+ ClassWithCompositeId theClass3 = ( ClassWithCompositeId ) s3 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
141
+ ClassWithCompositeId theSecondClass3 = ( ClassWithCompositeId ) s3 . Load ( typeof ( ClassWithCompositeId ) , secondId ) ;
138
142
139
- // check the update properties
140
- Assert . AreEqual ( theClass3 . OneProperty , theClass2 . OneProperty ) ;
141
- Assert . AreEqual ( theSecondClass3 . OneProperty , theSecondClass2 . OneProperty ) ;
143
+ // check the update properties
144
+ Assert . AreEqual ( theClass3 . OneProperty , theClass2 . OneProperty ) ;
145
+ Assert . AreEqual ( theSecondClass3 . OneProperty , theSecondClass2 . OneProperty ) ;
142
146
143
- // test the delete method
144
- s3 . Delete ( theClass3 ) ;
145
- s3 . Delete ( theSecondClass3 ) ;
147
+ // test the delete method
148
+ s3 . Delete ( theClass3 ) ;
149
+ s3 . Delete ( theSecondClass3 ) ;
146
150
147
- t3 . Commit ( ) ;
148
- s3 . Close ( ) ;
151
+ t3 . Commit ( ) ;
152
+ }
149
153
150
154
// lets verify the delete went through
151
- ISession s4 = OpenSession ( ) ;
152
-
153
- try
155
+ using ( ISession s4 = OpenSession ( ) )
154
156
{
155
- ClassWithCompositeId theClass4 = ( ClassWithCompositeId ) s4 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
157
+ try
158
+ {
159
+ ClassWithCompositeId theClass4 = ( ClassWithCompositeId ) s4 . Load ( typeof ( ClassWithCompositeId ) , id ) ;
160
+ }
161
+ catch ( ObjectNotFoundException )
162
+ {
163
+ // I expect this to be thrown because the object no longer exists...
164
+ }
165
+
166
+ IList results = s4 . CreateCriteria ( typeof ( ClassWithCompositeId ) )
167
+ . Add ( Expression . Eq ( "Id" , secondId ) )
168
+ . List ( ) ;
169
+
170
+ Assert . AreEqual ( 0 , results . Count ) ;
156
171
}
157
- catch ( ObjectNotFoundException onfe )
158
- {
159
- // I expect this to be thrown because the object no longer exists...
160
- Assert . IsNotNull ( onfe ) ; //getting ride of 'onfe' is never used compile warning
161
- }
162
-
163
- IList results = s4 . CreateCriteria ( typeof ( ClassWithCompositeId ) )
164
- . Add ( Expression . Eq ( "Id" , secondId ) )
165
- . List ( ) ;
166
-
167
- Assert . AreEqual ( 0 , results . Count ) ;
168
-
169
- s4 . Close ( ) ;
170
172
}
171
173
172
174
[ Test ]
@@ -178,53 +180,53 @@ public void Criteria()
178
180
179
181
// add the new instance to the session so I have something to get results
180
182
// back for
181
- ISession s = OpenSession ( ) ;
182
- s . Save ( cId ) ;
183
- s . Flush ( ) ;
184
- s . Close ( ) ;
185
-
186
- s = OpenSession ( ) ;
187
- ICriteria c = s . CreateCriteria ( typeof ( ClassWithCompositeId ) ) ;
188
- c . Add ( Expression . Eq ( "Id" , id ) ) ;
183
+ using ( ISession s = OpenSession ( ) )
184
+ {
185
+ s . Save ( cId ) ;
186
+ s . Flush ( ) ;
187
+ }
189
188
190
- // right now just want to see if the Criteria is valid
191
- IList results = c . List ( ) ;
189
+ using ( ISession s = OpenSession ( ) )
190
+ {
191
+ ICriteria c = s . CreateCriteria ( typeof ( ClassWithCompositeId ) ) ;
192
+ c . Add ( Expression . Eq ( "Id" , id ) ) ;
192
193
193
- Assert . AreEqual ( 1 , results . Count ) ;
194
+ // right now just want to see if the Criteria is valid
195
+ IList results = c . List ( ) ;
194
196
195
- s . Close ( ) ;
197
+ Assert . AreEqual ( 1 , results . Count ) ;
198
+ }
196
199
}
197
200
198
201
[ Test ]
199
202
public void Hql ( )
200
203
{
201
204
// insert the new objects
202
- ISession s = OpenSession ( ) ;
203
- ITransaction t = s . BeginTransaction ( ) ;
204
-
205
- ClassWithCompositeId theClass = new ClassWithCompositeId ( id ) ;
206
- theClass . OneProperty = 5 ;
207
-
208
- ClassWithCompositeId theSecondClass = new ClassWithCompositeId ( secondId ) ;
209
- theSecondClass . OneProperty = 10 ;
210
-
211
- s . Save ( theClass ) ;
212
- s . Save ( theSecondClass ) ;
205
+ using ( ISession s = OpenSession ( ) )
206
+ using ( ITransaction t = s . BeginTransaction ( ) )
207
+ {
208
+ ClassWithCompositeId theClass = new ClassWithCompositeId ( id ) ;
209
+ theClass . OneProperty = 5 ;
213
210
214
- t . Commit ( ) ;
215
- s . Close ( ) ;
211
+ ClassWithCompositeId theSecondClass = new ClassWithCompositeId ( secondId ) ;
212
+ theSecondClass . OneProperty = 10 ;
216
213
217
- ISession s2 = OpenSession ( ) ;
214
+ s . Save ( theClass ) ;
215
+ s . Save ( theSecondClass ) ;
218
216
219
- IQuery hql = s2 . CreateQuery ( "from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString" ) ;
217
+ t . Commit ( ) ;
218
+ }
220
219
221
- hql . SetString ( "keyString" , id . KeyString ) ;
220
+ using ( ISession s2 = OpenSession ( ) )
221
+ {
222
+ IQuery hql = s2 . CreateQuery ( "from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString" ) ;
222
223
223
- IList results = hql . List ( ) ;
224
+ hql . SetString ( "keyString" , id . KeyString ) ;
224
225
225
- Assert . AreEqual ( 1 , results . Count ) ;
226
+ IList results = hql . List ( ) ;
226
227
227
- s2 . Close ( ) ;
228
+ Assert . AreEqual ( 1 , results . Count ) ;
229
+ }
228
230
}
229
231
}
230
- }
232
+ }
0 commit comments