~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/legacy/FumTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: FumTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
 
2
package org.hibernate.test.legacy;
 
3
 
 
4
import java.io.ByteArrayInputStream;
 
5
import java.io.ByteArrayOutputStream;
 
6
import java.io.IOException;
 
7
import java.io.ObjectInputStream;
 
8
import java.io.ObjectOutputStream;
 
9
import java.io.Serializable;
 
10
import java.sql.SQLException;
 
11
import java.util.ArrayList;
 
12
import java.util.Date;
 
13
import java.util.GregorianCalendar;
 
14
import java.util.HashSet;
 
15
import java.util.Iterator;
 
16
import java.util.LinkedList;
 
17
import java.util.List;
 
18
import java.util.Map;
 
19
import java.util.Properties;
 
20
import java.util.Set;
 
21
 
 
22
import junit.framework.Test;
 
23
import junit.textui.TestRunner;
 
24
 
 
25
import org.hibernate.Criteria;
 
26
import org.hibernate.FetchMode;
 
27
import org.hibernate.FlushMode;
 
28
import org.hibernate.Hibernate;
 
29
import org.hibernate.HibernateException;
 
30
import org.hibernate.LockMode;
 
31
import org.hibernate.Query;
 
32
import org.hibernate.Transaction;
 
33
import org.hibernate.classic.Session;
 
34
import org.hibernate.criterion.Expression;
 
35
import org.hibernate.criterion.MatchMode;
 
36
import org.hibernate.dialect.HSQLDialect;
 
37
import org.hibernate.dialect.MckoiDialect;
 
38
import org.hibernate.dialect.MySQLDialect;
 
39
import org.hibernate.dialect.PointbaseDialect;
 
40
import org.hibernate.dialect.TimesTenDialect;
 
41
import org.hibernate.dialect.Dialect;
 
42
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
 
43
import org.hibernate.transform.Transformers;
 
44
import org.hibernate.type.DateType;
 
45
import org.hibernate.type.EntityType;
 
46
import org.hibernate.type.StringType;
 
47
import org.hibernate.type.Type;
 
