KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > orm > DbOrmTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.orm;
4
5 import static jodd.db.orm.sqlgen.DbSqlTemplate.ColumnAliasType.COLUMN_CODE;
6 import junit.framework.TestCase;
7 import jodd.db.connection.ConnectionProvider;
8 import jodd.db.pool.CoreConnectionPool;
9 import jodd.db.DbSession;
10 import jodd.db.DbThreadSession;
11 import jodd.db.DbQuery;
12 import jodd.db.DbQueryMode;
13 import jodd.db.orm.test.*;
14 import jodd.db.orm.sqlgen.*;
15
16 import java.util.*;
17
18 public class DbOrmTest extends TestCase {
19
20     ConnectionProvider cp;
21
22     protected void setUp() throws Exception JavaDoc {
23         super.setUp();
24         //noinspection NonConstantStringShouldBeStringBuffer
25
String JavaDoc dbFile = System.getProperty("prj.build.dir");
26         if (dbFile == null) {
27             dbFile = "";
28         }
29         dbFile += "/db/jodd5-db";
30         CoreConnectionPool pool = new CoreConnectionPool();
31         pool.setDriver("org.hsqldb.jdbcDriver");
32         pool.setUrl("jdbc:hsqldb:" + dbFile);
33         pool.setUser("sa");
34         pool.setPassword("");
35         pool.init();
36         cp = pool;
37     }
38
39     protected void tearDown() throws Exception JavaDoc {
40         super.tearDown();
41         cp.close();
42     }
43
44
45     public void testOrm() {
46
47         DbSession session = new DbThreadSession(cp);
48         String JavaDoc sql = "create table GIRL (" +
49                            "ID integer not null," +
50                            "NAME varchar(20) not null," +
51                            "SPECIALITY varchar(20) null," +
52                            "primary key (ID)" +
53                             ')';
54         DbQuery query = new DbQuery(sql);
55         query.executeUpdate();
56         assertTrue(query.isClosed());
57
58         sql = "create table BOY (" +
59                            "ID integer not null," +
60                            "GIRL_ID integer null," +
61                            "NAME varchar(20) null," +
62                            "primary key (ID)," +
63                             "FOREIGN KEY (GIRL_ID) REFERENCES GIRL (ID)" +
64                             ')';
65         query = new DbQuery(sql);
66         query.executeUpdate();
67         assertTrue(query.isClosed());
68
69         session.closeSession();
70         assertTrue(session.isSessionClosed());
71
72
73         // prepare
74
session = new DbThreadSession(cp);
75
76         DbOrm dbOrm = DbOrm.getInstance();
77         dbOrm.mapTable(BadBoy.class);
78
79         // ---------------------------------------------------------------- insert
80

81         assertEquals(1, new DbOrmQuery(new DbSqlInsert(new Girl(1, "Anna", "seduction"))).executeUpdate());
82         assertEquals(1, new DbOrmQuery(new DbSqlInsert(new Girl(2, "Sandra", "spying"))).executeUpdate());
83         assertEquals(0, session.getTotalActiveQueries());
84         assertEquals(2, session.getTotalQueries());
85
86         DbOrmQuery q2 = new DbOrmQuery(new DbSqlInsert(new Girl(3, "Monica", null)), new DbQueryMode().setDebug(true));
87         assertEquals("insert into GIRL (NAME, ID) values ('Monica', 3)", q2.getQueryString());
88         assertEquals(1, q2.executeUpdate());
89         assertTrue(q2.isClosed());
90
91         assertEquals(1, new DbOrmQuery(new DbSqlInsert(new BadBoy(Integer.valueOf(1), "Johny", Integer.valueOf(3)))).executeUpdate());
92         assertEquals(0, session.getTotalActiveQueries());
93         assertEquals(4, session.getTotalQueries());
94
95
96         // ---------------------------------------------------------------- girl
97

98         DbOrmQuery q = new DbOrmQuery("select * from GIRL order by ID");
99
100         Girl girl = q.findOne(Girl.class);
101         checkGirl1(girl);
102         assertTrue(q.isActive());
103
104         girl = (Girl) (q.find(Girl.class))[0];
105         checkGirl1(girl);
106
107         try {
108             //noinspection UnusedAssignment
109
girl = (Girl) (q.find()[0]);
110             fail();
111         } catch(DbOrmException doex) {
112         }
113
114
115         // list
116
List<Girl> listGirl = q.listOne(Girl.class);
117         assertEquals(3, listGirl.size());
118         girl = listGirl.get(1);
119         checkGirl2(girl);
120         
121
122         List list = q.list(Girl.class);
123         assertEquals(3, list.size());
124         girl = (Girl) ((Object JavaDoc[]) list.get(2))[0];
125         checkGirl3(girl);
126         assertFalse(q.isClosed());
127
128         // set
129
Set<Girl> setGirl = q.listSetOne(Girl.class);
130         assertEquals(3, setGirl.size());
131         girl = (setGirl.iterator().next());
132         checkGirl1(girl);
133
134         Set set = q.listSet(Girl.class);
135         assertEquals(3, set.size());
136         girl = (Girl) ((Object JavaDoc[]) set.iterator().next())[0];
137         checkGirl1(girl);
138
139         // iterator
140
Iterator it = q.iterateOne(Girl.class);
141         while (it.hasNext()) {
142             girl = (Girl) it.next();
143         }
144         checkGirl3(girl);
145
146         it = q.iterate(Girl.class);
147         while (it.hasNext()) {
148             girl = (Girl) ((Object JavaDoc[]) it.next())[0];
149         }
150         checkGirl3(girl);
151
152         q.close();
153
154
155         // ---------------------------------------------------------------- boy
156

157         q = new DbOrmQuery("select * from BOY order by ID");
158
159         BadBoy badBoy = q.findOne(BadBoy.class);
160         checkBoy(badBoy);
161
162         badBoy = (BadBoy) (q.find(BadBoy.class))[0];
163         checkBoy(badBoy);
164
165         badBoy = (BadBoy) (q.find()[0]);
166         checkBoy(badBoy);
167
168
169         // list
170
List<BadBoy> listBadBoyt = q.listOne(BadBoy.class);
171         assertEquals(1, listBadBoyt.size());
172         badBoy = listBadBoyt.get(0);
173         checkBoy(badBoy);
174
175         list = q.list(BadBoy.class);
176         assertEquals(1, list.size());
177         badBoy = (BadBoy) ((Object JavaDoc[]) list.get(0))[0];
178         checkBoy(badBoy);
179
180         list = q.list();
181         assertEquals(1, list.size());
182         badBoy = (BadBoy) ((Object JavaDoc[]) list.get(0))[0];
183         checkBoy(badBoy);
184
185
186
187         // set
188
Set<BadBoy> setBadBoy = q.listSetOne(BadBoy.class);
189         assertEquals(1, setBadBoy.size());
190         badBoy = (setBadBoy.iterator().next());
191         checkBoy(badBoy);
192
193         set = q.listSet(BadBoy.class);
194         assertEquals(1, set.size());
195         badBoy = (BadBoy) ((Object JavaDoc[]) set.iterator().next())[0];
196         checkBoy(badBoy);
197
198         set = q.listSet();
199         assertEquals(1, set.size());
200         badBoy = (BadBoy) ((Object JavaDoc[]) set.iterator().next())[0];
201         checkBoy(badBoy);
202
203         // iterator
204
it = q.iterateOne(BadBoy.class);
205         while (it.hasNext()) {
206             badBoy = (BadBoy) it.next();
207         }
208         checkBoy(badBoy);
209
210         it = q.iterate(BadBoy.class);
211         while (it.hasNext()) {
212             badBoy = (BadBoy) ((Object JavaDoc[]) it.next())[0];
213         }
214         checkBoy(badBoy);
215
216         it = q.iterate();
217         while (it.hasNext()) {
218             badBoy = (BadBoy) ((Object JavaDoc[]) it.next())[0];
219         }
220         checkBoy(badBoy);
221
222         q.close();
223
224
225         // ---------------------------------------------------------------- join
226

227         dbOrm.mapTable(BadGirl.class);
228
229         //q = new DbOrmQuery("select * from GIRL, BOY where BOY.GIRL_ID=GIRL.ID");
230
q = new DbOrmQuery("select * from GIRL join BOY on GIRL.ID=BOY.GIRL_ID");
231
232         badBoy = q.findOne(BadBoy.class);
233         assertNull(badBoy); // wrong mapping order, girl is first!
234

235         BadGirl badGirl = q.findOne(BadGirl.class);
236         checkBadGirl3(badGirl);
237
238         Object JavaDoc[] res = q.find(BadBoy.class, BadGirl.class);
239         badBoy = (BadBoy) res[0];
240         badGirl = (BadGirl) res[1];
241         checkBadGirl3(badGirl);
242         assertNull(badBoy); // order is invalid
243

244         res = q.find(BadGirl.class, BadBoy.class);
245         badBoy = (BadBoy) res[1];
246         badGirl = (BadGirl) res[0];
247         checkBadGirl3(badGirl);
248         checkBoy(badBoy);
249
250         res = q.find(Boy.class, Girl.class);
251         Boy boy = (Boy) res[0];
252         girl = (Girl) res[1];
253         assertNull(boy); // order is invalid
254
checkGirl3(girl);
255
256         res = q.find(Girl.class, Boy.class);
257         boy = (Boy) res[1];
258         girl = (Girl) res[0];
259         checkBoy(boy);
260         checkGirl3(girl);
261
262         res = q.find();
263         badGirl = (BadGirl) res[0];
264         badBoy = (BadBoy) res[1];
265         checkBadGirl3(badGirl);
266         checkBoy(badBoy);
267
268
269         // ---------------------------------------------------------------- left join
270

271         q = new DbOrmQuery("select * from GIRL left join BOY on GIRL.ID=BOY.GIRL_ID order by GIRL.ID");
272
273         list = q.list(Girl.class, Boy.class);
274         assertEquals(3, list.size());
275         checkGirl1((Girl) ((Object JavaDoc[])list.get(0))[0]);
276         assertNull(((Object JavaDoc[])list.get(0))[1]);
277
278         checkGirl2((Girl) ((Object JavaDoc[])list.get(1))[0]);
279         assertNull(((Object JavaDoc[])list.get(1))[1]);
280
281         checkGirl3((Girl) ((Object JavaDoc[])list.get(2))[0]);
282         checkBoy((Boy) ((Object JavaDoc[])list.get(2))[1]);
283
284
285         // ---------------------------------------------------------------- etc
286

287         badGirl = new BadGirl();
288         badBoy = new BadBoy();
289         DbDynamicSqlTemplate t = new DbDynamicSqlTemplate("select g.*, b.* from $T{g}, $T{b} where 1=1 $W{and g} $W{and b}").use("g", badGirl).use("b", badBoy);
290         q = new DbOrmQuery(t);
291         list = q.list(BadBoy.class, BadGirl.class);
292         assertEquals(3, list.size());
293
294         t = new DbDynamicSqlTemplate("select g.*, b.* from $T{g}, $T{b} $W{where g, b}").use("g", badGirl).use("b", badBoy);
295         q = new DbOrmQuery(t);
296         list = q.list(BadBoy.class, BadGirl.class);
297         assertEquals(3, list.size());
298
299         t = new DbDynamicSqlTemplate("select g.*, b.* from $T{g}, $T{b} $W{where b, g}").use("g", badGirl).use("b", badBoy);
300         q = new DbOrmQuery(t);
301         list = q.list(BadBoy.class, BadGirl.class);
302         assertEquals(3, list.size());
303
304
305
306
307         badGirl.fooname = "Sandra";
308         t = new DbDynamicSqlTemplate("select g.*, b.* from $T{g}, $T{b} where 1=1 $W{and b} $W{and g}").use("g", badGirl).use("b", badBoy);
309         q = new DbOrmQuery(t);
310         Object JavaDoc[] result = q.find(BadGirl.class, BadBoy.class);
311         checkBoy((BadBoy) result[1]);
312         checkBadGirl2((BadGirl) result[0]);
313
314         t = new DbDynamicSqlTemplate("select g.*, b.* from $T{g}, $T{b} $W{where b, g}").use("g", badGirl).use("b", badBoy);
315         q = new DbOrmQuery(t);
316         result = q.find(BadGirl.class, BadBoy.class);
317         checkBoy((BadBoy) result[1]);
318         checkBadGirl2((BadGirl) result[0]);
319
320         t = new DbDynamicSqlTemplate("select b.*, g.* from $T{g}, $T{b} $W{where g, b}").use("g", badGirl).use("b", badBoy);
321         q = new DbOrmQuery(t);
322         result = q.find(BadBoy.class, BadGirl.class);
323         checkBoy((BadBoy) result[0]);
324         checkBadGirl2((BadGirl) result[1]);
325
326
327
328         badBoy.ajdi = Integer.valueOf(1);
329         badBoy.nejm = "Johny";
330         t = new DbDynamicSqlTemplate("select b.*, g.* from $T{g}, $T{b} where 1=1 $W{and g} $W{and b}").use("g", badGirl).use("b", badBoy);
331         q = new DbOrmQuery(t);
332         result = q.find(BadBoy.class, BadGirl.class);
333         checkBoy((BadBoy) result[0]);
334         checkBadGirl2((BadGirl) result[1]);
335
336         t = new DbDynamicSqlTemplate("select b.*, g.* from $T{g}, $T{b} $W{where g, b}").use("g", badGirl).use("b", badBoy);
337         q = new DbOrmQuery(t);
338         result = q.find(BadBoy.class, BadGirl.class);
339         checkBoy((BadBoy) result[0]);
340         checkBadGirl2((BadGirl) result[1]);
341
342         t = new DbDynamicSqlTemplate("select b.*, g.* from $T{g}, $T{b} $W{where b, g}").use("g", badGirl).use("b", badBoy);
343         q = new DbOrmQuery(t);
344         result = q.find(BadBoy.class, BadGirl.class);
345         checkBoy((BadBoy) result[0]);
346         checkBadGirl2((BadGirl) result[1]);
347
348
349         t = new DbDynamicSqlTemplate("select $C{g.fooid}, $C{+b.*} from $T{g}, $T{b} $W{where g, b}").use("g", badGirl).use("b", badBoy);
350         q = new DbOrmQuery(t);
351         result = q.find(BadGirl.class, BadBoy.class);
352         badGirl = (BadGirl) result[0];
353         checkBoy((BadBoy) result[1]);
354         assertEquals(2, badGirl.fooid.intValue());
355         assertNull(badGirl.fooname);
356         assertNull(badGirl.foospeciality);
357
358
359         // ---------------------------------------------------------------- special
360

361         t = new DbDynamicSqlTemplate("select $g.fooid * 2 as did, $C{g.fooid} from $T{g} order by $g.fooid").setColumnAliasesType(COLUMN_CODE).use("g", BadGirl.class);
362         q = new DbOrmQuery(t);
363         list = q.list(null, BadGirl.class); // explicitly ignore the first column 'did'
364
assertEquals(3, list.size());
365         assertEquals(1, ((BadGirl)((Object JavaDoc[])list.get(0))[1]).fooid.intValue());
366         assertEquals(2, ((BadGirl)((Object JavaDoc[])list.get(1))[1]).fooid.intValue());
367         assertEquals(3, ((BadGirl)((Object JavaDoc[])list.get(2))[1]).fooid.intValue());
368
369         list = q.list(Integer JavaDoc.class, BadGirl.class);
370         assertEquals(3, list.size());
371         assertEquals(2, ((Integer JavaDoc)((Object JavaDoc[])list.get(0))[0]).intValue());
372         assertEquals(1, ((BadGirl)((Object JavaDoc[])list.get(0))[1]).fooid.intValue());
373         assertEquals(4, ((Integer JavaDoc)((Object JavaDoc[])list.get(1))[0]).intValue());
374         assertEquals(2, ((BadGirl)((Object JavaDoc[])list.get(1))[1]).fooid.intValue());
375         assertEquals(6, ((Integer JavaDoc)((Object JavaDoc[])list.get(2))[0]).intValue());
376         assertEquals(3, ((BadGirl)((Object JavaDoc[])list.get(2))[1]).fooid.intValue());
377
378
379         q = new DbOrmQuery("select g.ID * 2 as did, g.ID from Girl g order by g.ID");
380         list = q.list(Integer JavaDoc.class, BadGirl.class);
381         assertEquals(3, list.size());
382         assertEquals(2, ((Integer JavaDoc)((Object JavaDoc[])list.get(0))[0]).intValue());
383         assertEquals(1, ((BadGirl)((Object JavaDoc[])list.get(0))[1]).fooid.intValue());
384         assertEquals(4, ((Integer JavaDoc)((Object JavaDoc[])list.get(1))[0]).intValue());
385         assertEquals(2, ((BadGirl)((Object JavaDoc[])list.get(1))[1]).fooid.intValue());
386         assertEquals(6, ((Integer JavaDoc)((Object JavaDoc[])list.get(2))[0]).intValue());
387         assertEquals(3, ((BadGirl)((Object JavaDoc[])list.get(2))[1]).fooid.intValue());
388
389
390         q = new DbOrmQuery(new DbSqlTemplate("select $g.id+$b.id as total, $C{g.*}, $g.id*2 as gdub, $C{b.*}, $g.id/3.0, $g.name from $T{g}, $T{b} where $b.girlId=$g.id").
391                 setColumnAliasesType(COLUMN_CODE).use("b", Boy.class).use("g", Girl.class));
392         list = q.list(Integer JavaDoc.class, Girl.class, Long JavaDoc.class, Boy.class, Float JavaDoc.class, String JavaDoc.class);
393         assertEquals(1, list.size());
394         result = (Object JavaDoc[]) list.get(0);
395         assertEquals(6, result.length);
396         assertEquals(4, ((Integer JavaDoc) result[0]).intValue());
397         checkGirl3((Girl) result[1]);
398         assertEquals(6, ((Long JavaDoc) result[2]).intValue());
399         checkBoy((Boy) result[3]);
400         //noinspection AutoUnboxing
401
assertEquals(1.0, (Float JavaDoc) result[4], 0.05);
402         assertEquals("Monica", (String JavaDoc) result[5]);
403
404
405         q = new DbOrmQuery(new DbSqlTemplate("select $C{g.*}, $C{g.*} from $T{g} where $g.id=3").setColumnAliasesType(COLUMN_CODE).use("g", Girl.class));
406         list = q.list(Girl.class, Girl.class);
407         assertEquals(1, list.size());
408         result = (Object JavaDoc[]) list.get(0);
409         checkGirl3((Girl) result[0]);
410         checkGirl3((Girl) result[1]);
411
412         q = new DbOrmQuery(new DbSqlTemplate("select $C{g.*}, $g.name from $T{g} where $g.id=3").setColumnAliasesType(COLUMN_CODE).use("g", Girl.class));
413         list = q.list(Girl.class, String JavaDoc.class);
414         assertEquals(1, list.size());
415         result = (Object JavaDoc[]) list.get(0);
416         checkGirl3((Girl) result[0]);
417         assertEquals("Monica", result[1]);
418
419         q = new DbOrmQuery(new DbSqlTemplate("select $g.name, $C{g.*} from $T{g} where $g.id=3").setColumnAliasesType(COLUMN_CODE).use("g", Girl.class));
420         list = q.list(String JavaDoc.class, Girl.class);
421         assertEquals(1, list.size());
422         result = (Object JavaDoc[]) list.get(0);
423         checkGirl3((Girl) result[1]);
424         assertEquals("Monica", (String JavaDoc) result[0]);
425
426
427
428         q.close();
429
430         // ---------------------------------------------------------------- finder
431

432         girl = new Girl();
433         badGirl = new BadGirl();
434         badBoy = new BadBoy();
435
436         DbOrmQuery f = new DbOrmQuery(new DbSqlFind(girl).columnAliases(false), new DbQueryMode().setDebug(true));
437         assertEquals("select t.NAME, t.SPECIALITY, t.ID from GIRL t where t.ID=0", f.toString());
438         f.close();
439         f = new DbOrmQuery(new DbSqlFind(badGirl).columnAliases(false), new DbQueryMode().setDebug(true));
440         assertEquals("select t.SPECIALITY, t.ID, t.NAME from GIRL t ", f.toString());
441         f.close();
442         f = new DbOrmQuery(new DbSqlFind(badBoy).columnAliases(false), new DbQueryMode().setDebug(true));
443         assertEquals("select t.NAME, t.GIRL_ID, t.ID from BOY t ", f.toString());
444         f.close();
445
446         girl.name = "Monica";
447         badGirl.fooname = "Anna";
448         badBoy.nejm = "David";
449
450         f = new DbOrmQuery(new DbSqlFind(girl).columnAliases(false), new DbQueryMode().setDebug(true));
451         assertEquals("select t.NAME, t.SPECIALITY, t.ID from GIRL t where t.NAME='Monica' and t.ID=0", f.toString());
452         f.close();
453         f = new DbOrmQuery(new DbSqlFind(badGirl).columnAliases(false), new DbQueryMode().setDebug(true));
454         assertEquals("select t.SPECIALITY, t.ID, t.NAME from GIRL t where t.NAME='Anna'", f.toString());
455         f.close();
456         f = new DbOrmQuery(new DbSqlFind(badBoy).columnAliases(false), new DbQueryMode().setDebug(true));
457         assertEquals("select t.NAME, t.GIRL_ID, t.ID from BOY t where t.NAME='David'", f.toString());
458         f.close();
459
460         badBoy.nejm = null;
461         badBoy.nejmLike = "Jo%";
462         f = new DbOrmQuery(new DbSqlFind(badBoy).columnAliases(false), new DbQueryMode().setDebug(true));
463         assertEquals("select t.NAME, t.GIRL_ID, t.ID from BOY t where t.NAME like 'Jo%'", f.toString());
464         f.close();
465         badBoy.ajdiGreaterEqual = Integer.valueOf(3);
466         f = new DbOrmQuery(new DbSqlFind(badBoy).columnAliases(false), new DbQueryMode().setDebug(true));
467         assertEquals("select t.NAME, t.GIRL_ID, t.ID from BOY t where t.ID>=3 and t.NAME like 'Jo%'", f.toString());
468         f.close();
469
470         BadGirlFinder badGirlFinder = new BadGirlFinder();
471         badGirlFinder.fooidGreaterEqual = Integer.valueOf(2);
472         badGirlFinder.foospecialityLike = "s%";
473         f = new DbOrmQuery(new DbSqlFind(badGirlFinder).columnAliases(false), new DbQueryMode().setDebug(true));
474         assertEquals("select t.SPECIALITY, t.ID, t.NAME from GIRL t where t.ID>=2 and t.SPECIALITY like 's%'", f.toString());
475         f.close();
476
477
478         // ---------------------------------------------------------------- whole round
479

480         badGirl = new BadGirl();
481         badGirl.fooid = Integer.valueOf(2);
482         f = new DbOrmQuery(new DbSqlFind(badGirl));
483         list = f.listOneAndClose(BadGirl.class);
484         assertTrue(f.isClosed());
485         assertEquals(1, list.size());
486         checkBadGirl2((BadGirl) list.get(0));
487
488         f = new DbOrmQuery(new DbSqlCount(badGirl));
489         int count = (int) f.executeCountAndClose();
490         assertEquals(1, count);
491         assertTrue(f.isClosed());
492
493         f = new DbOrmQuery(new DbSqlDelete(badGirl));
494         f.executeUpdate();
495         assertTrue(f.isClosed());
496
497         f = new DbOrmQuery(new DbSqlFind("select count(*)", badGirl));
498         count = (int) f.executeCountAndClose();
499         assertEquals(0, count);
500         assertTrue(f.isClosed());
501
502         badGirl.fooid = null;
503         f = new DbOrmQuery(new DbSqlFind("select count(*)", badGirl));
504         count = (int) f.executeCountAndClose();
505         assertEquals(2, count);
506         assertTrue(f.isClosed());
507
508         girl = new Girl();
509         girl.id = 1;
510         girl.name = "A%";
511         f = new DbOrmQuery("select * from GIRL where id >= :girl.id and name like :girl.name", new DbQueryMode().debug());
512         f.setBean("girl", girl);
513         assertEquals("select * from GIRL where id >= 1 and name like 'A%'", f.toString());
514         count = (int) f.executeCountAndClose();
515         assertEquals(1, count);
516
517
518
519         // ---------------------------------------------------------------- update
520

521         badGirl = new BadGirl();
522         badGirl.fooid = Integer.valueOf(1);
523         badGirl = new DbOrmQuery(new DbSqlFind(badGirl)).findOne(badGirl.getClass());
524         checkBadGirl1(badGirl);
525
526         badGirl.fooname = "Ticky";
527         new DbOrmQuery(new DbSqlUpdate(badGirl, true, new BadGirl(Integer.valueOf(1), null, null))).executeUpdate();
528
529         badGirl = new BadGirl();
530         badGirl.fooid = Integer.valueOf(1);
531         badGirl = new DbOrmQuery(new DbSqlFind(badGirl)).findOne(badGirl.getClass());
532         checkBadGirl1Alt(badGirl);
533
534         badGirl.foospeciality = null;
535         new DbOrmQuery(new DbSqlUpdate(badGirl, false, new BadGirl(Integer.valueOf(1), null, null))).executeUpdate();
536
537         badGirl = new BadGirl();
538         badGirl.fooid = Integer.valueOf(1);
539         badGirl = new DbOrmQuery(new DbSqlFind(badGirl)).findOne(badGirl.getClass());
540         checkBadGirl1Alt2(badGirl);
541
542         session.closeSession();
543
544     }
545
546     // ---------------------------------------------------------------- utitlities
547

548     private void checkGirl1(Girl girl) {
549         assertNotNull(girl);
550         assertEquals(1, girl.id);
551         assertEquals("Anna", girl.name);
552         assertEquals("seduction", girl.speciality);
553     }
554
555     private void checkGirl2(Girl girl) {
556         assertNotNull(girl);
557         assertEquals(2, girl.id);
558         assertEquals("Sandra", girl.name);
559         assertEquals("spying", girl.speciality);
560     }
561
562     private void checkGirl3(Girl girl) {
563         assertNotNull(girl);
564         assertEquals(3, girl.id);
565         assertEquals("Monica", girl.name);
566         assertNull(girl.speciality);
567     }
568
569     private void checkBadGirl1(BadGirl girl) {
570         assertNotNull(girl);
571         assertEquals(1, girl.fooid.intValue());
572         assertEquals("Anna", girl.fooname);
573         assertEquals("seduction", girl.foospeciality);
574     }
575     private void checkBadGirl1Alt(BadGirl girl) {
576         assertNotNull(girl);
577         assertEquals(1, girl.fooid.intValue());
578         assertEquals("Ticky", girl.fooname);
579         assertEquals("seduction", girl.foospeciality);
580     }
581     private void checkBadGirl1Alt2(BadGirl girl) {
582         assertNotNull(girl);
583         assertEquals(1, girl.fooid.intValue());
584         assertEquals("Ticky", girl.fooname);
585         assertNull(girl.foospeciality);
586     }
587
588
589     private void checkBadGirl2(BadGirl girl) {
590         assertNotNull(girl);
591         assertEquals(2, girl.fooid.intValue());
592         assertEquals("Sandra", girl.fooname);
593         assertEquals("spying", girl.foospeciality);
594     }
595
596
597     private void checkBadGirl3(BadGirl girl) {
598         assertNotNull(girl);
599         assertEquals(3, girl.fooid.intValue());
600         assertEquals("Monica", girl.fooname);
601         assertNull(girl.foospeciality);
602     }
603
604
605     private void checkBoy(Boy boy) {
606         assertNotNull(boy);
607         assertEquals(1, boy.id);
608         assertEquals("Johny", boy.name);
609         assertEquals(3, boy.girlId);
610     }
611     private void checkBoy(BadBoy boy) {
612         assertNotNull(boy);
613         assertEquals(1, boy.ajdi.intValue());
614         assertEquals("Johny", boy.nejm);
615         assertEquals(3, boy.girlId.intValue());
616     }
617 }
618
Popular Tags