KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > MasterDetailTest


1 //$Id: MasterDetailTest.java,v 1.11 2005/06/22 18:58:16 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18 import junit.textui.TestRunner;
19
20 import org.hibernate.Hibernate;
21 import org.hibernate.LockMode;
22 import org.hibernate.ObjectNotFoundException;
23 import org.hibernate.Query;
24 import org.hibernate.Transaction;
25 import org.hibernate.classic.Session;
26 import org.hibernate.criterion.Example;
27 import org.hibernate.criterion.Expression;
28 import org.hibernate.dialect.DerbyDialect;
29 import org.hibernate.dialect.HSQLDialect;
30 import org.hibernate.dialect.MckoiDialect;
31 import org.hibernate.dialect.MySQLDialect;
32 import org.hibernate.dialect.Oracle9Dialect;
33 import org.hibernate.dialect.SAPDBDialect;
34 import org.hibernate.dialect.SybaseDialect;
35 import org.hibernate.mapping.MetaAttribute;
36 import org.hibernate.mapping.PersistentClass;
37 import org.hibernate.test.TestCase;
38
39 public class MasterDetailTest extends TestCase {
40
41     public MasterDetailTest(String JavaDoc arg) {
42         super(arg);
43     }
44
45     public void testOuterJoin() throws Exception JavaDoc {
46         Session s = openSession();
47         Eye e = new Eye();
48         e.setName("Eye Eye");
49         Jay jay = new Jay(e);
50         e.setJay(jay);
51         s.saveOrUpdate(e);
52         s.flush();
53         s.connection().commit();
54         s.close();
55
56         s = openSession();
57         e = (Eye) s.createCriteria(Eye.class).uniqueResult();
58         assertTrue( Hibernate.isInitialized( e.getJay() ) );
59         assertTrue( Hibernate.isInitialized( e.getJays() ) );
60         s.connection().commit();
61         s.close();
62
63         s = openSession();
64         jay = (Jay) s.createQuery("select new Jay(eye) from Eye eye").uniqueResult();
65         assertTrue( "Eye Eye".equals( jay.getEye().getName() ) );
66         s.delete( jay.getEye() );
67         s.flush();
68         s.connection().commit();
69         s.close();
70     }
71
72     public void testMeta() throws Exception JavaDoc {
73         PersistentClass clazz = getCfg().getClassMapping( Master.class.getName() );
74         MetaAttribute meta = clazz.getMetaAttribute("foo");
75         assertTrue( "foo".equals( meta.getValue() ) );
76         meta = clazz.getProperty("name").getMetaAttribute("bar");
77         assertTrue( meta.isMultiValued() );
78     }
79
80     public void testCopy() throws Exception JavaDoc {
81         Category catWA = new Category();
82         catWA.setName("HSQL workaround");
83         Category cat = new Category();
84         cat.setName("foo");
85         Category subCatBar = new Category();
86         subCatBar.setName("bar");
87         Category subCatBaz = new Category();
88         subCatBaz.setName("baz");
89         cat.getSubcategories().add(subCatBar);
90         cat.getSubcategories().add(subCatBaz);
91         Session s = openSession();
92         s.save(catWA);
93         s.save(cat);
94         s.flush();
95         s.connection().commit();
96         s.close();
97
98         cat.setName("new foo");
99         subCatBar.setName("new bar");
100         cat.getSubcategories().remove(subCatBaz);
101         Category newCat = new Category();
102         newCat.setName("new");
103         cat.getSubcategories().add(newCat);
104         Category newSubCat = new Category();
105         newSubCat.setName("new sub");
106         newCat.getSubcategories().add(newSubCat);
107
108         s = openSession();
109         Category copiedCat = (Category) s.saveOrUpdateCopy(cat);
110         s.flush();
111         s.connection().commit();
112         s.close();
113
114         assertFalse( copiedCat==cat );
115         //assertFalse( copiedCat.getSubcategories().contains(newCat) );
116
assertTrue( cat.getSubcategories().contains(newCat) );
117
118         s = openSession();
119         cat = (Category) s.createQuery("from Category cat where cat.name='new foo'").uniqueResult();
120         newSubCat = (Category) s.createQuery("from Category cat left join fetch cat.subcategories where cat.name='new sub'").uniqueResult();
121         assertTrue( newSubCat.getName().equals("new sub") );
122         s.close();
123
124         newSubCat.getSubcategories().add(cat);
125         cat.setName("new new foo");
126
127         s = openSession();
128         newSubCat = (Category) s.saveOrUpdateCopy( newSubCat, new Long JavaDoc( newSubCat.getId() ) );
129         assertTrue( newSubCat.getName().equals("new sub") );
130         assertTrue( newSubCat.getSubcategories().size()==1 );
131         cat = (Category) newSubCat.getSubcategories().get(0);
132         assertTrue( cat.getName().equals("new new foo") );
133         newSubCat.getSubcategories().remove(cat);
134         s.delete(cat);
135         s.delete(subCatBaz);
136         s.delete(catWA);
137         s.flush();
138         s.connection().commit();
139         s.close();
140
141     }
142
143     public void testNotNullDiscriminator() throws Exception JavaDoc {
144         Session s = openSession();
145         Transaction t = s.beginTransaction();
146         Up up = new Up();
147         up.setId1("foo");
148         up.setId2(123l);
149         Down down = new Down();
150         down.setId1("foo");
151         down.setId2(321l);
152         down.setValue(12312312l);
153         s.save(up);
154         s.save(down);
155         t.commit();
156         s.close();
157
158         s = openSession();
159         t = s.beginTransaction();
160         List JavaDoc list = s.find("from Up up order by up.id2 asc");
161         assertTrue( list.size()==2 );
162         assertFalse( list.get(0) instanceof Down );
163         assertTrue( list.get(1) instanceof Down );
164         list = s.find("from Down down");
165         assertTrue( list.size()==1 );
166         assertTrue( list.get(0) instanceof Down );
167         //list = s.find("from Up down where down.class = Down");
168
assertTrue( list.size()==1 );
169         assertTrue( list.get(0) instanceof Down );
170         s.delete("from Up up");
171         t.commit();
172         s.close();
173
174     }
175
176     public void testSelfManyToOne() throws Exception JavaDoc {
177
178         //if (dialect instanceof HSQLDialect) return;
179

180         Session s = openSession();
181         Transaction t = s.beginTransaction();
182         Master m = new Master();
183         m.setOtherMaster(m);
184         s.save(m);
185         t.commit();
186         s.close();
187         s = openSession();
188         t = s.beginTransaction();
189         Iterator JavaDoc i = s.iterate("from Master");
190         m = (Master) i.next();
191         assertTrue( m.getOtherMaster()==m );
192         if (getDialect() instanceof HSQLDialect) { m.setOtherMaster(null); s.flush(); }
193         s.delete(m);
194         t.commit();
195         s.close();
196     }
197
198     public void testExample() throws Exception JavaDoc {
199
200         Session s = openSession();
201         Transaction t = s.beginTransaction();
202         Master m = new Master();
203         m.setName("name");
204         m.setX(5);
205         m.setOtherMaster(m);
206         s.save(m);
207         t.commit();
208         s.close();
209
210         s = openSession();
211         t = s.beginTransaction();
212         Master m1 = (Master) s.createCriteria(Master.class)
213             .add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") )
214             .uniqueResult();
215         assertTrue( m1.getOtherMaster()==m1 );
216         m1 = (Master) s.createCriteria(Master.class)
217             .add( Expression.eq("name", "foobar") )
218             .uniqueResult();
219         assertTrue( m1==null );
220         m1 = (Master) s.createCriteria(Master.class)
221             .add( Example.create(m).excludeProperty("bigDecimal") )
222             .createCriteria("otherMaster")
223                 .add( Example.create(m).excludeZeroes().excludeProperty("bigDecimal") )
224             .uniqueResult();
225         assertTrue( m1.getOtherMaster()==m1 );
226         Master m2 = (Master) s.createCriteria(Master.class)
227             .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") )
228             .uniqueResult();
229         assertTrue( m2==m1 );
230         m.setName(null);
231         m2 = (Master) s.createCriteria(Master.class)
232             .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") )
233             .uniqueResult();
234         assertTrue( null==m2 );
235         if (getDialect() instanceof HSQLDialect) { m1.setOtherMaster(null); s.flush(); }
236         s.delete(m1);
237         t.commit();
238         s.close();
239     }
240
241     public void testNonLazyBidirectional() throws Exception JavaDoc {
242         Session s = openSession();
243         Transaction t = s.beginTransaction();
244         Single sin = new Single();
245         sin.setId("asdfds");
246         sin.setString("adsa asdfasd");
247         Several sev = new Several();
248         sev.setId("asdfasdfasd");
249         sev.setString("asd ddd");
250         sin.getSeveral().add(sev);
251         sev.setSingle(sin);
252         s.save(sin);
253         t.commit();
254         s.close();
255         s = openSession();
256         t = s.beginTransaction();
257         sin = (Single) s.load( Single.class, sin );
258         t.commit();
259         s.close();
260         s = openSession();
261         t = s.beginTransaction();
262         sev = (Several) s.load( Several.class, sev );
263         t.commit();
264         s.close();
265         s = openSession();
266         t = s.beginTransaction();
267         s.find("from Several");
268         t.commit();
269         s.close();
270         s = openSession();
271         t = s.beginTransaction();
272         s.delete("from Single");
273         t.commit();
274         s.close();
275     }
276
277     public void testCollectionQuery() throws Exception JavaDoc {
278         Session s = openSession();
279         Transaction t = s.beginTransaction();
280         if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) ) {
281             s.iterate("FROM Master m WHERE NOT EXISTS ( FROM m.details d WHERE NOT d.i=5 )");
282             s.iterate("FROM Master m WHERE NOT 5 IN ( SELECT d.i FROM m.details AS d )");
283         }
284         s.iterate("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");
285         s.find("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");
286         s.find("SELECT m.id FROM Master AS m JOIN m.details AS d WHERE d.i=5");
287         t.commit();
288         s.close();
289     }
290
291     public void testMasterDetail() throws Exception JavaDoc {
292
293         if (getDialect() instanceof HSQLDialect) return;
294
295         Session s = openSession();
296         Transaction t = s.beginTransaction();
297         Master master = new Master();
298         assertTrue( "save returned native id", s.save(master)!=null );
299         Serializable JavaDoc mid = s.getIdentifier(master);
300         Detail d1 = new Detail();
301         d1.setMaster(master);
302         Serializable JavaDoc did = s.save(d1);
303         Detail d2 = new Detail();
304         d2.setI(12);
305         d2.setMaster(master);
306         assertTrue( "generated id returned", s.save(d2)!=null);
307         master.addDetail(d1);
308         master.addDetail(d2);
309         if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof Oracle9Dialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof org.hibernate.dialect.TimesTenDialect)) {
310             assertTrue(
311                 "query",
312                 s.find("from Detail d, Master m where m = d.master and size(m.outgoing) = 0 and size(m.incoming) = 0").size()==2
313             );
314         }
315         t.commit();
316         s.close();
317
318         s = openSession();
319         t = s.beginTransaction();
320         master = new Master();
321         s.load(master, mid);
322         assertTrue( master.getDetails().size()==2 );
323         t.commit();
324         s.close();
325
326         s = openSession();
327         t = s.beginTransaction();
328         master = (Master) s.load(Master.class, mid);
329         Iterator JavaDoc iter = master.getDetails().iterator();
330         int i=0;
331         while ( iter.hasNext() ) {
332             Detail d = (Detail) iter.next();
333             assertTrue( "master-detail", d.getMaster()==master );
334             i++;
335         }
336         assertTrue( "master-detail", i==2 );
337         t.commit();
338         s.close();
339
340         s = openSession();
341         t = s.beginTransaction();
342         assertTrue( s.find("select elements(master.details) from Master master").size()==2 );
343         t.commit();
344         s.close();
345
346         s = openSession();
347         t = s.beginTransaction();
348         List JavaDoc list = s.find("from Master m left join fetch m.details");
349         Master m = (Master) list.get(0);
350         assertTrue( Hibernate.isInitialized( m.getDetails() ) );
351         assertTrue( m.getDetails().size()==2 );
352         list = s.find("from Detail d inner join fetch d.master");
353         Detail dt = (Detail) list.get(0);
354         Serializable JavaDoc dtid = s.getIdentifier(dt);
355         assertTrue( dt.getMaster()==m );
356
357         //assertTrue(m.getAllDetails().size()==2);
358