48
 
 
49
public class FumTest extends LegacyTestCase {
 
50
 
 
51
        private static short fumKeyShort = 1;
 
52
 
 
53
        public FumTest(String arg) {
 
54
                super(arg);
 
55
        }
 
56
 
 
57
        public String[] getMappings() {
 
58
                return new String[] {
 
59
                        "legacy/FooBar.hbm.xml",
 
60
                        "legacy/Baz.hbm.xml",
 
61
                        "legacy/Qux.hbm.xml",
 
62
                        "legacy/Glarch.hbm.xml",
 
63
                        "legacy/Fum.hbm.xml",
 
64
                        "legacy/Fumm.hbm.xml",
 
65
                        "legacy/Fo.hbm.xml",
 
66
                        "legacy/One.hbm.xml",
 
67
                        "legacy/Many.hbm.xml",
 
68
                        "legacy/Immutable.hbm.xml",
 
69
                        "legacy/Fee.hbm.xml",
 
70
                        "legacy/Vetoer.hbm.xml",
 
71
                        "legacy/Holder.hbm.xml",
 
72
                        "legacy/Location.hbm.xml",
 
73
                        "legacy/Stuff.hbm.xml",
 
74
                        "legacy/Container.hbm.xml",
 
75
                        "legacy/Simple.hbm.xml",
 
76
                        "legacy/Middle.hbm.xml"
 
77
                };
 
78
        }
 
79
 
 
80
        public static Test suite() {
 
81
                return new FunctionalTestClassTestSuite( FumTest.class );
 
82
        }
 
83
 
 
84
        public static void main(String[] args) throws Exception {
 
85
                TestRunner.run( suite() );
 
86
        }
 
87
        
 
88
        public void testQuery() {
 
89
                Session s = openSession();
 
90
                Transaction t = s.beginTransaction();
 
91
                s.createQuery("from Fum fum where fum.fo.id.string = 'x'").list();
 
92
                t.commit();
 
93
                s.close();
 
94
        }
 
95
 
 
96
        public void testCriteriaCollection() throws Exception {
 
97
                Session s = openSession();
 
98
                Fum fum = new Fum( fumKey("fum") );
 
99
                fum.setFum("a value");
 
100
                fum.getMapComponent().getFummap().put("self", fum);
 
101
                fum.getMapComponent().getStringmap().put("string", "a staring");
 
102
                fum.getMapComponent().getStringmap().put("string2", "a notha staring");
 
103
                fum.getMapComponent().setCount(1);
 
104
                s.save(fum);
 
105
                s.flush();
 
106
                s.connection().commit();
 
107
                s.close();
 
108
                s = openSession();
 
109
                Fum b = (Fum) s.createCriteria(Fum.class).add(
 
110
                        Expression.in("fum", new String[] { "a value", "no value" } )
 
111
                )
 
112
                .uniqueResult();
 
113
                //assertTrue( Hibernate.isInitialized( b.getMapComponent().getFummap() ) );
 
114
                assertTrue( Hibernate.isInitialized( b.getMapComponent().getStringmap() ) );
 
115
                assertTrue( b.getMapComponent().getFummap().size()==1 );
 
116
                assertTrue( b.getMapComponent().getStringmap().size()==2 );
 
117
                s.delete(b);
 
118
                s.flush();
 
119
                s.connection().commit();
 
120
                s.close();
 
121
        }
 
122
 
 
123
        public void testCriteria() throws Exception {
 
124
                Session s = openSession();
 
125
                Transaction txn = s.beginTransaction();
 
126
                Fum fum = new Fum( fumKey("fum") );
 
127
                fum.setFo( new Fum( fumKey("fo") ) );
 
128
                fum.setFum("fo fee fi");
 
129
                fum.getFo().setFum("stuff");
 
130
                Fum fr = new Fum( fumKey("fr") );
 
131
                fr.setFum("goo");
 
132
                Fum fr2 = new Fum( fumKey("fr2") );
 
133
                fr2.setFum("soo");
 
134
                fum.setFriends( new HashSet() );
 
135
                fum.getFriends().add(fr);
 
136
                fum.getFriends().add(fr2);
 
137
                s.save(fr);
 
138
                s.save(fr2);
 
139
                s.save( fum.getFo() );
 
140
                s.save(fum);
 
141
 
 
142
                Criteria base = s.createCriteria(Fum.class)
 
143
                        .add( Expression.like("fum", "f", MatchMode.START) );
 
144
                base.createCriteria("fo")
 
145
                        .add( Expression.isNotNull("fum") );
 
146
                base.createCriteria("friends")
 
147
                        .add( Expression.like("fum", "g%") );
 
148
                List list = base.list();
 
149
                assertTrue( list.size()==1 && list.get(0)==fum );
 
150
 
 
151
                base = s.createCriteria(Fum.class)
 
152
                        .add( Expression.like("fum", "f%") )
 
153
                        .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
 
154
                base.createCriteria("fo", "fo")
 
155
                        .add( Expression.isNotNull("fum") );
 
156
                base.createCriteria("friends", "fum")
 
157
                        .add( Expression.like("fum", "g", MatchMode.START) );
 
158
                Map map = (Map) base.uniqueResult();
 
159
 
 
160
                assertTrue(
 
161
                        map.get("this")==fum &&
 
162
                        map.get("fo")==fum.getFo() &&
 
163
                        fum.getFriends().contains( map.get("fum") ) &&
 
164
                        map.size()==3
 
165
                );
 
166
 
 
167
                base = s.createCriteria(Fum.class)
 
168
                        .add( Expression.like("fum", "f%") )
 
169
                        .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
 
170
                        .setFetchMode("friends", FetchMode.EAGER);
 
171
                base.createCriteria("fo", "fo")
 
172
                        .add( Expression.eq( "fum", fum.getFo().getFum() ) );
 
173
                map = (Map) base.list().get(0);
 
174
 
 
175
                assertTrue(
 
176
                        map.get("this")==fum &&
 
177
                        map.get("fo")==fum.getFo() &&
 
178
                        map.size()==2
 
179
                );
 
180
 
 
181
                list = s.createCriteria(Fum.class)
 
182
                        .createAlias("friends", "fr")
 
183
                        .createAlias("fo", "fo")
 
184
                        .add( Expression.like("fum", "f%") )
 
185
                        .add( Expression.isNotNull("fo") )
 
186
                        .add( Expression.isNotNull("fo.fum") )
 
187
                        .add( Expression.like("fr.fum", "g%") )
 
188
                        .add( Expression.eqProperty("fr.id.short", "id.short") )
 
189
                        .list();
 
190
                assertTrue( list.size()==1 && list.get(0)==fum );
 
191
                txn.commit();
 
192
                s.close();
 
193
 
 
194
                s = openSession();
 
195
                txn = s.beginTransaction();
 
196
                base = s.createCriteria(Fum.class)
 
197
                        .add( Expression.like("fum", "f%") );
 
198
                base.createCriteria("fo")
 
199
                        .add( Expression.isNotNull("fum") );
 
200
                base.createCriteria("friends")
 
201
                        .add( Expression.like("fum", "g%") );
 
202
                fum = (Fum) base.list().get(0);
 
203
                assertTrue(  fum.getFriends().size()==2 );
 
204
                s.delete(fum);
 
205
                s.delete( fum.getFo() );
 
206
                Iterator iter = fum.getFriends().iterator();
 
207
                while ( iter.hasNext() ) s.delete( iter.next() );
 
208
                txn.commit();
 
209
                s.close();
 
210
        }
 
211
 
 
212
        static public class ABean {
 
213
                public Fum fum;
 
214
                public Fum fo;
 
215
                public Fum getFo() {
 
216
                        return fo;
 
217
                }
 
218
                public void setFo(Fum fo) {
 
219
                        this.fo = fo;
 
220
                }
 
221
                public Fum getFum() {
 
222
                        return fum;
 
223
                }
 
224
                public void setFum(Fum fum) {
 
225
                        this.fum = fum;
 
226
                }
 
227
        }
 
228
        
 
229
        public void testBeanResultTransformer() throws HibernateException, SQLException {
 
230
                
 
231
                Session s = openSession();
 
232
                Transaction transaction = s.beginTransaction();
 
233
                Fum fum = new Fum( fumKey("fum") );
 
234
                fum.setFo( new Fum( fumKey("fo") ) );
 
235
                fum.setFum("fo fee fi");
 
236
                fum.getFo().setFum("stuff");
 
237
                Fum fr = new Fum( fumKey("fr") );
 
238
                fr.setFum("goo");
 
239
                Fum fr2 = new Fum( fumKey("fr2") );
 
240
                fr2.setFum("soo");
 
241
                fum.setFriends( new HashSet() );
 
242
                fum.getFriends().add(fr);
 
243
                fum.getFriends().add(fr2);
 
244
                s.save(fr);
 
245
                s.save(fr2);
 
246
                s.save( fum.getFo() );
 
247
                s.save(fum);
 
248
                
 
249
                Criteria test = s.createCriteria(Fum.class, "xam")
 
250
                        .createCriteria("fo", "fo")
 
251
                        .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
 
252
                
 
253
                Map fc = (Map) test.list().get(0);
 
254
                assertNotNull(fc.get("xam"));
 
255
                
 
256
                Criteria base = s.createCriteria(Fum.class, "fum")
 
257
                .add( Expression.like("fum", "f%") )
 
258
                .setResultTransformer(Transformers.aliasToBean(ABean.class))
 
259
                .setFetchMode("friends", FetchMode.JOIN);
 
260
                base.createCriteria("fo", "fo")
 
261
                .add( Expression.eq( "fum", fum.getFo().getFum() ) );
 
262
                ABean map = (ABean) base.list().get(0);
 
263
 
 
264
                assertTrue(
 
265
                                map.getFum()==fum &&
 
266
                                map.getFo()==fum.getFo() );
 
267
                
 
268
                s.delete(fr);
 
269
                s.delete(fr2);
 
270
                s.delete(fum);
 
271
                s.delete(fum.getFo());
 
272
                s.flush();
 
273
                transaction.commit();
 
274
                s.close();
 
275
                
 
276
        }
 
277
        
 
278
        
 
279
        public void testListIdentifiers() throws Exception {
 
280
                Session s = openSession();
 
281
                Transaction txn = s.beginTransaction();
 
282
                Fum fum = new Fum( fumKey("fum") );
 
283
                fum.setFum("fo fee fi");
 
284
                s.save(fum);
 
285
                fum = new Fum( fumKey("fi") );
 
286
                fum.setFum("fee fi fo");
 
287
                s.save(fum);
 
288
                List list = s.find("select fum.id from Fum as fum where not fum.fum='FRIEND'");
 
289
                assertTrue( "list identifiers", list.size()==2);
 
290
                Iterator iter = s.iterate("select fum.id from Fum fum where not fum.fum='FRIEND'");
 
291
                int i=0;
 
292
                while ( iter.hasNext() ) {
 
293
                        assertTrue( "iterate identifiers",  iter.next() instanceof FumCompositeID);
 
294
                        i++;
 
295
                }
 
296
                assertTrue(i==2);
 
297
 
 
298
                s.delete( s.load(Fum.class, (Serializable) list.get(0) ) );
 
299
                s.delete( s.load(Fum.class, (Serializable) list.get(1) ) );
 
300
                txn.commit();
 
301
                s.close();
 
302
        }
 
303
 
 
304
 
 
305
        public FumCompositeID fumKey(String str) {
 
306
 
 
307
                return fumKey(str,false);
 
308
        }
 
309
 
 
310
        private FumCompositeID fumKey(String str, boolean aCompositeQueryTest) {
 
311
                FumCompositeID id = new FumCompositeID();
 
312
                if ( Dialect.getDialect() instanceof MckoiDialect ) {
 
313
                        GregorianCalendar now = new GregorianCalendar();
 
314
                        GregorianCalendar cal = new GregorianCalendar(
 
315
                                now.get(java.util.Calendar.YEAR),
 
316
                                now.get(java.util.Calendar.MONTH),
 
317
                                now.get(java.util.Calendar.DATE)
 
318
                        );
 
319
                        id.setDate( cal.getTime() );
 
320
                }
 
321
                else {
 
322
                        id.setDate( new Date() );
 
323
                }
 
324
                id.setString( new String(str) );
 
325
 
 
326
                if (aCompositeQueryTest) {
 
327
                        id.setShort( fumKeyShort++ );
 
328
                }
 
329
                else {
 
330
                        id.setShort( (short) 12 );
 
331
                }
 
332
 
 
333
                return id;
 
334
        }
 
335
 
 
336
        public void testCompositeID() throws Exception {
 
337
                Session s = openSession();
 
338
                Transaction txn = s.beginTransaction();
 
339
                Fum fum = new Fum( fumKey("fum") );
 
340
                fum.setFum("fee fi fo");
 
341
                s.save(fum);
 
342
                assertTrue( "load by composite key", fum==s.load( Fum.class, fumKey("fum") ) );
 
343
                txn.commit();
 
344
                s.close();
 
345
 
 
346
                s = openSession();
 
347
                txn = s.beginTransaction();
 
348
                fum = (Fum) s.load( Fum.class, fumKey("fum"), LockMode.UPGRADE );
 
349
                assertTrue( "load by composite key", fum!=null );
 
350
 
 
351
                Fum fum2 = new Fum( fumKey("fi") );
 
352
                fum2.setFum("fee fo fi");
 
353
                fum.setFo(fum2);
 
354
                s.save(fum2);
 
355
                assertTrue(
 
356
                        "find composite keyed objects",
 
357
                        s.find("from Fum fum where not fum.fum='FRIEND'").size()==2
 
358
                );
 
359
                assertTrue(
 
360
                        "find composite keyed object",
 
361
                        s.find("select fum from Fum fum where fum.fum='fee fi fo'").get(0)==fum
 
362
                );
 
363
                fum.setFo(null);
 
364
                txn.commit();
 
365
                s.close();
 
366
 
 
367
                s = openSession();
 
368
                txn = s.beginTransaction();
 
369
                Iterator iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");
 
370
                int i = 0;
 
371
                while ( iter.hasNext() ) {
 
372
                        fum = (Fum) iter.next();
 
373
                        //iter.remove();
 
374
                        s.delete(fum);
 
375
                        i++;
 
376
                }
 
377
                assertTrue( "iterate on composite key", i==2 );
 
378
                txn.commit();
 
379
                s.close();
 
380
        }
 
381
 
 
382
        public void testCompositeIDOneToOne() throws Exception {
 
383
                Session s = openSession();
 
384
                Transaction txn = s.beginTransaction();
 
385
                Fum fum = new Fum( fumKey("fum") );
 
386
                fum.setFum("fee fi fo");
 
387
                //s.save(fum);
 
388
                Fumm fumm = new Fumm();
 
389
                fumm.setFum(fum);
 
390
                s.save(fumm);
 
391
                txn.commit();
 
392
                s.close();
 
393
 
 
394
                s = openSession();
 
395
                txn = s.beginTransaction();
 
396
                fumm = (Fumm) s.load( Fumm.class, fumKey("fum") );
 
397
                //s.delete( fumm.getFum() );
 
398
                s.delete(fumm);
 
399
                txn.commit();
 
400
                s.close();
 
401
        }
 
402
 
 
403
        public void testCompositeIDQuery() throws Exception {
 
404
                Session s = openSession();
 
405
                Fum fee = new Fum( fumKey("fee",true) );
 
406
                fee.setFum("fee");
 
407
                s.save(fee);
 
408
                Fum fi = new Fum( fumKey("fi",true) );
 
409
                fi.setFum("fi");
 
410
                short fiShort = fi.getId().getShort();
 
411
                s.save(fi);
 
412
                Fum fo = new Fum( fumKey("fo",true) );
 
413
                fo.setFum("fo");
 
414
                s.save(fo);
 
415
                Fum fum = new Fum( fumKey("fum",true) );
 
416
                fum.setFum("fum");
 
417
                s.save(fum);
 
418
                s.flush();
 
419
                s.connection().commit();
 
420
                s.close();
 
421
 
 
422
                s = openSession();
 
423
                // Try to find the Fum object "fo" that we inserted searching by the string in the id
 
424
                List vList = s.find("from Fum fum where fum.id.string='fo'"  );
 
425
                assertTrue( "find by composite key query (find fo object)", vList.size() == 1 );
 
426
                fum = (Fum)vList.get(0);
 
427
                assertTrue( "find by composite key query (check fo object)", fum.getId().getString().equals("fo") );
 
428
 
 
429
                // Try to find the Fum object "fi" that we inserted searching by the date in the id
 
430
                vList = s.find("from Fum fum where fum.id.short = ?",new Short(fiShort),Hibernate.SHORT);
 
431
                assertTrue( "find by composite key query (find fi object)", vList.size() == 1 );
 
432
                fi = (Fum)vList.get(0);
 
433
                assertTrue( "find by composite key query (check fi object)", fi.getId().getString().equals("fi") );
 
434
 
 
435
                // Make sure we can return all of the objects by searching by the date id
 
436
                assertTrue(
 
437
                        "find by composite key query with arguments",
 
438
                        s.find("from Fum fum where fum.id.date <= ? and not fum.fum='FRIEND'",new Date(),Hibernate.DATE).size()==4
 
439
                );
 
440
                s.flush();
 
441
                s.connection().commit();
 
442
                s.close();
 
443
 
 
444
                s = openSession();
 
445
                assertTrue(
 
446
                        s.iterate("select fum.id.short, fum.id.date, fum.id.string from Fum fum").hasNext()
 
447
                );
 
448
                assertTrue(
 
449
                        s.iterate("select fum.id from Fum fum").hasNext()
 
450
                );
 
451
                Query qu = s.createQuery("select fum.fum, fum , fum.fum, fum.id.date from Fum fum");
 
452
                Type[] types = qu.getReturnTypes();
 
453
                assertTrue(types.length==4);
 
454
                for ( int k=0; k<types.length; k++) {
 
455
                        assertTrue( types[k]!=null );
 
456
                }
 
457
                assertTrue(types[0] instanceof StringType);
 
458
                assertTrue(types[1] instanceof EntityType);
 
459
                assertTrue(types[2] instanceof StringType);
 
460
                assertTrue(types[3] instanceof DateType);
 
461
                Iterator iter = qu.iterate();
 
462
                int j = 0;
 
463
                while ( iter.hasNext() ) {
 
464
                        j++;
 
465
                        assertTrue( ( (Object[]) iter.next() )[1] instanceof Fum );
 
466
                }
 
467
                assertTrue( "iterate on composite key", j==8 );
 
468
 
 
469
                fum = (Fum) s.load( Fum.class, fum.getId() );
 
470
                s.filter( fum.getQuxArray(), "where this.foo is null" );
 
471
                s.filter( fum.getQuxArray(), "where this.foo.id = ?", "fooid", Hibernate.STRING );
 
472
                Query f = s.createFilter( fum.getQuxArray(), "where this.foo.id = :fooId" );
 
473
                f.setString("fooId", "abc");
 
474
                assertFalse( f.iterate().hasNext() );
 
475
 
 
476
                iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");
 
477
                int i = 0;
 
478
                while ( iter.hasNext() ) {
 
479
                        fum = (Fum) iter.next();
 
480
                        //iter.remove();
 
481
                        s.delete(fum);
 
482
                        i++;
 
483
                }
 
484
                assertTrue( "iterate on composite key", i==4 );
 
485
                s.flush();
 
486
 
 
487
                s.iterate("from Fum fu, Fum fo where fu.fo.id.string = fo.id.string and fo.fum is not null");
 
488
 
 
489
                s.find("from Fumm f1 inner join f1.fum f2");
 
490
 
 
491
                s.connection().commit();
 
492
                s.close();
 
493
        }
 
494
 
 
495
 
 
496
        public void testCompositeIDCollections() throws Exception {
 
497
                Session s = openSession();
 
498
                Fum fum1 = new Fum( fumKey("fum1") );
 
499
                Fum fum2 = new Fum( fumKey("fum2") );
 
500
                fum1.setFum("fee fo fi");
 
501
                fum2.setFum("fee fo fi");
 
502
                s.save(fum1);
 
503
                s.save(fum2);
 
504
                Qux q = new Qux();
 
505
                s.save(q);
 
506
                Set set = new HashSet();
 
507
                List list = new ArrayList();
 
508
                set.add(fum1); set.add(fum2);
 
509
                list.add(fum1);
 
510
                q.setFums(set);
 
511
                q.setMoreFums(list);
 
512
                fum1.setQuxArray( new Qux[] {q} );
 
513
                s.flush();
 
514
                s.connection().commit();
 
515
                s.close();
 
516
 
 
517
                s = openSession();
 
518
                q = (Qux) s.load( Qux.class, q.getKey() );
 
519
                assertTrue( "collection of fums", q.getFums().size()==2 );
 
520
                assertTrue( "collection of fums", q.getMoreFums().size()==1 );
 
521
                assertTrue( "unkeyed composite id collection", ( (Fum) q.getMoreFums().get(0) ).getQuxArray()[0]==q );
 
522
                Iterator iter = q.getFums().iterator();
 
523
                iter.hasNext();
 
524
                Fum f = (Fum) iter.next();
 
525
                s.delete(f);
 
526
                iter.hasNext();
 
527
                f = (Fum) iter.next();
 
528
                s.delete(f);
 
529
                s.delete(q);
 
530
                s.flush();
 
531
                s.connection().commit();
 
532
                s.close();
 
533
        }
 
534
 
 
535
 
 
536
        public void testDeleteOwner() throws Exception {
 
537
                Session s = openSession();
 
538
                Qux q = new Qux();
 
539
                s.save(q);
 
540
                Fum f1 = new Fum( fumKey("f1") );
 
541
                Fum f2 = new Fum( fumKey("f2") );
 
542
                Set set = new HashSet();
 
543
                set.add(f1);
 
544
                set.add(f2);
 
545
                List list = new LinkedList();
 
546
                list.add(f1);
 
547
                list.add(f2);
 
548
                f1.setFum("f1");
 
549
                f2.setFum("f2");
 
550
                q.setFums(set);
 
551
                q.setMoreFums(list);
 
552
                s.save(f1);
 
553
                s.save(f2);
 
554
                s.flush();
 
555
                s.connection().commit();
 
556
                s.close();
 
557
 
 
558
                s = openSession();
 
559
                q = (Qux) s.load( Qux.class, q.getKey(), LockMode.UPGRADE );
 
560
                s.lock( q, LockMode.UPGRADE );
 
561
                s.delete(q);
 
562
                s.flush();
 
563
                s.connection().commit();
 
564
                s.close();
 
565
 
 
566
                s = openSession();
 
567
                list = s.find("from Fum fum where not fum.fum='FRIEND'");
 
568
                assertTrue( "deleted owner", list.size()==2 );
 
569
                s.lock( list.get(0), LockMode.UPGRADE );
 
570
                s.lock( list.get(1), LockMode.UPGRADE );
 
571
                Iterator iter = list.iterator();
 
572
                while ( iter.hasNext() ) {
 
573
                        s.delete( iter.next() );
 
574
                }
 
575
                s.flush();
 
576
                s.connection().commit();
 
577
                s.close();
 
578
        }
 
579
 
 
580
 
 
581
        public void testCompositeIDs() throws Exception {
 
582
                Session s = openSession();
 
583
                Fo fo = Fo.newFo();
 
584
                Properties props = new Properties();
 
585
                props.setProperty("foo", "bar");
 
586
                props.setProperty("bar", "foo");
 
587
                fo.setSerial(props);
 
588
                fo.setBuf( "abcdefghij1`23%$*^*$*\n\t".getBytes() );
 
589
                s.save( fo, fumKey("an instance of fo") );
 
590
                s.flush();
 
591
                props.setProperty("x", "y");
 
592
                s.flush();
 
593
                s.connection().commit();
 
594
                s.close();
 
595
 
 
596
                s = openSession();
 
597
                fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") );
 
598
                props = (Properties) fo.getSerial();
 
599
                assertTrue( props.getProperty("foo").equals("bar") );
 
600
                //assertTrue( props.contains("x") );
 
601
                assertTrue( props.getProperty("x").equals("y") );
 
602
                assertTrue( fo.getBuf()[0]=='a' );
 
603
                fo.getBuf()[1]=(byte)126;
 
604
                s.flush();
 
605
                s.connection().commit();
 
606
                s.close();
 
607
 
 
608
                s = openSession();
 
609
                fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") );
 
610
                assertTrue( fo.getBuf()[1]==126 );
 
611
                assertTrue(
 
612
                        s.iterate("from Fo fo where fo.id.string like 'an instance of fo'").next()==fo
 
613
                );
 
614
                s.delete(fo);
 
615
                s.flush();
 
616
                try {
 
617
                        s.save( Fo.newFo() );
 
618
                        assertTrue(false);
 
619
                }
 
620
                catch (Exception e) {
 
621
                        //System.out.println( e.getMessage() );
 
622
                }
 
623
                s.connection().commit();
 
624
                s.close();
 
625
        }
 
626
 
 
627
        public void testKeyManyToOne() throws Exception {
 
628
                Session s = openSession();
 
629
                Inner sup = new Inner();
 
630
                InnerKey sid = new InnerKey();
 
631
                sup.setDudu("dudu");
 
632
                sid.setAkey("a");
 
633
                sid.setBkey("b");
 
634
                sup.setId(sid);
 
635
                Middle m = new Middle();
 
636
                MiddleKey mid = new MiddleKey();
 
637
                mid.setOne("one");
 
638
                mid.setTwo("two");
 
639
                mid.setSup(sup);
 
640
                m.setId(mid);
 
641
                m.setBla("bla");
 
642
                Outer d = new Outer();
 
643
                OuterKey did = new OuterKey();
 
644
                did.setMaster(m);
 
645
                did.setDetailId("detail");
 
646
                d.setId(did);
 
647
                d.setBubu("bubu");
 
648
                s.save(sup);
 
649
                s.save(m);
 
650
                s.save(d);
 
651
                s.flush();
 
652
                s.connection().commit();
 
653
                s.close();
 
654
 
 
655
                s = openSession();
 
656
                Inner in = (Inner) s.find("from Inner").get(0);
 
657
                assertTrue( in.getMiddles().size()==1 );
 
658
                s.flush();
 
659
                s.connection().commit();
 
660
                s.close();
 
661
                s = openSession();
 
662
                assertTrue( s.find("from Inner _inner join _inner.middles middle").size()==1 );
 
663
                s.flush();
 
664
                s.connection().commit();
 
665
                s.close();
 
666
 
 
667
                s = openSession();
 
668
                d = (Outer) s.load(Outer.class, did);
 
669
                assertTrue( d.getId().getMaster().getId().getSup().getDudu().equals("dudu") );
 
670
                s.delete(d);
 
671
                s.delete( d.getId().getMaster() );
 
672
                s.save( d.getId().getMaster() );
 
673
                s.save(d);
 
674
                s.flush();
 
675
                s.connection().commit();
 
676
                s.close();
 
677
 
 
678
                s = openSession();
 
679
                d = (Outer) s.find("from Outer o where o.id.detailId = ?", d.getId().getDetailId(), Hibernate.STRING ).get(0);
 
680
                s.find("from Outer o where o.id.master.id.sup.dudu is not null");
 
681
                s.find("from Outer o where o.id.master.id.sup.id.akey is not null");
 
682
                s.find("from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey");
 
683
                List l = s.find("select o.id.master.id.sup.dudu from Outer o where o.id.master.id.sup.dudu is not null");
 
684
                assertTrue(l.size()==1);
 
685
                l = s.find("select o.id.master.id.sup.id.akey from Outer o where o.id.master.id.sup.id.akey is not null");
 
686
                assertTrue(l.size()==1);
 
687
                s.find("select i.backOut.id.master.id.sup.id.akey from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey");
 
688
                s.find("from Outer o where o.id.master.bla = ''");
 
689
                s.find("from Outer o where o.id.master.id.one = ''");
 
690
                s.find("from Inner inn where inn.id.bkey is not null and inn.backOut.id.master.id.sup.id.akey > 'a'");
 
691
                s.find("from Outer as o left join o.id.master m left join m.id.sup where o.bubu is not null");
 
692
                s.find("from Outer as o left join o.id.master.id.sup s where o.bubu is not null");
 
693
                s.find("from Outer as o left join o.id.master m left join o.id.master.id.sup s where o.bubu is not null");
 
694
                s.delete(d);
 
695
                s.delete( d.getId().getMaster() );
 
696
                s.delete( d.getId().getMaster().getId().getSup() );
 
697
                s.flush();
 
698
                s.connection().commit();
 
699
                s.close();
 
700
        }
 
701
 
 
702
        public void testCompositeKeyPathExpressions() throws Exception {
 
703
                Session s = openSession();
 
704
                s.find("select fum1.fo from Fum fum1 where fum1.fo.fum is not null");
 
705
                s.find("from Fum fum1 where fum1.fo.fum is not null order by fum1.fo.fum");
 
706
                if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof PointbaseDialect) ) {
 
707
                        s.find("from Fum fum1 where exists elements(fum1.friends)");
 
708
                        if(!(getDialect() instanceof TimesTenDialect)) { // can't execute because TimesTen can't do subqueries combined with aggreations
 
709
                                s.find("from Fum fum1 where size(fum1.friends) = 0");
 
710
                        }
 
711
                }
 
