KickJava   Java API By Example, From Geeks To Geeks.

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


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