359         t.commit();
360         s.close();
361
362         s = openSession();
363         t = s.beginTransaction();
364         list = s.find("select m from Master m1, Master m left join fetch m.details where m.name=m1.name");
365         assertTrue( Hibernate.isInitialized( ( (Master) list.get(0) ).getDetails() ) );
366         dt = (Detail) s.load(Detail.class, dtid);
367         assertTrue( ( (Master) list.get(0) ).getDetails().contains(dt) );
368         t.commit();
369         s.close();
370
371         s = openSession();
372         t = s.beginTransaction();
373         list = s.find("select m, m1.name from Master m1, Master m left join fetch m.details where m.name=m1.name");
374         assertTrue( Hibernate.isInitialized( ( (Master) ( (Object JavaDoc[]) list.get(0) )[0] ).getDetails() ) );
375         dt = (Detail) s.load(Detail.class, dtid);
376         assertTrue( ( (Master) ( (Object JavaDoc[]) list.get(0) )[0] ).getDetails().contains(dt) );
377         //list = s.find("select m from Master m, Master m2 left join fetch m.details");
378
list = s.find("select m.id from Master m inner join fetch m.details");
379         t.commit();
380         s.close();
381
382
383         s = openSession();
384         t = s.beginTransaction();
385         Detail dd = (Detail) s.load(Detail.class, did);
386         master = dd.getMaster();
387         assertTrue( "detail-master", master.getDetails().contains(dd) );
388         assertTrue( s.filter( master.getDetails(), "order by this.i desc").size()==2 );
389         assertTrue( s.filter( master.getDetails(), "select this where this.id > -1").size()==2 );
390         Query q = s.createFilter( master.getDetails(), "where this.id > :id" );
391         q.setInteger("id", -1);
392         assertTrue( q.list().size()==2 );
393         q = s.createFilter( master.getDetails(), "where this.id > :id1 and this.id < :id2" );
394         q.setInteger("id1", -1);
395         q.setInteger("id2", 99999999);
396         assertTrue( q.list().size()==2 );
397         q.setInteger("id2", -1);
398         assertTrue( q.list().size()==0 );
399         q = s.createFilter( master.getDetails(), "where this.id in (:ids)" );
400         list = new ArrayList JavaDoc();
401         list.add(did);
402         list.add( new Long JavaDoc(-1) );
403         q.setParameterList("ids", list);
404         assertTrue( q.list().size()==1 );
405         assertTrue( q.iterate().hasNext() );
406         assertTrue( s.filter( master.getDetails(), "where this.id > -1").size()==2 );
407         assertTrue( s.filter( master.getDetails(), "select this.master where this.id > -1").size()==2 );
408         assertTrue( s.filter( master.getDetails(), "select m from Master m where this.id > -1 and this.master=m").size()==2 );
409         assertTrue( s.filter( master.getIncoming(), "where this.id > -1 and this.name is not null").size()==0 );
410
411         assertTrue( s.createFilter( master.getDetails(), "select max(this.i)" ).iterate().next() instanceof Integer JavaDoc );
412         assertTrue( s.createFilter( master.getDetails(), "select max(this.i) group by this.id" ).iterate().next() instanceof Integer JavaDoc );
413         assertTrue( s.createFilter( master.getDetails(), "select count(*)" ).iterate().next() instanceof Integer JavaDoc );
414
415         assertTrue( s.createFilter( master.getDetails(), "select this.master" ).list().size()==2 );
416         assertTrue( s.filter( master.getMoreDetails(), "" ).size()==0 );
417         assertTrue( s.filter( master.getIncoming(), "" ).size()==0 );
418
419         Query f = s.createFilter( master.getDetails(), "select max(this.i) where this.i < :top and this.i>=:bottom" );
420         f.setInteger("top", 100);
421         f.setInteger("bottom", 0);
422         assertEquals( f.iterate().next(), new Integer JavaDoc(12) );
423         f.setInteger("top", 2);
424         assertEquals( f.iterate().next(), new Integer JavaDoc(0) );
425
426         f = s.createFilter( master.getDetails(), "select max(this.i) where this.i not in (:list)" );
427         Collection JavaDoc coll = new ArrayList JavaDoc();
428         coll.add( new Integer JavaDoc(-666) );
429         coll.add( new Integer JavaDoc(22) );
430         coll.add( new Integer JavaDoc(0) );
431         f.setParameterList("list", coll);
432         assertEquals( f.iterate().next(), new Integer JavaDoc(12) );
433
434         f = s.createFilter( master.getDetails(), "select max(this.i) where this.i not in (:list) and this.master.name = :listy2" );
435         f.setParameterList("list", coll);
436         f.setParameter( "listy2", master.getName() );
437         assertEquals( f.iterate().next(), new Integer JavaDoc(12) );
438
439         iter = master.getDetails().iterator();
440         i=0;
441         while ( iter.hasNext() ) {
442             Detail d = (Detail) iter.next();
443             assertTrue( "master-detail", d.getMaster()==master );
444             s.delete(d);
445             i++;
446         }
447         assertTrue( "master-detail", i==2 );
448         s.delete(master);
449         t.commit();
450         s.close();
451     }
452
453     public void testIncomingOutgoing() throws Exception JavaDoc {
454
455         if (getDialect() instanceof HSQLDialect) return;
456
457         Session s = openSession();
458         Master master1 = new Master();
459         Master master2 = new Master();
460         Master master3 = new Master();
461         s.save(master1);
462         s.save(master2);
463         s.save(master3);
464         master1.addIncoming(master2);
465         master2.addOutgoing(master1);
466         master1.addIncoming(master3);
467         master3.addOutgoing(master1);
468         Serializable JavaDoc m1id = s.getIdentifier(master1);
469         assertTrue( s.filter( master1.getIncoming(), "where this.id > 0 and this.name is not null").size()==2 );
470         s.flush();
471         s.connection().commit();
472         s.close();
473
474         s = openSession();
475         master1 = (Master) s.load(Master.class, m1id);
476         Iterator JavaDoc iter = master1.getIncoming().iterator();
477         int i=0;
478         while ( iter.hasNext() ) {
479             Master m = (Master) iter.next();
480             assertTrue( "outgoing", m.getOutgoing().size()==1 );
481             assertTrue( "outgoing", m.getOutgoing().contains(master1) );
482             s.delete(m);
483             i++;
484         }
485         assertTrue( "incoming-outgoing", i==2 );
486         s.delete(master1);
487         s.flush();
488         s.connection().commit();
489         s.close();
490     }
491
492     public void testCascading() throws Exception JavaDoc {
493
494         if (getDialect() instanceof HSQLDialect) return;
495
496         Session s = openSession();
497         Detail d1 = new Detail();
498         Detail d2 = new Detail();
499         d2.setI(22);
500         Master m = new Master();
501         Master m0 = new Master();
502         Serializable JavaDoc m0id = s.save(m0);
503         m0.addDetail(d1); m0.addDetail(d2);
504         d1.setMaster(m0); d2.setMaster(m0);
505         m.getMoreDetails().add(d1);
506         m.getMoreDetails().add(d2);
507         Serializable JavaDoc mid = s.save(m);
508         s.flush();
509         s.connection().commit();
510         s.close();
511         s = openSession();
512         m = (Master) s.load(Master.class, mid);
513         assertTrue( "cascade save", m.getMoreDetails().size()==2 );
514         assertTrue( "cascade save", ( (Detail) m.getMoreDetails().iterator().next() ).getMaster().getDetails().size()==2 );
515
516         s.delete(m);
517         s.delete( s.load(Master.class, m0id) );
518
519         s.flush();
520         s.connection().commit();
521         s.close();
522     }
523
524     /*public void testCascading2() throws Exception {
525         Session s = sessionsopenSession();
526         Detail d1 = new Detail();
527         Detail d2 = new Detail();
528         Master m = new Master();
529         m.getMoreDetails().add(d1);
530         m.getMoreDetails().add(d2);
531         Serializable mid = s.save(m);
532         s.flush();
533         s.connection().commit();
534         s.close();
535         s = sessionsopenSession();
536         m = (Master) s.load(Master.class, mid);
537         assertTrue( m.getMoreDetails().size()==2, "cascade save" );
538         s.delete(m);
539         s.flush();
540         s.connection().commit();
541         s.close();
542     }*/