712
                s.find("select elements(fum1.friends) from Fum fum1");
 
713
                s.find("from Fum fum1, fr in elements( fum1.friends )");
 
714
                s.connection().commit();
 
715
                s.close();
 
716
        }
 
717
 
 
718
        public void testUnflushedSessionSerialization() throws Exception {
 
719
 
 
720
                ///////////////////////////////////////////////////////////////////////////
 
721
                // Test insertions across serializations
 
722
                Session s = getSessions().openSession();
 
723
                s.setFlushMode(FlushMode.NEVER);
 
724
 
 
725
                Simple simple = new Simple();
 
726
                simple.setAddress("123 Main St. Anytown USA");
 
727
                simple.setCount(1);
 
728
                simple.setDate( new Date() );
 
729
                simple.setName("My UnflushedSessionSerialization Simple");
 
730
                simple.setPay( new Float(5000) );
 
731
                s.save( simple, new Long(10) );
 
732
 
 
733
                // Now, try to serialize session without flushing...
 
734
                s.disconnect();
 
735
                Session s2 = spoofSerialization(s);
 
736
                s.close();
 
737
                s = s2;
 
738
                s.reconnect();
 
739
 
 
740
                simple = (Simple) s.load( Simple.class, new Long(10) );
 
741
                Simple other = new Simple();
 
742
                other.init();
 
743
                s.save( other, new Long(11) );
 
744
 
 
745
                simple.setOther(other);
 
746
                s.flush();
 
747
 
 
748
                s.connection().commit();
 
749
                s.close();
 
750
                Simple check = simple;
 
751
 
 
752
                ///////////////////////////////////////////////////////////////////////////
 
753
                // Test updates across serializations
 
754
                s = getSessions().openSession();
 
755
                s.setFlushMode(FlushMode.NEVER);
 
756
 
 
757
                simple = (Simple) s.get( Simple.class, new Long(10) );
 
758
                assertTrue("Not same parent instances", check.getName().equals( simple.getName() ) );
 
759
                assertTrue("Not same child instances", check.getOther().getName().equals( other.getName() ) );
 
760
 
 
761
                simple.setName("My updated name");
 
762
 
 
763
                s.disconnect();
 
764
                s2 = spoofSerialization(s);
 
765
                s.close();
 
766
                s = s2;
 
767
                s.reconnect();
 
768
                s.flush();
 
769
 
 
770
                s.connection().commit();
 
771
                s.close();
 
772
                check = simple;
 
773
 
 
774
                ///////////////////////////////////////////////////////////////////////////
 
775
                // Test deletions across serializations
 
776
                s = getSessions().openSession();
 
777
                s.setFlushMode(FlushMode.NEVER);
 
778
 
 
779
                simple = (Simple) s.get( Simple.class, new Long(10) );
 
780
                assertTrue("Not same parent instances", check.getName().equals( simple.getName() ) );
 
781
                assertTrue("Not same child instances", check.getOther().getName().equals( other.getName() ) );
 
782
 
 
783
                // Now, lets delete across serialization...
 
784
                s.delete(simple);
 
785
 
 
786
                s.disconnect();
 
787
                s2 = spoofSerialization(s);
 
788
                s.close();
 
789
                s = s2;
 
790
                s.reconnect();
 
791
                s.flush();
 
792
 
 
793
                s.connection().commit();
 
794
                s.close();
 
795
 
 
796
                ///////////////////////////////////////////////////////////////////////////
 
797
                // Test collection actions across serializations
 
798
                s = getSessions().openSession();
 
799
                s.setFlushMode(FlushMode.NEVER);
 
800
 
 
801
                Fum fum = new Fum( fumKey("uss-fum") );
 
802
                fum.setFo( new Fum( fumKey("uss-fo") ) );
 
803
                fum.setFum("fo fee fi");
 
804
                fum.getFo().setFum("stuff");
 
805
                Fum fr = new Fum( fumKey("uss-fr") );
 
806
                fr.setFum("goo");
 
807
                Fum fr2 = new Fum( fumKey("uss-fr2") );
 
808
                fr2.setFum("soo");
 
809
                fum.setFriends( new HashSet() );
 
810
                fum.getFriends().add(fr);
 
811
                fum.getFriends().add(fr2);
 
812
                s.save(fr);
 
813
                s.save(fr2);
 
814
                s.save( fum.getFo() );
 
815
                s.save(fum);
 
816
 
 
817
                s.disconnect();
 
818
                s2 = spoofSerialization(s);
 
819
                s.close();
 
820
                s = s2;
 
821
                s.reconnect();
 
822
                s.flush();
 
823
 
 
824
                s.connection().commit();
 
825
                s.close();
 
826
 
 
827
                s = getSessions().openSession();
 
828
                s.setFlushMode(FlushMode.NEVER);
 
829
                fum = (Fum) s.load( Fum.class, fum.getId() );
 
830
 
 
831
                assertTrue("the Fum.friends did not get saved", fum.getFriends().size() == 2);
 
832
 
 
833
                fum.setFriends(null);
 
834
                s.disconnect();
 
835
                s2 = spoofSerialization(s);
 
836
                s.close();
 
837
                
 
838
                s = s2;
 
839
                s.reconnect();
 
840
                s.flush();
 
841
 
 
842
                s.connection().commit();
 
843
                s.close();
 
844
 
 
845
                s = getSessions().openSession();
 
846
                s.setFlushMode(FlushMode.NEVER);
 
847
                fum = (Fum) s.load( Fum.class, fum.getId() );
 
848
                assertTrue("the Fum.friends is not empty", fum.getFriends() == null || fum.getFriends().size() == 0);
 
849
                s.connection().commit();
 
850
                s.close();
 
851
        }
 
852
 
 
853
        private Session spoofSerialization(Session session) throws IOException {
 
854
                try {
 
855
                        // Serialize the incoming out to memory
 
856
                        ByteArrayOutputStream serBaOut = new ByteArrayOutputStream();
 
857
                        ObjectOutputStream serOut = new ObjectOutputStream(serBaOut);
 
858
 
 
859
                        serOut.writeObject(session);
 
860
 
 
861
                        // Now, re-constitute the model from memory
 
862
                        ByteArrayInputStream serBaIn =
 
863
                                new ByteArrayInputStream(serBaOut.toByteArray());
 
864
                        ObjectInputStream serIn = new ObjectInputStream(serBaIn);
 
865
 
 
866
                        Session outgoing = (Session) serIn.readObject();
 
867
 
 
868
                        return outgoing;
 
869
                }
 
870
                catch (ClassNotFoundException cnfe) {
 
871
                        throw new IOException("Unable to locate class on reconstruction");
 
872
                }
 
873
        }
 
874
 
 
875
}
 
876
 
 
877
 
 
878
 
 
879
 
 
880
 
 
881
 
 
882