543
544     public void testNamedQuery() throws Exception JavaDoc {
545         Session s = openSession();
546         Query q = s.getNamedQuery("all_details");
547         q.list();
548         s.connection().commit();
549         s.close();
550     }
551
552
553     public void testSerialization() throws Exception JavaDoc {
554
555         if (getDialect() instanceof HSQLDialect) return;
556
557         Session s = openSession();
558         Master m = new Master();
559         Detail d1 = new Detail();
560         Detail d2 = new Detail();
561         Serializable JavaDoc mid = s.save(m);
562         d1.setMaster(m);
563         d2.setMaster(m);
564         m.addDetail(d1);
565         m.addDetail(d2);
566         if ( (getDialect() instanceof SybaseDialect) || (getDialect() instanceof DerbyDialect) ) {
567             s.save(d1);
568         }
569         else {
570             s.save( d1, new Long JavaDoc(666) );
571         }
572         //s.save(d2);
573
s.flush();
574         s.connection().commit();
575         s.disconnect();
576         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
577         new ObjectOutputStream JavaDoc(os).writeObject(s);
578         byte[] bytes = os.toByteArray();
579         System.out.println(bytes.length);
580         s = (Session) new ObjectInputStream JavaDoc( new ByteArrayInputStream JavaDoc(bytes) ).readObject();
581         s.reconnect();
582         Master m2 = (Master) s.load(Master.class, mid);
583         assertTrue( "serialized state", m2.getDetails().size()==2 );
584         Iterator JavaDoc iter = m2.getDetails().iterator();
585         while ( iter.hasNext() ) {
586             Detail d = (Detail) iter.next();
587             assertTrue( "deserialization", d.getMaster()==m2 );
588             try {
589                 s.getIdentifier(d);
590                 s.delete(d);
591             }
592             catch (Exception JavaDoc e) {}
593         }
594         s.delete(m2);
595         s.flush();
596         s.connection().commit();
597         s.close();
598
599         s = openSession();
600         mid = s.save( new Master() );
601         Serializable JavaDoc mid2 = s.save( new Master() );
602         s.flush();
603         s.connection().commit();
604         s.disconnect();
605         os = new ByteArrayOutputStream JavaDoc();
606         new ObjectOutputStream JavaDoc(os).writeObject(s);
607         bytes = os.toByteArray();
608         System.out.println(bytes.length);
609         s = (Session) new ObjectInputStream JavaDoc( new ByteArrayInputStream JavaDoc(bytes) ).readObject();
610         s.reconnect();
611         s.delete( s.load(Master.class, mid) );
612         s.delete( s.load(Master.class, mid2) );
613         s.flush();
614         s.connection().commit();
615         s.close();
616
617         s = openSession();
618         s.connection(); //force session to grab a connection
619
try {
620             os = new ByteArrayOutputStream JavaDoc();
621             new ObjectOutputStream JavaDoc(os).writeObject(s);
622         }
623         catch (Exception JavaDoc e) {
624             assertTrue("illegal state", e instanceof IllegalStateException JavaDoc );
625             s.connection().commit();
626             s.close();
627             return;
628         }
629         assertTrue("serialization should have failed", false);
630
631     }
632
633     public void testUpdateLazyCollections() throws Exception JavaDoc {
634
635         if (getDialect() instanceof HSQLDialect) return;
636
637         Session s = openSession();
638         Master m = new Master();
639         Detail d1 = new Detail();
640         Detail d2 = new Detail();
641         d2.setX(14);
642         Serializable JavaDoc mid = s.save(m);
643         //s.flush();
644
d1.setMaster(m);
645         d2.setMaster(m);
646         m.addDetail(d1);
647         m.addDetail(d2);
648         if ( (getDialect() instanceof SybaseDialect) || (getDialect() instanceof DerbyDialect) ) {
649             s.save(d1); s.save(d2);
650         }
651         else {
652             s.save( d1, new Long JavaDoc(666) );
653             s.save( d2, new Long JavaDoc(667) );
654         }
655         s.flush();
656         s.connection().commit();
657         s.close();
658         s = openSession();
659         m = (Master) s.load(Master.class, mid);
660         s.connection().commit();
661         s.close();
662         m.setName("New Name");
663         s = openSession();
664         s.update(m, mid);
665         Iterator JavaDoc iter = m.getDetails().iterator();
666         int i=0;
667         while ( iter.hasNext() ) {
668             assertTrue( iter.next()!=null );
669             i++;
670         }
671         assertTrue(i==2);
672         iter = m.getDetails().iterator();
673         while ( iter.hasNext() ) s.delete( iter.next() );
674         s.delete(m);
675         s.flush();
676         s.connection().commit();
677         s.close();
678
679     }
680
681     public void testMultiLevelCascade() throws Exception JavaDoc {
682
683         if (getDialect() instanceof HSQLDialect) return;
684
685         Session s = openSession();
686         Detail detail = new Detail();
687         SubDetail subdetail = new SubDetail();
688         Master m = new Master();
689         Master m0 = new Master();
690         Serializable JavaDoc m0id = s.save(m0);
691         m0.addDetail(detail);
692         detail.setMaster(m0);
693         m.getMoreDetails().add(detail);
694         detail.setSubDetails( new HashSet JavaDoc() );
695         detail.getSubDetails().add(subdetail);
696         Serializable JavaDoc mid = s.save(m);
697         s.flush();
698         s.connection().commit();
699         s.close();
700         s = openSession();
701         m = (Master) s.load( Master.class, mid );
702         assertTrue( ( (Detail) m.getMoreDetails().iterator().next() ).getSubDetails().size()!=0 );
703         s.delete(m);
704         assertTrue( s.find("from SubDetail").size()==0 );
705         assertTrue( s.find("from Detail d").size()==0 );
706         s.delete( s.load(Master.class, m0id) );
707         s.flush();
708         s.connection().commit();
709         s.close();
710     }
711
712     public void testMixNativeAssigned() throws Exception JavaDoc {
713
714         if (getDialect() instanceof HSQLDialect) return;
715
716         Session s = openSession();
717         Category c = new Category();
718         c.setName("NAME");
719         Assignable assn = new Assignable();
720         assn.setId("i.d.");
721         List JavaDoc l = new ArrayList JavaDoc();
722         l.add(c);
723         assn.setCategories(l);
724         c.setAssignable(assn);
725         s.save(assn);
726         s.flush();
727         s.connection().commit();
728         s.close();
729         s = openSession();
730         s.delete(assn);
731         s.flush();
732         s.connection().commit();
733         s.close();
734     }
735
736     public void testCollectionReplaceOnUpdate() throws Exception JavaDoc {
737         Session s = openSession();
738         Transaction t = s.beginTransaction();
739         Category c = new Category();
740         List JavaDoc list = new ArrayList JavaDoc();
741         c.setSubcategories(list);
742         list.add( new Category() );
743         s.save(c);
744         t.commit();
745         s.close();
746         c.setSubcategories(list);
747
748         s = openSession();
749         t = s.beginTransaction();
750         s.update(c);
751         t.commit();
752         s.close();
753
754         s = openSession();
755         t = s.beginTransaction();
756         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
757         List JavaDoc list2 = c.getSubcategories();
758         t.commit();
759         s.close();
760
761         assertTrue( !Hibernate.isInitialized( c.getSubcategories() ) );
762
763         c.setSubcategories(list2);
764         s = openSession();
765         t = s.beginTransaction();
766         s.update(c);
767         t.commit();
768         s.close();
769
770         s = openSession();
771         t = s.beginTransaction();
772         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
773         assertTrue( c.getSubcategories().size()==1 );
774         s.delete(c);
775         t.commit();
776         s.close();
777     }
778
779     public void testCollectionReplace2() throws Exception JavaDoc {
780         Session s = openSession();
781         Transaction t = s.beginTransaction();
782         Category c = new Category();
783         List JavaDoc list = new ArrayList JavaDoc();
784         c.setSubcategories(list);
785         list.add( new Category() );
786         Category c2 = new Category();
787         s.save(c2);
788         s.save(c);
789         t.commit();
790         s.close();
791
792         s = openSession();
793         t = s.beginTransaction();
794         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
795         List JavaDoc list2 = c.getSubcategories();
796         t.commit();
797         s.close();
798
799         s = openSession();
800         t = s.beginTransaction();
801         c2 = (Category) s.load( Category.class, new Long JavaDoc( c2.getId() ), LockMode.UPGRADE );
802         c2.setSubcategories(list2);
803         t.commit();
804         s.close();
805
806         s = openSession();
807         t = s.beginTransaction();
808         c2 = (Category) s.load( Category.class, new Long JavaDoc( c2.getId() ), LockMode.UPGRADE );
809         assertTrue( c2.getSubcategories().size()==1 );
810         s.delete(c2);
811         s.delete( s.load( Category.class, new Long JavaDoc( c.getId() ) ) );
812         t.commit();
813         s.close();
814     }
815
816     public void testCollectionReplace() throws Exception JavaDoc {
817         Session s = openSession();
818         Transaction t = s.beginTransaction();
819         Category c = new Category();
820         List JavaDoc list = new ArrayList JavaDoc();
821         c.setSubcategories(list);
822         list.add( new Category() );
823         s.save(c);
824         t.commit();
825         s.close();
826
827         s = openSession();
828         t = s.beginTransaction();
829         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
830         c.setSubcategories(list);
831         t.commit();
832         s.close();
833
834         s = openSession();
835         t = s.beginTransaction();
836         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
837         List JavaDoc list2 = c.getSubcategories();
838         t.commit();
839         s.close();
840
841         assertTrue( !Hibernate.isInitialized( c.getSubcategories() ) );
842
843         s = openSession();
844         t = s.beginTransaction();
845         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
846         c.setSubcategories(list2);
847         t.commit();
848         s.close();
849
850         s = openSession();
851         t = s.beginTransaction();
852         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ), LockMode.UPGRADE );
853         assertTrue( c.getSubcategories().size()==1 );
854         s.delete(c);
855         t.commit();
856         s.close();
857     }
858
859     public void testCategories() throws Exception JavaDoc {
860         Session s = openSession();
861         Category c = new Category();
862         c.setName(Category.ROOT_CATEGORY);
863         Category c1 = new Category();
864         Category c2 = new Category();
865         Category c3 = new Category();
866         c.getSubcategories().add(c1);
867         c.getSubcategories().add(c2);
868         c2.getSubcategories().add(null);
869         c2.getSubcategories().add(c3);
870         s.save(c);
871         s.flush();
872         s.connection().commit();
873         s.close();
874
875         s = openSession();
876         Transaction tx = s.beginTransaction();
877         s.lock(c, LockMode.UPGRADE);
878         Category loaded = (Category) s.load( Category.class, new Long JavaDoc( c3.getId() ) );
879         assertTrue( s.contains(c3) );
880         assertTrue(loaded==c3);
881         assertTrue( s.getCurrentLockMode(c3)==LockMode.NONE );
882         assertTrue( s.getCurrentLockMode(c)==LockMode.UPGRADE );
883         s.flush();
884         tx.commit();
885         s.close();
886
887         s = openSession();
888         loaded = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ) );
889         assertFalse( Hibernate.isInitialized( loaded.getSubcategories() ) );
890         s.connection().commit();
891         s.close();
892         s = openSession();
893         s.lock(loaded, LockMode.NONE);
894         assertTrue( loaded.getSubcategories().size()==2 );
895         s.connection().commit();
896         s.close();
897
898
899         s = openSession();
900         c = (Category) s.load( Category.class, new Long JavaDoc( c.getId() ) );
901         System.out.println( c.getSubcategories() );
902         assertTrue( c.getSubcategories().get(0)!=null && c.getSubcategories().get(1)!=null );
903         List JavaDoc list = ( (Category) c.getSubcategories().get(1) ).getSubcategories();
904         assertTrue( list.get(1)!=null && list.get(0)==null );
905
906         assertTrue(
907             s.iterate("from Category c where c.name = org.hibernate.test.legacy.Category.ROOT_CATEGORY").hasNext()
908         );
909         s.delete(c);
910         s.flush();
911         s.connection().commit();
912         s.close();
913
914     }
915
916     public void testCollectionRefresh() throws Exception JavaDoc {
917         Session s = openSession();
918         Category c = new Category();
919         List JavaDoc list = new ArrayList JavaDoc();
920         c.setSubcategories(list);
921         list.add( new Category() );
922         c.setName("root");
923         Serializable JavaDoc id = s.save(c);
924         s.flush();
925         s.connection().commit();
926         s.close();
927         s = openSession();
928         c = (Category) s.load(Category.class, id);
929         s.refresh(c);
930         s.flush();
931         assertTrue( c.getSubcategories().size()==1 );
932         s.flush();
933         s.connection().commit();
934         s.close();
935         s = openSession();
936         c = (Category) s.load(Category.class, id);
937         assertTrue( c.getSubcategories().size()==1 );
938         s.delete(c);
939         s.flush();
940         s.connection().commit();
941         s.close();
942     }
943
944     public void testCachedCollectionRefresh() throws Exception JavaDoc {
945         Session s = openSession();
946         Category c = new Category();
947         List JavaDoc list = new ArrayList JavaDoc();
948         c.setSubcategories(list);
949         list.add( new Category() );
950         c.setName("root");
951         Serializable JavaDoc id = s.save(c);
952         s.flush();
953         s.connection().commit();
954         s.close();
955
956         s = openSession();
957         c = (Category) s.load(Category.class, id);
958         c.getSubcategories().size(); //force load and cache
959
s.connection().commit();
960         s.close();
961         
962         s = openSession();
963         if ( (getDialect() instanceof MySQLDialect) ) s.connection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
964         c = (Category) s.load(Category.class, id);
965         c.getSubcategories().size(); //force load
966

967         Session ss = openSession();
968         Category c2 = (Category) ss.load(Category.class, id);
969         ss.delete( c2.getSubcategories().get(0) );
970         c2.getSubcategories().clear();
971         ss.flush();
972         ss.connection().commit();
973         ss.close();
974
975         s.refresh(c);
976         assertTrue( c.getSubcategories().size()==0 );
977
978         ss = openSession();
979         c2 = (Category) ss.load(Category.class, id);
980         c2.getSubcategories().add( new Category() );
981         c2.getSubcategories().add( new Category() );
982         ss.flush();
983         ss.connection().commit();
984         ss.close();
985
986         s.refresh(c);
987         assertTrue( c.getSubcategories().size()==2 );
988
989         s.flush();
990         s.connection().commit();
991         s.close();
992
993         s = openSession();
994         c = (Category) s.load(Category.class, id);
995         assertTrue( c.getSubcategories().size()==2 );
996         s.delete(c);
997         s.flush();
998         s.connection().commit();
999         s.close();
1000    }
1001
1002    public void testCustomPersister() throws Exception JavaDoc {
1003        Session s = openSession();
1004        Custom c = new Custom();
1005        c.setName( "foo" );
1006        c.id="100";
1007        String JavaDoc id = (String JavaDoc) s.save(c);
1008        assertTrue( c==s.load(Custom.class, id) );
1009        s.flush();
1010        s.close();
1011        s = openSession();
1012        c = (Custom) s.load(Custom.class, id);
1013        assertTrue( c.getName().equals("foo") );
1014        c.setName( "bar" );
1015        s.flush();
1016        s.close();
1017        s = openSession();
1018        c = (Custom) s.load(Custom.class, id);
1019        assertTrue( c.getName().equals("bar") );
1020        s.delete(c);
1021        s.flush();
1022        s.close();
1023        s = openSession();
1024        boolean none = false;
1025        try {
1026            s.load(Custom.class, id);
1027        }
1028        catch (ObjectNotFoundException onfe) {
1029            none=true;
1030        }
1031        assertTrue(none);
1032        s.close();
1033
1034    }
1035
1036    public void testInterface() throws Exception JavaDoc {
1037        Session s = openSession();
1038        Serializable JavaDoc id = s.save( new BasicNameable() );
1039        s.flush();
1040        s.connection().commit();
1041        s.close();
1042        s = openSession();
1043        Nameable n = (Nameable) s.load(Nameable.class, id);
1044        s.delete(n);
1045        s.flush();
1046        s.connection().commit();
1047        s.close();
1048    }
1049
1050    public void testNoUpdateManyToOne() throws Exception JavaDoc {
1051        Session s = openSession();
1052        W w1 = new W();
1053        W w2 = new W();
1054        Z z = new Z();
1055        z.setW(w1);
1056        s.save(z);
1057        s.flush();
1058        z.setW(w2);
1059        s.flush();
1060        s.connection().commit();
1061        s.close();
1062
1063        s = openSession();
1064        s.update(z);
1065        s.flush();
1066        s.delete(z);
1067        s.delete("from W");
1068        s.flush();
1069        s.connection().commit();
1070        s.close();
1071    }
1072
1073    public void testQueuedBagAdds() throws Exception JavaDoc {
1074        Session s = openSession();
1075        Assignable a = new Assignable();
1076        a.setId("foo");
1077        a.setCategories( new ArrayList JavaDoc() );
1078        Category c = new Category();
1079        c.setAssignable(a);
1080        a.getCategories().add(c);
1081        s.save(a);
1082        s.flush();
1083        s.connection().commit();
1084        s.close();
1085
1086        getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
1087
1088        s = openSession();
1089        a = (Assignable) s.get(Assignable.class, "foo");
1090        c = new Category();
1091        c.setAssignable(a);
1092        a.getCategories().add(c);
1093        assertFalse( Hibernate.isInitialized( a.getCategories() ) );
1094        assertTrue( a.getCategories().size()==2 );
1095        s.flush();
1096        s.connection().commit();
1097        s.close();
1098
1099        getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
1100
1101        s = openSession();
1102        a = (Assignable) s.get(Assignable.class, "foo");
1103        c = new Category();
1104        c.setAssignable(a);
1105        a.getCategories().add(c);
1106        assertFalse( Hibernate.isInitialized( a.getCategories() ) );
1107        s.flush();
1108        assertFalse( Hibernate.isInitialized( a.getCategories() ) );
1109        assertTrue( a.getCategories().size()==3 );
1110        s.connection().commit();
1111        s.close();
1112
1113        getSessions().evictCollection("org.hibernate.test.legacy.Assignable.categories");
1114
1115        s = openSession();
1116        a = (Assignable) s.get(Assignable.class, "foo");
1117        assertTrue( a.getCategories().size()==3 );
1118        s.delete(a);
1119        s.flush();
1120        s.connection().commit();
1121        s.close();
1122
1123    }
1124
1125    public void testPolymorphicCriteria() throws Exception JavaDoc {
1126        Session s = openSession();
1127        Category f = new Category();
1128        Single b = new Single();
1129        b.setId("asdfa");
1130        b.setString("asdfasdf");
1131        s.save(f);
1132        s.save(b);
1133        List JavaDoc list = s.createCriteria(Object JavaDoc.class).list();
1134        assertTrue( list.size()==2 );
1135        assertTrue( list.contains(f) && list.contains(b) );
1136        s.delete(f);
1137        s.delete(b);
1138        s.flush();
1139        s.connection().commit();
1140        s.close();
1141    }
1142
1143    public String JavaDoc[] getMappings() {
1144        return new String JavaDoc[] {
1145            "legacy/MasterDetail.hbm.xml",
1146            "legacy/Custom.hbm.xml",
1147            "legacy/Category.hbm.xml",
1148            "legacy/Nameable.hbm.xml",
1149            "legacy/SingleSeveral.hbm.xml",
1150            "legacy/WZ.hbm.xml",
1151            "legacy/UpDown.hbm.xml",
1152            "legacy/Eye.hbm.xml"
1153        };
1154    }
1155
1156    public static Test suite() {
1157        return new TestSuite(MasterDetailTest.class);
1158    }
1159
1160    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
1161        TestRunner.run( suite() );
1162    }
1163
1164}
1165
1166
1167
1168
1169
1170
1171
Popular Tags