KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: FooBarTest.java,v 1.31 2005/07/14 23:56:52 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.Time JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.SortedSet JavaDoc;
18 import java.util.TimeZone JavaDoc;
19 import java.util.TreeMap JavaDoc;
20 import java.util.TreeSet 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.LazyInitializationException;
32 import org.hibernate.LockMode;
33 import org.hibernate.ObjectDeletedException;
34 import org.hibernate.ObjectNotFoundException;
35 import org.hibernate.Query;
36 import org.hibernate.QueryException;
37 import org.hibernate.ScrollableResults;
38 import org.hibernate.Transaction;
39 import org.hibernate.cfg.Configuration;
40 import org.hibernate.cfg.Environment;
41 import org.hibernate.classic.Session;
42 import org.hibernate.connection.ConnectionProvider;
43 import org.hibernate.connection.DriverManagerConnectionProvider;
44 import org.hibernate.criterion.Example;
45 import org.hibernate.criterion.Expression;
46 import org.hibernate.criterion.MatchMode;
47 import org.hibernate.criterion.Order;
48 import org.hibernate.dialect.DB2Dialect;
49 import org.hibernate.dialect.DerbyDialect;
50 import org.hibernate.dialect.HSQLDialect;
51 import org.hibernate.dialect.InterbaseDialect;
52 import org.hibernate.dialect.MckoiDialect;
53 import org.hibernate.dialect.MySQLDialect;
54 import org.hibernate.dialect.Oracle9Dialect;
55 import org.hibernate.dialect.OracleDialect;
56 import org.hibernate.dialect.PointbaseDialect;
57 import org.hibernate.dialect.PostgreSQLDialect;
58 import org.hibernate.dialect.SAPDBDialect;
59 import org.hibernate.dialect.SQLServerDialect;
60 import org.hibernate.dialect.SybaseDialect;
61 import org.hibernate.dialect.TimesTenDialect;
62 import org.hibernate.engine.SessionFactoryImplementor;
63 import org.hibernate.jmx.HibernateService;
64 import org.hibernate.mapping.RootClass;
65 import org.hibernate.proxy.HibernateProxy;
66 import org.hibernate.test.TestCase;
67 import org.hibernate.type.Type;
68 import org.hibernate.util.JoinedIterator;
69 import org.hibernate.util.SerializationHelper;
70
71 public class FooBarTest extends TestCase {
72
73     public FooBarTest(String JavaDoc arg) {
74         super(arg);
75     }
76
77     public void testSaveOrUpdateCopyAny() throws Exception JavaDoc {
78         Session s = openSession();
79         Bar bar = new Bar();
80         One one = new One();
81         bar.setObject(one);
82         s.save(bar);
83         GlarchProxy g = bar.getComponent().getGlarch();
84         bar.getComponent().setGlarch(null);
85         s.delete(g);
86         s.flush();
87         assertTrue( s.contains(one) );
88         s.connection().commit();
89         s.close();
90
91         s = openSession();
92         Bar bar2 = (Bar) s.saveOrUpdateCopy(bar);
93         s.flush();
94         s.delete(bar2);
95         s.flush();
96         s.connection().commit();
97         s.close();
98     }
99
100     public void testRefreshProxy() throws Exception JavaDoc {
101         Session s = openSession();
102         Glarch g = new Glarch();
103         Serializable JavaDoc gid = s.save(g);
104         s.flush();
105         s.clear();
106         GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid);
107         gp.getName(); //force init
108
s.refresh(gp);
109         s.delete(gp);
110         s.flush();
111         s.connection().commit();
112         s.close();
113     }
114
115     public void testOnCascadeDelete() throws Exception JavaDoc {
116
117         if (getDialect() instanceof MySQLDialect) return;
118
119         Session s = openSession();
120         Baz baz = new Baz();
121         baz.subs = new ArrayList JavaDoc();
122         Baz sub = new Baz();
123         sub.superBaz = baz;
124         baz.subs.add(sub);
125         s.save(baz);
126         s.flush();
127         assertTrue( s.createQuery("from Baz").list().size()==2 );
128         s.connection().commit();
129         s.delete(baz);
130         s.flush();
131         s.connection().commit();
132         assertTrue( s.createQuery("from Baz").list().size()==0 );
133         s.connection().commit();
134         s.close();
135     }
136
137     public void testRemoveFromIdbag() throws Exception JavaDoc {
138         Session s = openSession();
139         Baz baz = new Baz();
140         baz.setByteBag( new ArrayList JavaDoc() );
141         byte[] bytes = { 12, 13 };
142         baz.getByteBag().add( new byte[] { 10, 45 } );
143         baz.getByteBag().add(bytes);
144         baz.getByteBag().add( new byte[] { 1, 11 } );
145         baz.getByteBag().add( new byte[] { 12 } );
146         s.save(baz);
147         s.flush();
148         baz.getByteBag().remove(bytes);
149         s.flush();
150         baz.getByteBag().add(bytes);
151         s.flush();
152         s.delete(baz);
153         s.flush();
154         s.connection().commit();
155         s.close();
156     }
157
158     public void testLoad() throws Exception JavaDoc {
159         Session s = openSession();
160         Qux q = new Qux();
161         s.save(q);
162         BarProxy b = new Bar();
163         s.save(b);
164         s.flush();
165         s.connection().commit();
166         s.close();
167         s = openSession();
168         q = (Qux) s.load(Qux.class, q.getKey() );
169         b = (BarProxy) s.load( Foo.class, b.getKey() );
170         b.getKey();
171         assertFalse( Hibernate.isInitialized(b) );
172         b.getBarString();
173         assertTrue( Hibernate.isInitialized(b) );
174         BarProxy b2 = (BarProxy) s.load( Bar.class, new String JavaDoc( b.getKey() ) );
175         Qux q2 = (Qux) s.load( Qux.class, q.getKey() );
176         assertTrue( "loaded same object", q==q2 );
177         assertTrue( "loaded same object", b==b2 );
178         assertTrue( Math.round( b.getFormula() ) == b.getInt()/2 );
179         s.delete(q2);
180         s.delete(b2);
181         s.flush();
182         s.connection().commit();
183         s.close();
184     }
185
186     public void testJoin() throws Exception JavaDoc {
187         Session s = openSession();
188         Foo foo = new Foo();
189         foo.setJoinedProp("foo");
190         s.save(foo);
191         s.flush();
192         foo.setJoinedProp("bar");
193         s.flush();
194         String JavaDoc fid = foo.getKey();
195         s.delete(foo);
196         s.flush();
197         s.connection().commit();
198         s.close();
199
200         s = openSession();
201         Foo foo2 = new Foo();
202         foo2.setJoinedProp("foo");
203         s.save(foo2);
204         s.find("select foo.id from Foo foo where foo.joinedProp = 'foo'");
205         assertNull( s.get(Foo.class, fid) );
206         s.delete(foo2);
207         s.flush();
208         s.connection().commit();
209         s.close();
210
211     }
212
213     public void testDereferenceLazyCollection() throws Exception JavaDoc {
214         Session s = openSession();
215         Baz baz = new Baz();
216         baz.setFooSet( new HashSet JavaDoc() );
217         Foo foo = new Foo();
218         baz.getFooSet().add(foo);
219         s.save(foo);
220         s.save(baz);
221         foo.setBytes( "foobar".getBytes() );
222         s.flush();
223         s.connection().commit();
224         s.close();
225
226         s = openSession();
227         foo = (Foo) s.get( Foo.class, foo.getKey() );
228         assertTrue( Hibernate.isInitialized( foo.getBytes() ) );
229         assertTrue( foo.getBytes().length==6 );
230         baz = (Baz) s.get( Baz.class, baz.getCode() );
231         assertTrue( baz.getFooSet().size()==1 );
232         s.flush();
233         s.connection().commit();
234         s.close();
235
236         getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet");
237
238         s = openSession();
239         baz = (Baz) s.get( Baz.class, baz.getCode() );
240         assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
241         baz.setFooSet(null);
242         s.flush();
243         s.connection().commit();
244         s.close();
245
246         s = openSession();
247         foo = (Foo) s.get( Foo.class, foo.getKey() );
248         assertTrue( foo.getBytes().length==6 );
249         baz = (Baz) s.get( Baz.class, baz.getCode() );
250         assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
251         assertTrue( baz.getFooSet().size()==0 );
252         s.delete(baz);
253         s.delete(foo);
254         s.flush();
255         s.connection().commit();
256         s.close();
257     }
258
259     public void testMoveLazyCollection() throws Exception JavaDoc {
260         Session s = openSession();
261         Baz baz = new Baz();
262         Baz baz2 = new Baz();
263         baz.setFooSet( new HashSet JavaDoc() );
264         Foo foo = new Foo();
265         baz.getFooSet().add(foo);
266         s.save(foo);
267         s.save(baz);
268         s.save(baz2);
269         foo.setBytes( "foobar".getBytes() );
270         s.flush();
271         s.connection().commit();
272         s.close();
273
274         s = openSession();
275         foo = (Foo) s.get( Foo.class, foo.getKey() );
276         assertTrue( Hibernate.isInitialized( foo.getBytes() ) );
277         assertTrue( foo.getBytes().length==6 );
278         baz = (Baz) s.get( Baz.class, baz.getCode() );
279         assertTrue( baz.getFooSet().size()==1 );
280         s.flush();
281         s.connection().commit();
282         s.close();
283
284         getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet");
285
286         s = openSession();
287         baz = (Baz) s.get( Baz.class, baz.getCode() );
288         assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
289         baz2 = (Baz) s.get( Baz.class, baz2.getCode() );
290         baz2.setFooSet( baz.getFooSet() );
291         baz.setFooSet(null);
292         assertFalse( Hibernate.isInitialized( baz2.getFooSet() ) );
293         s.flush();
294         s.connection().commit();
295         s.close();
296
297         s = openSession();
298         foo = (Foo) s.get( Foo.class, foo.getKey() );
299         assertTrue( foo.getBytes().length==6 );
300         baz = (Baz) s.get( Baz.class, baz.getCode() );
301         baz2 = (Baz) s.get( Baz.class, baz2.getCode() );
302         assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
303         assertTrue( baz.getFooSet().size()==0 );
304         assertTrue( Hibernate.isInitialized( baz2.getFooSet() ) ); //fooSet has batching enabled
305
assertTrue( baz2.getFooSet().size()==1 );
306         s.delete(baz);
307         s.delete(baz2);
308         s.delete(foo);
309         s.flush();
310         s.connection().commit();
311         s.close();
312     }
313
314     public void testCriteriaCollection() throws Exception JavaDoc {
315         Session s = openSession();
316         Baz bb = (Baz) s.createCriteria(Baz.class).uniqueResult();
317         assertTrue(bb==null);
318         Baz baz = new Baz();
319         s.save(baz);
320         s.flush();
321         s.connection().commit();
322         s.close();
323         s = openSession();
324         Baz b = (Baz) s.createCriteria(Baz.class).uniqueResult();
325         assertTrue( Hibernate.isInitialized( b.getTopGlarchez() ) );
326         assertTrue( b.getTopGlarchez().size()==0 );
327         s.delete(b);
328         s.flush();
329         s.connection().commit();
330         s.close();
331     }
332
333     public void testQuery() throws Exception JavaDoc {
334         Session s = openSession();
335         Foo foo = new Foo();
336         s.save(foo);
337         Foo foo2 = new Foo();
338         s.save(foo2);
339         foo.setFoo(foo2);
340
341         List JavaDoc list = s.find("from Foo foo inner join fetch foo.foo");
342         Foo foof = (Foo) list.get(0);
343         assertTrue( Hibernate.isInitialized( foof.getFoo() ) );
344
345         list = s.find("from Baz baz left outer join fetch baz.fooToGlarch");
346
347         list = s.find(
348             "select foo, bar from Foo foo left outer join foo.foo bar where foo = ?",
349             foo,
350             Hibernate.entity(Foo.class)
351         );
352         Object JavaDoc[] row1 = (Object JavaDoc[]) list.get(0);
353         assertTrue( row1[0]==foo && row1[1]==foo2 );
354
355         s.find("select foo.foo.foo.string from Foo foo where foo.foo = 'bar'");
356         s.find("select foo.foo.foo.foo.string from Foo foo where foo.foo = 'bar'");
357         s.find("select foo from Foo foo where foo.foo.foo = 'bar'");
358         s.find("select foo.foo.foo.foo.string from Foo foo where foo.foo.foo = 'bar'");
359         s.find("select foo.foo.foo.string from Foo foo where foo.foo.foo.foo.string = 'bar'");
360         if ( ! (getDialect() instanceof HSQLDialect) ) s.find("select foo.string from Foo foo where foo.foo.foo.foo = foo.foo.foo");
361         s.find("select foo.string from Foo foo where foo.foo.foo = 'bar' and foo.foo.foo.foo = 'baz'");
362         s.find("select foo.string from Foo foo where foo.foo.foo.foo.string = 'a' and foo.foo.string = 'b'");
363
364         s.find("from Bar bar, foo in elements(bar.baz.fooArray)");
365
366         //s.find("from Baz as baz where baz.topComponents[baz].name = 'bazzz'");
367

368         if ( (getDialect() instanceof DB2Dialect) && !(getDialect() instanceof DerbyDialect) ) {
369             s.find("from Foo foo where lower( foo.foo.string ) = 'foo'");
370             s.find("from Foo foo where lower( (foo.foo.string || 'foo') || 'bar' ) = 'foo'");
371             s.find("from Foo foo where repeat( (foo.foo.string || 'foo') || 'bar', 2 ) = 'foo'");
372             s.find("from Bar foo where foo.foo.integer is not null and repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'");
373             s.find("from Bar foo where foo.foo.integer is not null or repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'");
374         }
375         if (getDialect() instanceof SybaseDialect) {
376             s.iterate("select baz from Baz as baz join baz.fooArray foo group by baz order by sum(foo.float)");
377         }
378
379         s.find("from Foo as foo where foo.component.glarch.name is not null");
380         s.find("from Foo as foo left outer join foo.component.glarch as glarch where glarch.name = 'foo'");
381
382         list = s.find("from Foo");
383         assertTrue( list.size()==2 && list.get(0) instanceof FooProxy );
384         list = s.find("from Foo foo left outer join foo.foo");
385         assertTrue( list.size()==2 && ( (Object JavaDoc[]) list.get(0) )[0] instanceof FooProxy );
386
387         s.createQuery("from Bar, Bar").list();
388         s.createQuery("from Foo, Bar").list();
389         s.find("from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo");
390         s.find("from Baz baz left join baz.fooToGlarch join baz.fooSet");
391         s.find("from Baz baz left join baz.fooToGlarch join fetch baz.fooSet foo left join fetch foo.foo");
392
393         list = s.find("from Foo foo where foo.string='osama bin laden' and foo.boolean = true order by foo.string asc, foo.component.count desc");
394         assertTrue( "empty query", list.size()==0 );
395         Iterator JavaDoc iter = s.iterate("from Foo foo where foo.string='osama bin laden' order by foo.string asc, foo.component.count desc");
396         assertTrue( "empty iterator", !iter.hasNext() );
397
398         list = s.find("select foo.foo from Foo foo");
399         assertTrue( "query", list.size()==1 );
400         assertTrue( "returned object", list.get(0)==foo.getFoo() );
401         foo.getFoo().setFoo(foo);
402         foo.setString("fizard");
403         //The following test is disabled for databases with no subselects...also for Interbase (not sure why).
404
if (
405                 !(getDialect() instanceof MySQLDialect) &&
406                 !(getDialect() instanceof HSQLDialect) &&
407                 !(getDialect() instanceof MckoiDialect) &&
408                 !(getDialect() instanceof SAPDBDialect) &&
409                 !(getDialect() instanceof PointbaseDialect) &&
410                 !(getDialect() instanceof DerbyDialect)
411         ) {
412             // && !db.equals("weblogic") {
413
if ( !( getDialect() instanceof InterbaseDialect ) ) {
414                 list = s.find("from Foo foo where ? = some elements(foo.component.importantDates)", new Date JavaDoc(), Hibernate.DATE);
415                 assertTrue( "component query", list.size()==2 );
416             }
417             if( !( getDialect() instanceof TimesTenDialect)) {
418                 list = s.find("from Foo foo where size(foo.component.importantDates) = 3"); //WAS: 4
419
assertTrue( "component query", list.size()==2 );
420                 list = s.find("from Foo foo where 0 = size(foo.component.importantDates)");
421                 assertTrue( "component query", list.size()==0 );
422             }
423             list = s.find("from Foo foo where exists elements(foo.component.importantDates)");
424             assertTrue( "component query", list.size()==2 );
425             s.find("from Foo foo where not exists (from Bar bar where bar.id = foo.id)");
426
427             s.find("select foo.foo from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)");
428             s.find("select foo.foo from Foo foo where foo = some(from Foo x where (x.long > foo.foo.long))");
429             if ( !( getDialect() instanceof TimesTenDialect)) {
430                 s.find("select foo.foo from Foo foo where foo.long = some( select max(x.long) from Foo x where (x.long > foo.foo.long) group by x.foo )");
431             }
432             s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long) and foo.foo.string='baz'");
433             s.find("from Foo foo where foo.foo.string='baz' and foo = some(select x from Foo x where x.long > foo.foo.long)");
434             s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)");
435
436             s.iterate("select foo.string, foo.date, foo.foo.string, foo.id from Foo foo, Baz baz where foo in elements(baz.fooArray) and foo.string like 'foo'");
437         }
438         list = s.find("from Foo foo where foo.component.count is null order by foo.component.count");
439         assertTrue( "component query", list.size()==0 );
440         list = s.find("from Foo foo where foo.component.name='foo'");
441         assertTrue( "component query", list.size()==2 );
442         list = s.find("select distinct foo.component.name, foo.component.name from Foo foo where foo.component.name='foo'");
443         assertTrue( "component query", list.size()==1 );
444         list = s.find("select distinct foo.component.name, foo.id from Foo foo where foo.component.name='foo'");
445         assertTrue( "component query", list.size()==2 );
446         list = s.find("select foo.foo from Foo foo");
447         assertTrue( "query", list.size()==2 );
448         list = s.find("from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING);
449         assertTrue( "id query", list.size()==1 );
450         list = s.find("from Foo foo where foo.key=?", foo.getKey(), Hibernate.STRING);
451         assertTrue( "named id query", list.size()==1 );
452         assertTrue( "id query", list.get(0)==foo );
453         list = s.find("select foo.foo from Foo foo where foo.string='fizard'");
454         assertTrue( "query", list.size()==1 );
455         assertTrue( "returned object", list.get(0)==foo.getFoo() );
456         list = s.find("from Foo foo where foo.component.subcomponent.name='bar'");
457         assertTrue( "components of components", list.size()==2 );
458         list = s.find("select foo.foo from Foo foo where foo.foo.id=?", foo.getFoo().getKey(), Hibernate.STRING);
459         assertTrue( "by id query", list.size()==1 );
460         assertTrue( "by id returned object", list.get(0)==foo.getFoo() );
461
462         s.find( "from Foo foo where foo.foo = ?", foo.getFoo(), Hibernate.entity(Foo.class) );
463
464         assertTrue( !s.iterate("from Bar bar where bar.string='a string' or bar.string='a string'").hasNext() );
465
466         iter = s.iterate(
467             "select foo.component.name, elements(foo.component.importantDates) from Foo foo where foo.foo.id=?",
468             foo.getFoo().getKey(),
469             Hibernate.STRING
470         );
471         int i=0;
472         while ( iter.hasNext() ) {
473             i++;
474             Object JavaDoc[] row = (Object JavaDoc[]) iter.next();
475             assertTrue( row[0] instanceof String JavaDoc && ( row[1]==null || row[1] instanceof Date JavaDoc ) );
476         }
477         assertTrue(i==3); //WAS: 4
478
iter = s.iterate(
479             "select max( elements(foo.component.importantDates) ) from Foo foo group by foo.id"
480         );
481         assertTrue( iter.next() instanceof Date JavaDoc );
482
483         list = s.find(
484             "select foo.foo.foo.foo from Foo foo, Foo foo2 where"
485             + " foo = foo2.foo and not not ( not foo.string='fizard' )"
486             + " and foo2.string between 'a' and (foo.foo.string)"
487             + ( ( getDialect() instanceof HSQLDialect || getDialect() instanceof InterbaseDialect || getDialect() instanceof TimesTenDialect)?
488                 " and ( foo2.string in ( 'fiz', 'blah') or 1=1 )"
489                 :
490                 " and ( foo2.string in ( 'fiz', 'blah', foo.foo.string, foo.string, foo2.string ) )"
491             )
492         );
493         assertTrue( "complex query", list.size()==1 );
494         assertTrue( "returned object", list.get(0)==foo );
495         foo.setString("from BoogieDown -tinsel town =!@#$^&*())");
496         list = s.find("from Foo foo where foo.string='from BoogieDown -tinsel town =!@#$^&*())'");
497         assertTrue( "single quotes", list.size()==1 );
498         list = s.find("from Foo foo where not foo.string='foo''bar'");
499         assertTrue( "single quotes", list.size()==2 );
500         list = s.find("from Foo foo where foo.component.glarch.next is null");
501         assertTrue( "query association in component", list.size()==2 );
502         Bar bar = new Bar();
503         Baz baz = new Baz();
504         baz.setDefaults();
505         bar.setBaz(baz);
506         baz.setManyToAny( new ArrayList JavaDoc() );
507         baz.getManyToAny().add(bar);
508         baz.getManyToAny().add(foo);
509         s.save(bar);
510         s.save(baz);
511         list = s.find(" from Bar bar where bar.baz.count=667 and bar.baz.count!=123 and not bar.baz.name='1-E-1'");
512         assertTrue( "query many-to-one", list.size()==1 );
513         list = s.find(" from Bar i where i.baz.name='Bazza'");
514         assertTrue( "query many-to-one", list.size()==1 );
515
516         Iterator JavaDoc rs = s.iterate("select count(distinct foo.foo) from Foo foo");
517         assertTrue( "count", ( (Integer JavaDoc) rs.next() ).intValue()==2 );
518         assertTrue( !rs.hasNext() );
519         rs = s.iterate("select count(foo.foo.boolean) from Foo foo");
520         assertTrue( "count", ( (Integer JavaDoc) rs.next() ).intValue()==2 );
521         assertTrue( !rs.hasNext() );
522         rs = s.iterate("select count(*), foo.int from Foo foo group by foo.int");
523         assertTrue( "count(*) group by", ( (Object JavaDoc[]) rs.next() )[0].equals( new Integer JavaDoc(3) ) );
524         assertTrue( !rs.hasNext() );
525         rs = s.iterate("select sum(foo.foo.int) from Foo foo");
526         assertTrue( "sum", ( (Integer JavaDoc) rs.next() ).intValue()==4 );
527         assertTrue( !rs.hasNext() );
528         rs = s.iterate("select count(foo) from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING);
529         assertTrue( "id query count", ( (Integer JavaDoc) rs.next() ).intValue()==1 );
530         assertTrue( !rs.hasNext() );
531
532         list = s.find( "from Foo foo where foo.boolean = ?", new Boolean JavaDoc(true), Hibernate.BOOLEAN );
533
534         list = s.find("select new Foo(fo.x) from Fo fo");
535         list = s.find("select new Foo(fo.integer) from Foo fo");
536
537         list = s.createQuery("select new Foo(fo.x) from Foo fo")
538             //.setComment("projection test")
539
.setCacheable(true)
540             .list();
541         assertTrue(list.size()==3);
542         list = s.createQuery("select new Foo(fo.x) from Foo fo")
543             //.setComment("projection test 2")
544
.setCacheable(true)
545             .list();
546         assertTrue(list.size()==3);
547
548         rs = s.iterate("select new Foo(fo.x) from Foo fo");
549         assertTrue( "projection iterate (results)", rs.hasNext() );
550         assertTrue( "projection iterate (return check)", Foo.class.isAssignableFrom( rs.next().getClass() ) );
551
552         ScrollableResults sr = s.createQuery("select new Foo(fo.x) from Foo fo").scroll();
553         assertTrue( "projection scroll (results)", sr.next() );
554         assertTrue( "projection scroll (return check)", Foo.class.isAssignableFrom( sr.get(0).getClass() ) );
555
556         list = s.find("select foo.long, foo.component.name, foo, foo.foo from Foo foo");
557         rs = list.iterator();
558         int count=0;
559         while ( rs.hasNext() ) {
560             count++;
561             Object JavaDoc[] row = (Object JavaDoc[]) rs.next();
562             assertTrue( row[0] instanceof Long JavaDoc );
563             assertTrue( row[1] instanceof String JavaDoc );
564             assertTrue( row[2] instanceof Foo );
565             assertTrue( row[3] instanceof Foo );
566         }
567         assertTrue(count!=0);
568         list = s.find("select avg(foo.float), max(foo.component.name), count(distinct foo.id) from Foo foo");
569         rs = list.iterator();
570         count=0;
571         while ( rs.hasNext() ) {
572             count++;
573             Object JavaDoc[] row = (Object JavaDoc[]) rs.next();
574             assertTrue( row[0] instanceof Float JavaDoc );
575             assertTrue( row[1] instanceof String JavaDoc );
576             assertTrue( row[2] instanceof Integer JavaDoc );
577         }
578         assertTrue(count!=0);
579         list = s.find("select foo.long, foo.component, foo, foo.foo from Foo foo");
580         rs = list.iterator();
581         count=0;
582         while ( rs.hasNext() ) {
583             count++;
584             Object JavaDoc[] row = (Object JavaDoc[]) rs.next();
585             assertTrue( row[0] instanceof Long JavaDoc );
586             assertTrue( row[1] instanceof FooComponent );
587             assertTrue( row[2] instanceof Foo );
588             assertTrue( row[3] instanceof Foo );
589         }
590         assertTrue(count!=0);
591
592         s.save( new Holder("ice T") );
593         s.save( new Holder("ice cube") );
594
595         assertTrue( s.find("from java.lang.Object as o").size()==15 );
596         assertTrue( s.find("from Named").size()==7 );
597         assertTrue( s.find("from Named n where n.name is not null").size()==4 );
598         iter = s.iterate("from Named n");
599         while ( iter.hasNext() ) {
600             assertTrue( iter.next() instanceof Named );
601         }
602
603         s.save( new Holder("bar") );
604         iter = s.iterate("from Named n0, Named n1 where n0.name = n1.name");
605         int cnt = 0;
606         while ( iter.hasNext() ) {
607             Object JavaDoc[] row = (Object JavaDoc[]) iter.next();
608             if ( row[0]!=row[1] ) cnt++;
609         }
610         if ( !(getDialect() instanceof HSQLDialect) ) {
611             assertTrue(cnt==2);
612             assertTrue( s.find("from Named n0, Named n1 where n0.name = n1.name").size()==7 );
613         }
614
615         Query qu = s.createQuery("from Named n where n.name = :name");
616         qu.getReturnTypes();
617         qu.getNamedParameters();
618
619         iter = s.iterate("from java.lang.Object");
620         int c = 0;
621         while ( iter.hasNext() ) {
622             iter.next();
623             c++;
624         }
625         assertTrue(c==16);
626
627         s.iterate("select baz.code, min(baz.count) from Baz baz group by baz.code");
628
629         iter = s.iterate("selecT baz from Baz baz where baz.stringDateMap['foo'] is not null or baz.stringDateMap['bar'] = ?", new Date JavaDoc(), Hibernate.DATE);
630         assertFalse( iter.hasNext() );
631         list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null");
632         assertTrue( list.size()==1 );
633         list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null and baz.stringDateMap['big bang'] < baz.stringDateMap['now']");
634         assertTrue( list.size()==1 );
635         list = s.find("select index(date) from Baz baz join baz.stringDateMap date");
636         System.out.println(list);
637         assertTrue( list.size()==2 );
638
639         s.find("from Foo foo where foo.integer not between 1 and 5 and foo.string not in ('cde', 'abc') and foo.string is not null and foo.integer<=3");
640
641         s.find("from Baz baz inner join baz.collectionComponent.nested.foos foo where foo.string is null");
642         if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) /*&& !(dialect instanceof Oracle9Dialect)*/ ) {
643             s.find("from Baz baz inner join baz.fooSet where '1' in (from baz.fooSet foo where foo.string is not null)");
644             s.find("from Baz baz where 'a' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)");
645             s.find("from Baz baz where 'b' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)");
646         }
647
648         s.find("from Foo foo join foo.foo where foo.foo in ('1','2','3')");
649         if ( !(getDialect() instanceof HSQLDialect) ) s.find("from Foo foo left join foo.foo where foo.foo in ('1','2','3')");
650         s.find("select foo.foo from Foo foo where foo.foo in ('1','2','3')");
651         s.find("select foo.foo.string from Foo foo where foo.foo in ('1','2','3')");
652         s.find("select foo.foo.string from Foo foo where foo.foo.string in ('1','2','3')");
653         s.find("select foo.foo.long from Foo foo where foo.foo.string in ('1','2','3')");
654         s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') or foo.foo.long in (1,2,3)");
655         s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') group by foo.foo.long");
656
657         s.find("from Foo foo1 left join foo1.foo foo2 left join foo2.foo where foo1.string is not null");
658         s.find("from Foo foo1 left join foo1.foo.foo where foo1.string is not null");
659         s.find("from Foo foo1 left join foo1.foo foo2 left join foo1.foo.foo foo3 where foo1.string is not null");
660
661         s.find("select foo.formula from Foo foo where foo.formula > 0");
662
663         int len = s.find("from Foo as foo join foo.foo as foo2 where foo2.id >'a' or foo2.id <'a'").size();
664         assertTrue(len==2);
665
666         s.delete("from Holder");
667         s.flush();
668         s.connection().commit();
669         s.close();
670
671         s = openSession();
672         baz = (Baz) s.createQuery("from Baz baz left outer join fetch baz.manyToAny").uniqueResult();
673         assertTrue( Hibernate.isInitialized( baz.getManyToAny() ) );
674         assertTrue( baz.getManyToAny().size()==2 );
675         BarProxy barp = (BarProxy) baz.getManyToAny().get(0);
676         s.find("from Baz baz join baz.manyToAny");
677         assertTrue( s.find("select baz from Baz baz join baz.manyToAny a where index(a) = 0").size()==1 );
678
679         FooProxy foop = (FooProxy) s.get( Foo.class, foo.getKey() );
680         assertTrue( foop == baz.getManyToAny().get(1) );
681
682         barp.setBaz(baz);
683         assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['now'] is not null").size()==1 );
684         assertTrue( s.find("select bar from Bar bar join bar.baz b where b.stringDateMap['big bang'] < b.stringDateMap['now'] and b.stringDateMap['now'] is not null").size()==1 );
685         assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['big bang'] < bar.baz.stringDateMap['now'] and bar.baz.stringDateMap['now'] is not null").size()==1 );
686
687         list = s.find("select foo.string, foo.component, foo.id from Bar foo");
688         assertTrue ( ( (FooComponent) ( (Object JavaDoc[]) list.get(0) )[1] ).getName().equals("foo") );
689         list = s.find("select elements(baz.components) from Baz baz");
690         assertTrue( list.size()==2 );
691         list = s.find("select bc.name from Baz baz join baz.components bc");
692         assertTrue( list.size()==2 );
693         //list = s.find("select bc from Baz baz join baz.components bc");
694

695         s.createQuery("from Foo foo where foo.integer < 10 order by foo.string").setMaxResults(12).list();
696
697         s.delete(barp);
698         s.delete(baz);
699         s.delete( foop.getFoo() );
700         s.delete(foop);
701         s.flush();
702         s.connection().commit();
703         s.close();
704     }
705
706     public void testCascadeDeleteDetached() throws Exception JavaDoc {
707         Session s = openSession();
708         Baz baz = new Baz();
709         List JavaDoc list = new ArrayList JavaDoc();
710         list.add( new Fee() );
711         baz.setFees(list);
712         s.save(baz);
713         s.flush();
714         s.connection().commit();
715         s.close();
716
717         s = openSession();
718         baz = (Baz) s.get( Baz.class, baz.getCode() );
719         s.connection().commit();
720         s.close();
721
722         assertFalse( Hibernate.isInitialized( baz.getFees() ) );
723
724         s = openSession();
725         s.delete(baz);
726         s.flush();
727         assertFalse( s.iterate("from Fee").hasNext() );
728         s.connection().commit();
729         s.close();
730
731         s = openSession();
732         baz = new Baz();
733         list = new ArrayList JavaDoc();
734         list.add( new Fee() );
735         list.add( new Fee() );
736         baz.setFees(list);
737         s.save(baz);
738         s.flush();
739         s.connection().commit();
740         s.close();
741
742         s = openSession();
743         baz = (Baz) s.get( Baz.class, baz.getCode() );
744         Hibernate.initialize( baz.getFees() );
745         s.connection().commit();
746         s.close();
747
748         assertTrue( baz.getFees().size()==2 );
749
750         s = openSession();
751         s.delete(baz);
752         s.flush();
753         assertFalse( s.iterate("from Fee").hasNext() );
754         s.connection().commit();
755         s.close();
756
757     }
758
759     public void testForeignKeys() throws Exception JavaDoc {
760         Session s = openSession();
761         Baz baz = new Baz();
762         Foo foo = new Foo();
763         List JavaDoc bag = new ArrayList JavaDoc();
764         bag.add(foo);
765         baz.setIdFooBag(bag);
766         baz.setFoo(foo);
767         s.save(baz);
768         s.flush();
769         s.connection().commit();
770         s.close();
771
772         s = openSession();
773         baz = (Baz) s.load( Baz.class, baz.getCode() );
774         s.delete(baz);
775         s.flush();
776         s.connection().commit();
777         s.close();
778     }
779
780     public void testNonlazyCollection() throws Exception JavaDoc {
781         Session s = openSession();
782         Baz baz = new Baz();
783         s.save(baz);
784         s.flush();
785         s.connection().commit();
786         s.close();
787
788         s = openSession();
789         baz = (Baz) s.createCriteria(Baz.class)
790             //.setComment("criteria test")
791
.setFetchMode("stringDateMap", FetchMode.EAGER)
792             .uniqueResult();
793         assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) );
794         assertTrue( Hibernate.isInitialized( baz.getFooComponentToFoo() ) );
795         assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) );
796         assertTrue( Hibernate.isInitialized( baz.getStringDateMap() ) );
797         s.delete(baz);
798         s.flush();
799         s.connection().commit();
800         s.close();
801
802     }
803
804     public void testReuseDeletedCollection() throws Exception JavaDoc {
805         Session s = openSession();
806         Baz baz = new Baz();
807         baz.setDefaults();
808         s.save(baz);
809         s.flush();
810         s.delete(baz);
811         Baz baz2 = new Baz();
812         baz2.setStringArray( new String JavaDoc[] {"x-y-z"} );
813         s.save(baz2);
814         s.flush();
815         s.connection().commit();
816         s.close();
817
818         baz2.setStringSet( baz.getStringSet() );
819         baz2.setStringArray( baz.getStringArray() );
820         baz2.setFooArray( baz.getFooArray() );
821
822         s = openSession();
823         s.update(baz2);
824         s.flush();
825         s.connection().commit();
826         s.close();
827
828         s = openSession();
829         baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
830         assertTrue( baz2.getStringArray().length==3 );
831         assertTrue( baz2.getStringSet().size()==3 );
832         s.delete(baz2);
833         s.flush();
834         s.connection().commit();
835         s.close();
836
837
838     }
839
840     public void testPropertyRef() throws Exception JavaDoc {
841         Session s = openSession();
842         Holder h = new Holder();
843         h.setName("foo");
844         Holder h2 = new Holder();
845         h2.setName("bar");
846         h.setOtherHolder(h2);
847         Serializable JavaDoc hid = s.save(h);
848         Qux q = new Qux();
849         q.setHolder(h2);
850         Serializable JavaDoc qid = s.save(q);
851         s.flush();
852         s.connection().commit();
853         s.close();
854
855         s = openSession();
856         h = (Holder) s.load(Holder.class, hid);
857         assertEquals( h.getName(), "foo");
858         assertEquals( h.getOtherHolder().getName(), "bar");
859         Object JavaDoc[] res = (Object JavaDoc[]) s.find("from Holder h join h.otherHolder oh where h.otherHolder.name = 'bar'").get(0);
860         assertTrue( res[0]==h );
861         q = (Qux) s.get(Qux.class, qid);
862         assertTrue( q.getHolder() == h.getOtherHolder() );
863         s.delete(h);
864         s.delete(q);
865         s.flush();
866         s.connection().commit();
867         s.close();
868     }
869
870     public void testQueryCollectionOfValues() throws Exception JavaDoc {
871         Session s = openSession();
872         Baz baz = new Baz();
873         baz.setDefaults();
874         s.save(baz);
875         Glarch g = new Glarch();
876         Serializable JavaDoc gid = s.save(g);
877
878         if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) /*&& !(dialect instanceof MckoiDialect)*/ && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof TimesTenDialect) ) {
879             s.filter( baz.getFooArray(), "where size(this.bytes) > 0");
880             s.filter( baz.getFooArray(), "where 0 in elements(this.bytes)");
881         }
882         s.flush();
883         s.connection().commit();
884         s.close();
885
886         s = openSession();
887         //s.find("from Baz baz where baz.fooSet.string = 'foo'");
888
//s.find("from Baz baz where baz.fooArray.string = 'foo'");
889
//s.find("from Baz baz where baz.fooSet.foo.string = 'foo'");
890
//s.find("from Baz baz join baz.fooSet.foo foo where foo.string = 'foo'");
891
s.find("from Baz baz join baz.fooSet foo join foo.foo.foo foo2 where foo2.string = 'foo'");
892         s.find("from Baz baz join baz.fooArray foo join foo.foo.foo foo2 where foo2.string = 'foo'");
893         s.find("from Baz baz join baz.stringDateMap date where index(date) = 'foo'");
894         s.find("from Baz baz join baz.topGlarchez g where index(g) = 'A'");
895         s.find("select index(g) from Baz baz join baz.topGlarchez g");
896
897         assertTrue( s.find("from Baz baz left join baz.stringSet").size()==3 );
898         baz = (Baz) s.find("from Baz baz join baz.stringSet str where str='foo'").get(0);
899         assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) );
900         baz = (Baz) s.find("from Baz baz left join fetch baz.stringSet").get(0);
901         assertTrue( Hibernate.isInitialized( baz.getStringSet() ) );
902         assertTrue( s.find("from Baz baz join baz.stringSet string where string='foo'").size()==1 );
903         assertTrue( s.find("from Baz baz inner join baz.components comp where comp.name='foo'").size()==1 );
904         //List bss = s.find("select baz, ss from Baz baz inner join baz.stringSet ss");
905
s.find("from Glarch g inner join g.fooComponents comp where comp.fee is not null");
906         s.find("from Glarch g inner join g.fooComponents comp join comp.fee fee where fee.count > 0");
907         s.find("from Glarch g inner join g.fooComponents comp where comp.fee.count is not null");
908
909         s.delete(baz);
910         //s.delete("from Glarch g");
911
s.delete( s.get(Glarch.class, gid) );
912         s.flush();
913
914         s.connection().commit();
915         s.close();
916
917     }
918
919     public void testBatchLoad() throws Exception JavaDoc {
920         Session s = openSession();
921         Baz baz = new Baz();
922         SortedSet JavaDoc stringSet = new TreeSet JavaDoc();
923         stringSet.add("foo");
924         stringSet.add("bar");
925         Set JavaDoc fooSet = new HashSet JavaDoc();
926         for (int i=0; i<3; i++) {
927             Foo foo = new Foo();
928             s.save(foo);
929             fooSet.add(foo);
930         }
931         baz.setFooSet(fooSet);
932         baz.setStringSet(stringSet);
933         s.save(baz);
934         Baz baz2 = new Baz();
935         fooSet = new HashSet JavaDoc();
936         for (int i=0; i<2; i++) {
937             Foo foo = new Foo();
938             s.save(foo);
939             fooSet.add(foo);
940         }
941         baz2.setFooSet(fooSet);
942         s.save(baz2);
943         Baz baz3 = new Baz();
944         stringSet = new TreeSet JavaDoc();
945         stringSet.add("foo");
946         stringSet.add("baz");
947         baz3.setStringSet(stringSet);
948         s.save(baz3);
949         s.flush();
950         s.connection().commit();
951         s.close();
952
953         s = openSession();
954         baz = (Baz) s.load( Baz.class, baz.getCode() );
955         baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
956         baz3 = (Baz) s.load( Baz.class, baz3.getCode() );
957         assertFalse( Hibernate.isInitialized(baz.getFooSet()) || Hibernate.isInitialized(baz2.getFooSet()) || Hibernate.isInitialized(baz3.getFooSet()) );
958         assertFalse( Hibernate.isInitialized(baz.getStringSet()) || Hibernate.isInitialized(baz2.getStringSet()) || Hibernate.isInitialized(baz3.getStringSet()) );
959         assertTrue( baz.getFooSet().size()==3 );
960         assertTrue( Hibernate.isInitialized(baz.getFooSet()) && Hibernate.isInitialized(baz2.getFooSet()) && Hibernate.isInitialized(baz3.getFooSet()));
961         assertTrue( baz2.getFooSet().size()==2 );
962         assertTrue( baz3.getStringSet().contains("baz") );
963         assertTrue( Hibernate.isInitialized(baz.getStringSet()) && Hibernate.isInitialized(baz2.getStringSet()) && Hibernate.isInitialized(baz3.getStringSet()));
964         assertTrue( baz.getStringSet().size()==2 && baz2.getStringSet().size()==0 );
965         s.delete(baz);
966         s.delete(baz2);
967         s.delete(baz3);
968         Iterator JavaDoc iter = new JoinedIterator( new Iterator JavaDoc[] { baz.getFooSet().iterator(), baz2.getFooSet().iterator() } );
969         while ( iter.hasNext() ) s.delete( iter.next() );
970         s.flush();
971         s.connection().commit();
972         s.close();
973
974     }
975
976     public void testFetchInitializedCollection() throws Exception JavaDoc {
977         Session s = openSession();
978         Baz baz = new Baz();
979         Collection JavaDoc fooBag = new ArrayList JavaDoc();
980         fooBag.add( new Foo() );
981         fooBag.add( new Foo() );
982         baz.setFooBag(fooBag);
983         s.save(baz);
984         s.flush();
985         fooBag = baz.getFooBag();
986         s.find("from Baz baz left join fetch baz.fooBag");
987         assertTrue( fooBag==baz.getFooBag() );
988         s.connection().commit();
989         s.close();
990
991         s = openSession();
992         baz = (Baz) s.load( Baz.class, baz.getCode() );
993         Object JavaDoc bag = baz.getFooBag();
994         assertFalse( Hibernate.isInitialized(bag) );
995         s.find("from Baz baz left join fetch baz.fooBag");
996         assertTrue( bag==baz.getFooBag() );
997         assertTrue( baz.getFooBag().size()==2 );
998         s.delete(baz);
999         s.flush();
1000        s.connection().commit();
1001        s.close();
1002    }
1003
1004    public void testLateCollectionAdd() throws Exception JavaDoc {
1005        Session s = openSession();
1006        Baz baz = new Baz();
1007        List JavaDoc l = new ArrayList JavaDoc();
1008        baz.setStringList(l);
1009        l.add("foo");
1010        Serializable JavaDoc id = s.save(baz);
1011        l.add("bar");
1012        s.flush();
1013        l.add("baz");
1014        s.flush();
1015        s.connection().commit();
1016        s.close();
1017
1018        s = openSession();
1019        baz = (Baz) s.load(Baz.class, id);
1020        assertTrue( baz.getStringList().size()==3 && baz.getStringList().contains("bar") );
1021        s.delete(baz);
1022        s.flush();
1023        s.connection().commit();
1024        s.close();
1025
1026    }
1027
1028    public void testUpdate() throws Exception JavaDoc {
1029        Session s = openSession();
1030        Foo foo = new Foo();
1031        s.save(foo);
1032        s.flush();
1033        s.connection().commit();
1034        s.close();
1035
1036        foo = (Foo) SerializationHelper.deserialize( SerializationHelper.serialize(foo) );
1037
1038        s = openSession();
1039        FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
1040        foo2.setString("dirty");
1041        foo2.setBoolean( new Boolean JavaDoc(false) );
1042        foo2.setBytes( new byte[] { 1,2,3} );
1043        foo2.setDate(null);
1044        foo2.setShort( new Short JavaDoc("69") );
1045        s.flush();
1046        s.connection().commit();
1047        s.close();
1048
1049        s = openSession();
1050        foo2.setString("dirty again");
1051        s.update(foo2);
1052        s.flush();
1053        s.connection().commit();
1054        s.close();
1055
1056        s = openSession();
1057        foo2.setString("dirty again 2");
1058        s.update(foo2);
1059        s.flush();
1060        s.connection().commit();
1061        s.close();
1062
1063        s = openSession();
1064        Foo foo3 = new Foo();
1065        s.load( foo3, foo.getKey() );
1066        // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
1067
assertTrue( "update", foo2.equalsFoo(foo3) );
1068        s.delete(foo3);
1069        s.delete("from Glarch");
1070        s.flush();
1071        s.connection().commit();
1072        s.close();
1073
1074    }
1075
1076    public void testListRemove() throws Exception JavaDoc {
1077        Session s = openSession();
1078        Baz b = new Baz();
1079        List JavaDoc stringList = new ArrayList JavaDoc();
1080        List JavaDoc feeList = new ArrayList JavaDoc();
1081        b.setFees(feeList);
1082        b.setStringList(stringList);
1083        feeList.add( new Fee() );
1084        feeList.add( new Fee() );
1085        feeList.add( new Fee() );
1086        feeList.add( new Fee() );
1087        stringList.add("foo");
1088        stringList.add("bar");
1089        stringList.add("baz");
1090        stringList.add("glarch");
1091        s.save(b);
1092        s.flush();
1093        stringList.remove(1);
1094        feeList.remove(1);
1095        s.flush();
1096        s.evict(b);
1097        s.refresh(b);
1098        assertTrue( b.getFees().size()==3 );
1099        stringList = b.getStringList();
1100        assertTrue(
1101            stringList.size()==3 &&
1102            "baz".equals( stringList.get(1) ) &&
1103            "foo".equals( stringList.get(0) )
1104        );
1105        s.delete(b);
1106        s.delete("from Fee");
1107        s.flush();
1108        s.connection().commit();
1109        s.close();
1110    }
1111
1112    public void testFetchInitializedCollectionDupe() throws Exception JavaDoc {
1113        Session s = openSession();
1114        Baz baz = new Baz();
1115        Collection JavaDoc fooBag = new ArrayList JavaDoc();
1116        fooBag.add( new Foo() );
1117        fooBag.add( new Foo() );
1118        baz.setFooBag(fooBag);
1119        s.save(baz);
1120        s.flush();
1121        fooBag = baz.getFooBag();
1122        s.find("from Baz baz left join fetch baz.fooBag");
1123        assertTrue( Hibernate.isInitialized(fooBag) );
1124        assertTrue( fooBag==baz.getFooBag() );
1125        assertTrue( baz.getFooBag().size()==2 );
1126        s.connection().commit();
1127        s.close();
1128
1129        s = openSession();
1130        baz = (Baz) s.load( Baz.class, baz.getCode() );
1131        Object JavaDoc bag = baz.getFooBag();
1132        assertFalse( Hibernate.isInitialized(bag) );
1133        s.find("from Baz baz left join fetch baz.fooBag");
1134        assertTrue( Hibernate.isInitialized(bag) );
1135        assertTrue( bag==baz.getFooBag() );
1136        assertTrue( baz.getFooBag().size()==2 );
1137        s.delete(baz);
1138        s.flush();
1139        s.connection().commit();
1140        s.close();
1141    }
1142
1143    public void testSortables() throws Exception JavaDoc {
1144        Session s = openSession();
1145        Baz b = new Baz();
1146        b.setName("name");
1147        SortedSet JavaDoc ss = new TreeSet JavaDoc();
1148        ss.add( new Sortable("foo") );
1149        ss.add( new Sortable("bar") );
1150        ss.add( new Sortable("baz") );
1151        b.setSortablez(ss);
1152        s.save(b);
1153        s.flush();
1154        s.connection().commit();
1155        s.close();
1156
1157        s = openSession();
1158        Criteria cr = s.createCriteria(Baz.class);
1159        cr.setFetchMode("topGlarchez", FetchMode.LAZY);
1160        List JavaDoc result = cr
1161            .addOrder( Order.asc("name") )
1162            .list();
1163        assertTrue( result.size()==1 );
1164        b = (Baz) result.get(0);
1165        assertTrue( b.getSortablez().size()==3 );
1166        assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
1167        s.connection().commit();
1168        s.close();
1169
1170        s = openSession();
1171        result = s.createQuery("from Baz baz left join fetch baz.sortablez order by baz.name asc")
1172            .list();
1173        b = (Baz) result.get(0);
1174        assertTrue( b.getSortablez().size()==3 );
1175        assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
1176        s.connection().commit();
1177        s.close();
1178
1179        s = openSession();
1180        result = s.createQuery("from Baz baz order by baz.name asc")
1181            .list();
1182        b = (Baz) result.get(0);
1183        assertTrue( b.getSortablez().size()==3 );
1184        assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
1185        s.delete(b);
1186        s.flush();
1187        s.connection().commit();
1188        s.close();
1189
1190    }
1191
1192    public void testFetchList() throws Exception JavaDoc {
1193        Session s = openSession();
1194        Baz baz = new Baz();
1195        s.save(baz);
1196        Foo foo = new Foo();
1197        s.save(foo);
1198        Foo foo2 = new Foo();
1199        s.save(foo2);
1200        s.flush();
1201        List JavaDoc list = new ArrayList JavaDoc();
1202        for ( int i=0; i<5; i++ ) {
1203            Fee fee = new Fee();
1204            list.add(fee);
1205        }
1206        baz.setFees(list);
1207        list = s.find("from Foo foo, Baz baz left join fetch baz.fees");
1208        assertTrue( Hibernate.isInitialized( ( (Baz) ( (Object JavaDoc[]) list.get(0) )[1] ).getFees() ) );
1209        s.delete(foo);
1210        s.delete(foo2);
1211        s.delete(baz);
1212        s.flush();
1213        s.connection().commit();
1214        s.close();
1215    }
1216
1217    public void testBagOneToMany() throws Exception JavaDoc {
1218        Session s = openSession();
1219        Baz baz = new Baz();
1220        List JavaDoc list = new ArrayList JavaDoc();
1221        baz.setBazez(list);
1222        list.add( new Baz() );
1223        s.save(baz);
1224        s.flush();
1225        list.add( new Baz() );
1226        s.flush();
1227        list.add( 0, new Baz() );
1228        s.flush();
1229        s.delete( list.remove(1) );
1230        s.flush();
1231        s.delete(baz);
1232        s.flush();
1233        s.connection().commit();
1234        s.close();
1235    }
1236
1237    public void testQueryLockMode() throws Exception JavaDoc {
1238
1239        Session s = openSession();
1240        Transaction tx = s.beginTransaction();
1241        Bar bar = new Bar();
1242        s.save(bar);
1243        s.flush();
1244        bar.setString("changed");
1245        Baz baz = new Baz();
1246        baz.setFoo(bar);
1247        s.save(baz);
1248        Query q = s.createQuery("from Foo foo, Bar bar");
1249        if ( !(getDialect() instanceof DB2Dialect) ) {
1250            q.setLockMode("bar", LockMode.UPGRADE);
1251        }
1252        Object JavaDoc[] result = (Object JavaDoc[]) q.uniqueResult();
1253        Object JavaDoc b = result[0];
1254        assertTrue( s.getCurrentLockMode(b)==LockMode.WRITE && s.getCurrentLockMode( result[1] )==LockMode.WRITE );
1255        tx.commit();
1256        s.disconnect();
1257
1258        s.reconnect();
1259        tx = s.beginTransaction();
1260        assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1261        s.find("from Foo foo");
1262        assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1263        q = s.createQuery("from Foo foo");
1264        q.setLockMode("foo", LockMode.READ);
1265        q.list();
1266        assertTrue( s.getCurrentLockMode(b)==LockMode.READ);
1267        s.evict(baz);
1268        tx.commit();
1269        s.disconnect();
1270        
1271        s.reconnect();
1272        tx = s.beginTransaction();
1273        assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1274        s.delete( s.load( Baz.class, baz.getCode() ) );
1275        assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1276        tx.commit();
1277        s.close();
1278
1279        s = openSession();
1280        tx = s.beginTransaction();
1281        q = s.createQuery("from Foo foo, Bar bar, Bar bar2");
1282        if ( !(getDialect() instanceof DB2Dialect) ) {
1283            q.setLockMode("bar", LockMode.UPGRADE);
1284        }
1285        q.setLockMode("bar2", LockMode.READ);
1286        result = (Object JavaDoc[]) q.list().get(0);
1287        if ( !(getDialect() instanceof DB2Dialect) ) {
1288            assertTrue( s.getCurrentLockMode( result[0] )==LockMode.UPGRADE && s.getCurrentLockMode( result[1] )==LockMode.UPGRADE );
1289        }
1290        s.delete( result[0] );
1291        tx.commit();
1292        s.close();
1293    }
1294
1295    public void testManyToManyBag() throws Exception JavaDoc {
1296
1297        Session s = openSession();
1298        Baz baz = new Baz();
1299        Serializable JavaDoc id = s.save(baz);
1300        s.flush();
1301        s.connection().commit();
1302        s.close();
1303
1304        s = openSession();
1305        baz = (Baz) s.load(Baz.class, id);
1306        baz.getFooBag().add( new Foo() );
1307        s.flush();
1308        s.connection().commit();
1309        s.close();
1310
1311        s = openSession();
1312        baz = (Baz) s.load(Baz.class, id);
1313        assertTrue( !Hibernate.isInitialized( baz.getFooBag() ) );
1314        assertTrue( baz.getFooBag().size()==1 );
1315        if ( !(getDialect() instanceof HSQLDialect) ) assertTrue( Hibernate.isInitialized( baz.getFooBag().iterator().next() ) );
1316        s.delete(baz);
1317        s.flush();
1318        s.connection().commit();
1319        s.close();
1320    }
1321
1322    public void testIdBag() throws Exception JavaDoc {
1323        Session s = openSession();
1324        Baz baz = new Baz();
1325        s.save(baz);
1326        List JavaDoc l = new ArrayList JavaDoc();
1327        List JavaDoc l2 = new ArrayList JavaDoc();
1328        baz.setIdFooBag(l);
1329        baz.setByteBag(l2);
1330        l.add( new Foo() );
1331        l.add( new Bar() );
1332        byte[] bytes = "ffo".getBytes();
1333        l2.add(bytes);
1334        l2.add( "foo".getBytes() );
1335        s.flush();
1336        l.add( new Foo() );
1337        l.add( new Bar() );
1338        l2.add( "bar".getBytes() );
1339        s.flush();
1340        s.delete( l.remove(3) );
1341        bytes[1]='o';
1342        s.flush();
1343        s.connection().commit();
1344        s.close();
1345
1346        s = openSession();
1347        baz = (Baz) s.load(Baz.class, baz.getCode());
1348        assertTrue( baz.getIdFooBag().size()==3 );
1349        assertTrue( baz.getByteBag().size()==3 );
1350        bytes = "foobar".getBytes();
1351        Iterator JavaDoc iter = baz.getIdFooBag().iterator();
1352        while ( iter.hasNext() ) s.delete( iter.next() );
1353        baz.setIdFooBag(null);
1354        baz.getByteBag().add(bytes);
1355        baz.getByteBag().add(bytes);
1356        assertTrue( baz.getByteBag().size()==5 );
1357        s.flush();
1358        s.connection().commit();
1359        s.close();
1360
1361        s = openSession();
1362        baz = (Baz) s.load(Baz.class, baz.getCode());
1363        assertTrue( baz.getIdFooBag().size()==0 );
1364        assertTrue( baz.getByteBag().size()==5 );
1365        baz.getIdFooBag().add( new Foo() );
1366        iter = baz.getByteBag().iterator();
1367        iter.next();
1368        iter.remove();
1369        s.flush();
1370        s.connection().commit();
1371        s.close();
1372
1373        s = openSession();
1374        baz = (Baz) s.load(Baz.class, baz.getCode());
1375        assertTrue( baz.getIdFooBag().size()==1 );
1376        assertTrue( baz.getByteBag().size()==4 );
1377        s.delete(baz);
1378        s.flush();
1379        s.connection().commit();
1380        s.close();
1381    }
1382
1383    private boolean isOuterJoinFetchingDisabled() {
1384        return new Integer JavaDoc(0).equals( ( (SessionFactoryImplementor) getSessions() ).getSettings().getMaximumFetchDepth() );
1385    }
1386
1387    public void testForceOuterJoin() throws Exception JavaDoc {
1388
1389        if ( isOuterJoinFetchingDisabled() ) return;
1390
1391        Session s = openSession();
1392        Glarch g = new Glarch();
1393        FooComponent fc = new FooComponent();
1394        fc.setGlarch(g);
1395        FooProxy f = new Foo();
1396        FooProxy f2 = new Foo();
1397        f.setComponent(fc);
1398        f.setFoo(f2);
1399        s.save(f2);
1400        Serializable JavaDoc id = s.save(f);
1401        Serializable JavaDoc gid = s.getIdentifier( f.getComponent().getGlarch() );
1402        s.flush();
1403        s.connection().commit();
1404        s.close();
1405
1406        getSessions().evict(Foo.class);
1407
1408        s = openSession();
1409        f = (FooProxy) s.load(Foo.class, id);
1410        assertFalse( Hibernate.isInitialized(f) );
1411        assertTrue( Hibernate.isInitialized( f.getComponent().getGlarch() ) ); //outer-join="true"
1412
assertFalse( Hibernate.isInitialized( f.getFoo() ) ); //outer-join="auto"
1413
assertEquals( s.getIdentifier( f.getComponent().getGlarch() ), gid );
1414        s.delete(f);
1415        s.delete( f.getFoo() );
1416        s.flush();
1417        s.connection().commit();
1418        s.close();
1419    }
1420
1421    public void testEmptyCollection() throws Exception JavaDoc {
1422        Session s = openSession();
1423        Serializable JavaDoc id = s.save( new Baz() );
1424        s.flush();
1425        s.connection().commit();
1426        s.close();
1427        s = openSession();
1428        Baz baz = (Baz) s.load(Baz.class, id);
1429        Set JavaDoc foos = baz.getFooSet();
1430        assertTrue( foos.size()==0 );
1431        Foo foo = new Foo();
1432        foos.add(foo);
1433        s.save(foo);
1434        s.flush();
1435        s.delete(foo);
1436        s.delete(baz);
1437        s.flush();
1438        s.connection().commit();
1439        s.close();
1440    }
1441
1442    public void testOneToOneGenerator() throws Exception JavaDoc {
1443        Session s = openSession();
1444        X x = new X();
1445        Y y = new Y();
1446        x.setY(y);
1447        y.setTheX(x);
1448        x.getXxs().add( new X.XX(x) );
1449        x.getXxs().add( new X.XX(x) );
1450        Serializable JavaDoc id = s.save(y);
1451        assertEquals( id, s.save(x) );
1452        s.flush();
1453        assertTrue( s.contains(y) && s.contains(x) );
1454        s.connection().commit();
1455        s.close();
1456        assertEquals( new Long JavaDoc(x.getId()), y.getId() );
1457
1458        s = openSession();
1459        x = new X();
1460        y = new Y();
1461        x.setY(y);
1462        y.setTheX(x);
1463        x.getXxs().add( new X.XX(x) );
1464        s.save(y);
1465        s.flush();
1466        assertTrue( s.contains(y) && s.contains(x) );
1467        s.connection().commit();
1468        s.close();
1469        assertEquals( new Long JavaDoc(x.getId()), y.getId() );
1470
1471        s = openSession();
1472        x = new X();
1473        y = new Y();
1474        x.setY(y);
1475        y.setTheX(x);
1476        x.getXxs().add( new X.XX(x) );
1477        x.getXxs().add( new X.XX(x) );
1478        id = s.save(x);
1479        assertEquals( id, y.getId() );
1480        assertEquals( id, new Long JavaDoc( x.getId() ) );
1481        s.flush();
1482        assertTrue( s.contains(y) && s.contains(x) );
1483        s.delete("from X x");
1484        s.flush();
1485        s.connection().commit();
1486        s.close();
1487
1488    }
1489
1490    public void testLimit() throws Exception JavaDoc {
1491        Session s = openSession();
1492        for ( int i=0; i<10; i++ ) s.save( new Foo() );
1493        Iterator JavaDoc iter = s.createQuery("from Foo foo")
1494            .setMaxResults(4)
1495            .setFirstResult(2)
1496            .iterate();
1497        int count=0;
1498        while ( iter.hasNext() ) {
1499            iter.next();
1500            count++;
1501        }
1502        assertTrue(count==4);
1503        iter = s.createQuery("select distinct foo from Foo foo")
1504            .setMaxResults(2)
1505            .setFirstResult(2)
1506            .list()
1507            .iterator();
1508        count=0;
1509        while ( iter.hasNext() ) {
1510            iter.next();
1511            count++;
1512        }
1513        assertTrue(count==2);
1514        iter = s.createQuery("select distinct foo from Foo foo")
1515        .setMaxResults(3)
1516        .list()
1517        .iterator();
1518        count=0;
1519        while ( iter.hasNext() ) {
1520            iter.next();
1521            count++;
1522        }
1523        assertTrue(count==3);
1524        assertTrue( s.delete("from Foo foo")==10 );
1525        s.flush();
1526        s.connection().commit();
1527        s.close();
1528    }
1529
1530    public void testCustom() throws Exception JavaDoc {
1531        GlarchProxy g = new Glarch();
1532        Multiplicity m = new Multiplicity();
1533        m.count = 12;
1534        m.glarch = (Glarch) g;
1535        g.setMultiple(m);
1536        Session s = openSession();
1537        Serializable JavaDoc gid = s.save(g);
1538        s.flush();
1539        s.connection().commit();
1540        s.close();
1541
1542        s = openSession();
1543        g = (Glarch) s.find("from Glarch g where g.multiple.count=12").get(0);
1544        s.connection().commit();
1545        s.close();
1546        
1547        s = openSession();
1548        g = (Glarch) s.find("from Glarch g where g.multiple.glarch=g and g.multiple.count=12").get(0);
1549        assertTrue( g.getMultiple()!=null );
1550        assertEquals( g.getMultiple().count, 12 );
1551        assertSame(g.getMultiple().glarch, g);
1552        s.flush();
1553        s.connection().commit();
1554        s.close();
1555
1556        s = openSession();
1557        g = (GlarchProxy) s.load(Glarch.class, gid);
1558        assertTrue( g.getMultiple()!=null );
1559        assertEquals( g.getMultiple().count, 12 );
1560        assertSame(g.getMultiple().glarch, g);
1561        s.delete(g);
1562        s.flush();
1563        s.connection().commit();
1564        s.close();
1565    }
1566
1567    public void testSaveAddDelete() throws Exception JavaDoc {
1568        Session s = openSession();
1569        Baz baz = new Baz();
1570        Set JavaDoc bars = new HashSet JavaDoc();
1571        baz.setCascadingBars(bars);
1572        s.save(baz);
1573        s.flush();
1574        baz.getCascadingBars().add( new Bar() );
1575        s.delete(baz);
1576        s.flush();
1577        s.connection().commit();
1578        s.close();
1579    }
1580
1581    public void testNamedParams() throws Exception JavaDoc {
1582        Bar bar = new Bar();
1583        Bar bar2 = new Bar();
1584        bar.setName("Bar");
1585        bar2.setName("Bar Two");
1586        bar.setX(10);
1587        bar2.setX(1000);Baz baz = new Baz();
1588        baz.setCascadingBars( new HashSet JavaDoc() );
1589        baz.getCascadingBars().add(bar);
1590        bar.setBaz(baz);
1591        Session s = openSession();
1592        s.save(baz);
1593        s.save(bar2);
1594
1595        List JavaDoc list = s.find("from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'");
1596        Object JavaDoc row = list.iterator().next();
1597        assertTrue( row instanceof Object JavaDoc[] && ( (Object JavaDoc[]) row ).length==3 );
1598
1599        Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'");
1600        list = q.list();
1601        if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
1602
1603        q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal");
1604        HashSet JavaDoc nameList = new HashSet JavaDoc();
1605        nameList.add("bar");
1606        nameList.add("Bar");
1607        nameList.add("Bar Two");
1608        q.setParameterList("nameList", nameList);
1609        q.setParameter("stringVal", "a string");
1610        list = q.list();
1611        if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
1612
1613        try {
1614            q.setParameterList("nameList", (Collection JavaDoc)null);
1615            fail("Should throw an queryexception when passing a null!");
1616        } catch (QueryException qe) {
1617            //should happen
1618
}
1619
1620
1621        if (
1622            !( getDialect() instanceof Oracle9Dialect) &&
1623            !( getDialect() instanceof MySQLDialect) &&
1624            !( getDialect() instanceof DB2Dialect) &&
1625            !( getDialect() instanceof HSQLDialect) &&
1626            !( getDialect() instanceof SQLServerDialect) &&
1627            !( getDialect() instanceof SybaseDialect) &&
1628            !( getDialect() instanceof PostgreSQLDialect) &&
1629            !( getDialect() instanceof TimesTenDialect)
1630        ) { // oracle barfs on "x in ()"
1631
q.setParameterList("nameList", Collections.EMPTY_LIST);
1632            list = q.list();
1633            assertTrue( list.size()==0 );
1634        }
1635
1636        q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");
1637        Object JavaDoc result = q.uniqueResult();
1638        assertTrue( result!=null );
1639        q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name");
1640        q.setString("name", "Bar%");
1641        list = q.list();
1642        assertTrue( list.size()==1 );
1643
1644
1645        // This test added for issue HB-297 - there is an named parameter in the Order By clause
1646
q = s.createQuery("select bar from Bar bar order by ((bar.x - :valueX)*(bar.x - :valueX))");
1647        q.setInteger("valueX", bar.getX()+1);
1648        list = q.list();
1649        assertTrue( ((Bar)list.get(0)).getX() == bar.getX());
1650        q.setInteger("valueX", bar2.getX()+1);
1651        list = q.list();
1652        assertTrue( ((Bar)list.get(0)).getX() == bar2.getX());
1653
1654        s.delete(baz);
1655        s.delete(bar2);
1656        s.flush();
1657        s.connection().commit();
1658        s.close();
1659    }
1660
1661    public void testParameterCheck() throws HibernateException {
1662        Session s = openSession();
1663        try {
1664            Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");
1665            q.list();
1666            fail("Should throw QueryException for missing myX");
1667        }
1668        catch (QueryException iae) {
1669            // should happen
1670
}
1671        finally {
1672            s.close();
1673        }
1674
1675        s = openSession();
1676        try {
1677            Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");
1678            q.list();
1679            fail("Should throw QueryException for missing ?");
1680        }
1681        catch (QueryException iae) {
1682            // should happen
1683
}
1684        finally {
1685            s.close();
1686        }
1687
1688        s = openSession();
1689        try {
1690            Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");
1691            q.setInteger(0, 1);
1692            q.list();
1693        }
1694        catch (QueryException iae) {
1695            fail("Should not throw QueryException for missing ?");
1696        }
1697        finally {
1698            s.close();
1699        }
1700
1701        s = openSession();
1702        try {
1703            Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'");
1704            q.list();
1705        }
1706        catch (QueryException iae) {
1707            fail("Should not throw QueryException for ? in quotes");
1708        }
1709        finally {
1710            s.close();
1711        }
1712
1713        s = openSession();
1714        try {
1715            Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");
1716            q.setParameter(0, "bull");
1717            q.setParameter(2, "shit");
1718            q.list();
1719            fail("should throw exception telling me i have not set parameter 1");
1720        }
1721        catch (QueryException iae) {
1722            // should happen!
1723
}
1724        finally {
1725            s.close();
1726        }
1727    }
1728    public void testDyna() throws Exception JavaDoc {
1729        Session s = openSession();
1730        GlarchProxy g = new Glarch();
1731        g.setName("G");
1732        Serializable JavaDoc id = s.save(g);
1733        s.flush();
1734        s.connection().commit();
1735        s.close();
1736
1737        s = openSession();
1738        g = (GlarchProxy) s.load(Glarch.class, id);
1739        assertTrue( g.getName().equals("G") );
1740        assertTrue( g.getDynaBean().get("foo").equals("foo") && g.getDynaBean().get("bar").equals( new Integer JavaDoc(66) ) );
1741        assertTrue( ! (g instanceof Glarch) );
1742        g.getDynaBean().put("foo", "bar");
1743        s.flush();
1744        s.connection().commit();
1745        s.close();
1746
1747        s = openSession();
1748        g = (GlarchProxy) s.load(Glarch.class, id);
1749        assertTrue( g.getDynaBean().get("foo").equals("bar") && g.getDynaBean().get("bar").equals( new Integer JavaDoc(66) ) );
1750        g.setDynaBean(null);
1751        s.flush();
1752        s.connection().commit();
1753        s.close();
1754
1755        s = openSession();
1756        g = (GlarchProxy) s.load(Glarch.class, id);
1757        assertTrue( g.getDynaBean()==null );
1758        s.delete(g);
1759        s.flush();
1760        s.connection().commit();
1761        s.close();
1762    }
1763
1764    public void testFindByCriteria() throws Exception JavaDoc {
1765        if ( getDialect() instanceof DB2Dialect ) return;
1766        Session s = openSession();
1767        Foo f = new Foo();
1768        s.save(f);
1769        s.flush();
1770
1771        List JavaDoc list = s.createCriteria(Foo.class)
1772            .add( Expression.eq( "integer", f.getInteger() ) )
1773            .add( Expression.eqProperty("integer", "integer") )
1774            .add( Expression.like( "string", f.getString().toUpperCase() ).ignoreCase() )
1775            .add( Expression.in( "boolean", new Boolean JavaDoc[] { f.getBoolean(), f.getBoolean() } ) )
1776            .setFetchMode("foo", FetchMode.JOIN)
1777            .setFetchMode("baz", FetchMode.SELECT)
1778            .setFetchMode("abstracts", FetchMode.JOIN)
1779            .list();
1780        assertTrue( list.size()==1 && list.get(0)==f );
1781
1782        list = s.createCriteria(Foo.class).add(
1783                Expression.disjunction()
1784                    .add( Expression.eq( "integer", f.getInteger() ) )
1785                    .add( Expression.like( "string", f.getString() ) )
1786                    .add( Expression.eq( "boolean", f.getBoolean() ) )
1787            )
1788            .add( Expression.isNotNull("boolean") )
1789            .list();
1790        assertTrue( list.size()==1 && list.get(0)==f );
1791
1792        Foo example = new Foo();
1793        example.setString("a STRing");
1794        list = s.createCriteria(Foo.class).add(
1795            Example.create(example)
1796                .excludeZeroes()
1797                .ignoreCase()
1798                .excludeProperty("bool")
1799                .excludeProperty("char")
1800                .excludeProperty("yesno")
1801            )
1802            .list();
1803        assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f );
1804        example.setString("rin");
1805
1806        list = s.createCriteria(Foo.class).add(
1807            Example.create(example)
1808                .excludeZeroes()
1809                .enableLike(MatchMode.ANYWHERE)
1810                .excludeProperty("bool")
1811                .excludeProperty("char")
1812                .excludeProperty("yesno")
1813            )
1814            .list();
1815        assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f );
1816
1817        list = s.createCriteria(Foo.class)
1818            .add( Expression.or(
1819                Expression.and(
1820                    Expression.eq( "integer", f.getInteger() ),
1821                    Expression.like( "string", f.getString() )
1822                ),
1823                Expression.eq( "boolean", f.getBoolean() )
1824            ) )
1825            .list();
1826        assertTrue( list.size()==1 && list.get(0)==f );
1827        list = s.createCriteria(Foo.class)
1828            .setMaxResults(5)
1829            .addOrder( Order.asc("date") )
1830            .list();
1831        assertTrue( list.size()==1 && list.get(0)==f );
1832        if(!(getDialect() instanceof TimesTenDialect)) {
1833            list = s.createCriteria(Foo.class).setMaxResults(0).list();
1834            assertTrue( list.size()==0 );
1835        }
1836        list = s.createCriteria(Foo.class)
1837            .setFirstResult(1)
1838            .addOrder( Order.asc("date") )
1839            .addOrder( Order.desc("string") )
1840            .list();
1841        assertTrue( list.size()==0 );
1842        list = s.createCriteria(Foo.class)
1843            .setFetchMode("component.importantDates", FetchMode.EAGER)
1844            .list();
1845        assertTrue( list.size()==3 );
1846
1847        list = s.createCriteria(Foo.class)
1848            .setFetchMode("component.importantDates", FetchMode.EAGER)
1849            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
1850            .list();
1851        assertTrue( list.size()==1 );
1852
1853        f.setFoo( new Foo() );
1854        s.save( f.getFoo() );
1855        s.flush();
1856        s.connection().commit();
1857        s.close();
1858
1859        s = openSession();
1860        list = s.createCriteria(Foo.class)
1861            .add( Expression.eq( "integer", f.getInteger() ) )
1862            .add( Expression.like( "string", f.getString() ) )
1863            .add( Expression.in( "boolean", new Boolean JavaDoc[] { f.getBoolean(), f.getBoolean() } ) )
1864            .add( Expression.isNotNull("foo") )
1865            .setFetchMode("foo", FetchMode.EAGER)
1866            .setFetchMode("baz", FetchMode.LAZY)
1867            .setFetchMode("component.glarch", FetchMode.LAZY)
1868            .setFetchMode("foo.baz", FetchMode.LAZY)
1869            .setFetchMode("foo.component.glarch", FetchMode.LAZY)
1870            .list();
1871        f = (Foo) list.get(0);
1872        assertTrue( Hibernate.isInitialized( f.getFoo() ) );
1873        assertTrue( !Hibernate.isInitialized( f.getComponent().getGlarch() ) );
1874
1875        s.save( new Bar() );
1876        list = s.createCriteria(Bar.class)
1877            .list();
1878        assertTrue( list.size()==1 );
1879        assertTrue( s.createCriteria(Foo.class).list().size()==3 );
1880        s.delete( list.get(0) );
1881
1882        s.delete( f.getFoo() );
1883        s.delete(f);
1884        s.flush();
1885        s.connection().commit();
1886        s.close();
1887    }
1888
1889    public void testAfterDelete() throws Exception JavaDoc {
1890        Session s = openSession();
1891        Foo foo = new Foo();
1892        s.save(foo);
1893        s.flush();
1894        s.delete(foo);
1895        s.save(foo);
1896        s.delete(foo);
1897        s.flush();
1898        s.connection().commit();
1899        s.close();
1900    }
1901
1902    public void testCollectionWhere() throws Exception JavaDoc {
1903        Foo foo1 = new Foo();
1904        Foo foo2 = new Foo();
1905        Baz baz = new Baz();
1906        Foo[] arr = new Foo[10];
1907        arr[0] = foo1;
1908        arr[9] = foo2;
1909        Session s = openSession();
1910        s.save(foo1);
1911        s.save(foo2);
1912        baz.setFooArray(arr);
1913        s.save(baz);
1914        s.flush();
1915        s.connection().commit();
1916        s.close();
1917
1918        s = openSession();
1919        baz = (Baz) s.load( Baz.class, baz.getCode() );
1920        assertTrue( baz.getFooArray().length==1 );
1921        assertTrue( s.find("from Baz baz join baz.fooArray foo").size()==1 );
1922        assertTrue( s.find("from Foo foo").size()==2 );
1923        assertTrue( s.filter( baz.getFooArray(), "" ).size()==1 );
1924        //assertTrue( s.delete("from java.lang.Object o")==9 );
1925
s.delete("from Foo foo");
1926        String JavaDoc bazid = baz.getCode();
1927        s.delete(baz);
1928        int rows=s.connection().createStatement().executeUpdate(
1929            "delete from fooArray where id_='" + bazid + "' and i>=8"
1930        );
1931        assertTrue(rows==1);
1932        s.flush();
1933        s.connection().commit();
1934        s.close();
1935    }
1936
1937    public void testComponentParent() throws Exception JavaDoc {
1938        Session s = openSession();
1939        Transaction t = s.beginTransaction();
1940        BarProxy bar = new Bar();
1941        bar.setBarComponent( new FooComponent() );
1942        Baz baz = new Baz();
1943        baz.setComponents( new FooComponent[] { new FooComponent(), new FooComponent() } );
1944        s.save(bar);
1945        s.save(baz);
1946        t.commit();
1947        s.close();
1948        s = openSession();
1949        t = s.beginTransaction();
1950        bar = (BarProxy) s.load(Bar.class, bar.getKey());
1951        s.load(baz, baz.getCode());
1952        assertTrue( bar.getBarComponent().getParent()==bar );
1953        assertTrue( baz.getComponents()[0].getBaz()==baz && baz.getComponents()[1].getBaz()==baz );
1954        s.delete(baz);
1955        s.delete(bar);
1956        t.commit();
1957        s.close();
1958    }
1959
1960    public void testCollectionCache() throws Exception JavaDoc {
1961        Session s = openSession();
1962        Baz baz = new Baz();
1963        baz.setDefaults();
1964        s.save(baz);
1965        s.flush();
1966        s.connection().commit();
1967        s.close();
1968
1969        s = openSession();
1970        s.load( Baz.class, baz.getCode() );
1971        s.flush();
1972        s.connection().commit();
1973        s.close();
1974
1975        s = openSession();
1976        baz = (Baz) s.load( Baz.class, baz.getCode() );
1977        s.delete(baz);
1978        s.flush();
1979        s.connection().commit();
1980        s.close();
1981    }
1982
1983    public void ntestAssociationId() throws Exception JavaDoc {
1984        Session s = openSession();
1985        Transaction t = s.beginTransaction();
1986        Bar bar = new Bar();
1987        String JavaDoc id = (String JavaDoc) s.save(bar);
1988        MoreStuff more = new MoreStuff();
1989        more.setName("More Stuff");
1990        more.setIntId(12);
1991        more.setStringId("id");
1992        Stuff stuf = new Stuff();
1993        stuf.setMoreStuff(more);
1994        more.setStuffs( new ArrayList JavaDoc() );
1995        more.getStuffs().add(stuf);
1996        stuf.setFoo(bar);
1997        stuf.setId(1234);
1998        stuf.setProperty( TimeZone.getDefault() );
1999        s.save(more);
2000        t.commit();
2001        s.close();
2002
2003        s = openSession();
2004        t = s.beginTransaction();
2005        assertTrue( s.find(
2006            "from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?",
2007            new Object JavaDoc[] { bar, new Long JavaDoc(1234), new Integer JavaDoc(12), "id" },
2008            new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING }
2009        ).size()==1 );
2010        assertTrue( s.find(
2011            "from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?",
2012            new Object JavaDoc[] { bar, new Long JavaDoc(1234), "More Stuff" },
2013            new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING }
2014        ).size()==1 );
2015        s.find("from Stuff as s where s.foo.string is not null");
2016        assertTrue(
2017            s.find("from Stuff as s where s.foo > '0' order by s.foo").size()==1
2018        );
2019        //s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list();
2020
t.commit();
2021        s.close();
2022
2023        s = openSession();
2024        t = s.beginTransaction();
2025        FooProxy foo = (FooProxy) s.load(Foo.class, id);
2026        s.load(more, more);
2027        t.commit();
2028        s.close();
2029
2030        s = openSession();
2031        t = s.beginTransaction();
2032        Stuff stuff = new Stuff();
2033        stuff.setFoo(foo);
2034        stuff.setId(1234);
2035        stuff.setMoreStuff(more);
2036        s.load(stuff, stuff);
2037        assertTrue( stuff.getProperty().equals( TimeZone.getDefault() ) );
2038        assertTrue( stuff.getMoreStuff().getName().equals("More Stuff") );
2039        s.delete("from MoreStuff");
2040        s.delete("from Foo foo");
2041        t.commit();
2042        s.close();
2043    }
2044
2045    public void testCascadeSave() throws Exception JavaDoc {
2046        Session s = openSession();
2047        Transaction t = s.beginTransaction();
2048        Baz baz = new Baz();
2049        List JavaDoc list = new ArrayList JavaDoc();
2050        list.add( new Fee() );
2051        list.add( new Fee() );
2052        baz.setFees(list);
2053        s.save(baz);
2054        t.commit();
2055        s.close();
2056
2057        s = openSession();
2058        t = s.beginTransaction();
2059        baz = (Baz) s.load( Baz.class, baz.getCode() );
2060        assertTrue( baz.getFees().size()==2 );
2061        s.delete(baz);
2062        assertTrue( !s.iterate("from Fee fee").hasNext() );
2063        t.commit();
2064        s.close();
2065
2066    }
2067
2068    public void testCollectionsInSelect() throws Exception JavaDoc {
2069        Session s = openSession();
2070        Transaction t = s.beginTransaction();
2071        Foo[] foos = new Foo[] { null, new Foo() };
2072        s.save( foos[1] );
2073        Baz baz = new Baz();
2074        baz.setDefaults();
2075        baz.setFooArray(foos);
2076        s.save(baz);
2077        Baz baz2 = new Baz();
2078        baz2.setDefaults();
2079        s.save(baz2);
2080
2081        Bar bar = new Bar();
2082        bar.setBaz(baz);
2083        s.save(bar);
2084
2085        List JavaDoc list = s.find("select new Result(foo.string, foo.long, foo.integer) from Foo foo");
2086        assertTrue( list.size()==2 && ( list.get(0) instanceof Result ) && ( list.get(1) instanceof Result ) );
2087        /*list = s.find("select new Result( baz.name, foo.long, count(elements(baz.fooArray)) ) from Baz baz join baz.fooArray foo group by baz.name, foo.long");
2088        assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
2089        Result r = ((Result) list.get(0) );
2090        assertEquals( r.getName(), baz.getName() );
2091        assertEquals( r.getCount(), 1 );
2092        assertEquals( r.getAmount(), foos[1].getLong().longValue() );*/

2093        list = s.find("select new Result( baz.name, max(foo.long), count(foo) ) from Baz baz join baz.fooArray foo group by baz.name");
2094        assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
2095        Result r = ((Result) list.get(0) );
2096        assertEquals( r.getName(), baz.getName() );
2097        assertEquals( r.getCount(), 1 );
2098        assertTrue( r.getAmount() > 696969696969696000l );
2099
2100
2101        //s.find("select max( elements(bar.baz.fooArray) ) from Bar as bar");
2102
//The following test is disabled for databases with no subselects...also for Interbase (not sure why).
2103
if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) /*&& !(dialect instanceof MckoiDialect)*/ && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) ) {
2104            s.find("select count(*) from Baz as baz where 1 in indices(baz.fooArray)");
2105            s.find("select count(*) from Bar as bar where 'abc' in elements(bar.baz.fooArray)");
2106            s.find("select count(*) from Bar as bar where 1 in indices(bar.baz.fooArray)");
2107            if ( !(getDialect() instanceof DB2Dialect) && !(getDialect() instanceof Oracle9Dialect) ) {
2108                s.find("select count(*) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)");
2109                s.find("select max( elements(bar.baz.fooArray) ) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)");
2110            }
2111            s.find("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')");
2112            s.find("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')");
2113            s.find("select count(*) from Bar as bar left outer join bar.component.glarch.proxyArray as pg where '1' in (from g in bar.component.glarch.proxyArray)");
2114        }
2115
2116        list = s.find("from Baz baz left join baz.fooToGlarch join fetch baz.fooArray foo left join fetch foo.foo");
2117        assertTrue( list.size()==1 && ( (Object JavaDoc[]) list.get(0) ).length==2 );
2118
2119        s.find("select baz.name from Bar bar inner join bar.baz baz inner join baz.fooSet foo where baz.name = bar.string");
2120        s.find("SELECT baz.name FROM Bar AS bar INNER JOIN bar.baz AS baz INNER JOIN baz.fooSet AS foo WHERE baz.name = bar.string");
2121
2122        if ( !( getDialect() instanceof HSQLDialect ) ) s.find("select baz.name from Bar bar join bar.baz baz left outer join baz.fooSet foo where baz.name = bar.string");
2123
2124        s.find("select baz.name from Bar bar join bar.baz baz join baz.fooSet foo where baz.name = bar.string");
2125        s.find("SELECT baz.name FROM Bar AS bar JOIN bar.baz AS baz JOIN baz.fooSet AS foo WHERE baz.name = bar.string");
2126
2127        if ( !( getDialect() instanceof HSQLDialect ) ) {
2128            s.find("select baz.name from Bar bar left join bar.baz baz left join baz.fooSet foo where baz.name = bar.string");
2129            s.find("select foo.string from Bar bar left join bar.baz.fooSet foo where bar.string = foo.string");
2130        }
2131
2132        s.find("select baz.name from Bar bar left join bar.baz baz left join baz.fooArray foo where baz.name = bar.string");
2133        s.find("select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string");
2134
2135        s.find("select bar.string, foo.string from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo where baz.name = 'name'");
2136        s.find("select foo from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo");
2137        s.find("select foo from Bar bar inner join bar.baz.fooSet as foo");
2138
2139        s.find("select bar.string, foo.string from Bar bar join bar.baz as baz join baz.fooSet as foo where baz.name = 'name'");
2140        s.find("select foo from Bar bar join bar.baz as baz join baz.fooSet as foo");
2141        s.find("select foo from Bar bar join bar.baz.fooSet as foo");
2142
2143        assertTrue( s.find("from Bar bar join bar.baz.fooArray foo").size()==1 );
2144
2145        assertTrue( s.find("from Bar bar join bar.baz.fooSet foo").size()==0 );
2146        assertTrue( s.find("from Bar bar join bar.baz.fooArray foo").size()==1 );
2147
2148        s.delete(bar);
2149
2150        if ( getDialect() instanceof DB2Dialect || getDialect() instanceof PostgreSQLDialect ) {
2151            s.iterate("select one from One one join one.manies many group by one order by count(many)");
2152            s.iterate("select one from One one join one.manies many group by one having count(many) < 5");
2153        }
2154
2155        s.find("from One one join one.manies many where one.id = 1 and many.id = 1");
2156        s.iterate("select one.id, elements(one.manies) from One one");
2157        s.iterate("select max( elements(one.manies) ) from One one");
2158        s.find("select one, elements(one.manies) from One one");
2159        //s.iterate("select one, max( elements(one.manies) ) from One one group by one");
2160
Iterator JavaDoc iter = s.iterate("select elements(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2161        //WAS: assertTrue( iter.next()==null && iter.next()==foos[1] && !iter.hasNext() );
2162
assertTrue( iter.next()==foos[1] && !iter.hasNext() );
2163        list = s.find("select elements(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2164        //WAS: assertTrue( list.size()==2 );
2165
assertTrue( list.size()==1 );
2166        iter = s.iterate("select indices(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2167        //WAS: assertTrue( iter.next().equals( new Integer(0) ) && iter.next().equals( new Integer(1) ) && !iter.hasNext() );
2168
assertTrue( iter.next().equals( new Integer JavaDoc(1) ) && !iter.hasNext() );
2169
2170        //assertTrue( s.iterate("select max( elements(baz.timeArray) ) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next() instanceof Time );
2171
//assertTrue( s.iterate("select max( elements(baz.stringSet) ) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next().equals("foo") );
2172
assertTrue( s.iterate("select size(baz.stringSet) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next().equals( new Integer JavaDoc(3) ) );
2173        //s.find("from One one where sum one.manies.elements =0 or 1 = min one.manies.elements");
2174

2175        s.find("from Foo foo where foo.component.glarch.id is not null");
2176
2177        //iter = s.iterate("select baz, max( elements(baz.timeArray) ) from Baz baz group by baz");
2178
//while ( iter.hasNext() ) { Object[] arr = (Object[]) iter.next(); System.out.println( arr[0] + " " + arr[1] ); }
2179
iter = s.iterate("select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from Baz baz group by baz");
2180        while ( iter.hasNext() ) { Object JavaDoc[] arr = (Object JavaDoc[]) iter.next(); System.out.println( arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3] ); }
2181        
2182        s.delete(baz);
2183        s.delete(baz2);
2184        s.delete( foos[1] );
2185        t.commit();
2186        s.close();
2187    }
2188
2189    public void testNewFlushing() throws Exception JavaDoc {
2190        Session s = openSession();
2191        Baz baz = new Baz();
2192        baz.setDefaults();
2193        s.save(baz);
2194        s.flush();
2195        baz.getStringArray()[0] = "a new value";
2196        Iterator JavaDoc iter = s.iterate("from Baz baz");//no flush
2197
assertTrue( iter.next()==baz );
2198        iter = s.iterate("select elements(baz.stringArray) from Baz baz");
2199        boolean found = false;
2200        while ( iter.hasNext() ) {
2201            if ( iter.next().equals("a new value") ) found = true;
2202        }
2203        assertTrue(found);
2204        baz.setStringArray(null);
2205        s.iterate("from Baz baz"); //no flush
2206
iter = s.iterate("select elements(baz.stringArray) from Baz baz");
2207        assertTrue( !iter.hasNext() );
2208        baz.getStringList().add("1E1");
2209        iter = s.iterate("from Foo foo");//no flush
2210
assertTrue( !iter.hasNext() );
2211        iter = s.iterate("select elements(baz.stringList) from Baz baz");
2212        found = false;
2213        while ( iter.hasNext() ) {
2214            if ( iter.next().equals("1E1") ) found = true;
2215        }
2216        assertTrue(found);
2217        baz.getStringList().remove("1E1");
2218        iter = s.iterate("select elements(baz.stringArray) from Baz baz"); //no flush
2219
iter = s.iterate("select elements(baz.stringList) from Baz baz");
2220        found = false;
2221        while ( iter.hasNext() ) {
2222            if ( iter.next().equals("1E1") ) found = true;
2223        }
2224        assertTrue(!found);
2225
2226        List JavaDoc newList = new ArrayList JavaDoc();
2227        newList.add("value");
2228        baz.setStringList(newList);
2229        iter = s.iterate("from Foo foo");//no flush
2230
baz.setStringList(null);
2231        iter = s.iterate("select elements(baz.stringList) from Baz baz");
2232        assertTrue( !iter.hasNext() );
2233
2234        baz.setStringList(newList);
2235        iter = s.iterate("from Foo foo");//no flush
2236
iter = s.iterate("select elements(baz.stringList) from Baz baz");
2237        assertTrue( iter.hasNext() );
2238
2239        s.delete(baz);
2240        s.flush();
2241        s.connection().commit();
2242        s.close();
2243    }
2244
2245    public void testPersistCollections() throws Exception JavaDoc {
2246
2247        Session s = openSession();
2248        assertTrue( ( (Integer JavaDoc) s.iterate("select count(*) from Bar").next() ).intValue()==0 );
2249        assertTrue( s.iterate("select count(*) from Bar b").next().equals( new Integer JavaDoc(0) ) );
2250        assertFalse( s.iterate("from Glarch g").hasNext() );
2251
2252        Baz baz = new Baz();
2253        s.save(baz);
2254        baz.setDefaults();
2255        baz.setStringArray( new String JavaDoc[] { "stuff" } );
2256        Set JavaDoc bars = new HashSet JavaDoc();
2257        bars.add( new Bar() );
2258        baz.setCascadingBars(bars);
2259        HashMap JavaDoc sgm = new HashMap JavaDoc();
2260        sgm.put( "a", new Glarch() );
2261        sgm.put( "b", new Glarch() );
2262        baz.setStringGlarchMap(sgm);
2263        //System.out.println( s.print(baz) );
2264
s.flush();
2265        s.connection().commit();
2266        s.close();
2267
2268        s = openSession();
2269        assertTrue( ( (Integer JavaDoc) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2270        baz = (Baz) ( (Object JavaDoc[]) s.find("select baz, baz from Baz baz").get(0) )[1];
2271        assertTrue( baz.getCascadingBars().size()==1 );
2272        //System.out.println( s.print(baz) );
2273
Foo foo = new Foo();
2274        s.save(foo);
2275        Foo foo2 = new Foo() ;
2276        s.save(foo2);
2277        baz.setFooArray( new Foo[] { foo, foo, null, foo2 } );
2278        baz.getFooSet().add(foo);
2279        baz.getCustoms().add( new String JavaDoc[] { "new", "custom" } );
2280        baz.setStringArray(null);
2281        baz.getStringList().set(0, "new value");
2282        baz.setStringSet( new TreeSet JavaDoc() );
2283        Time JavaDoc time = new java.sql.Time JavaDoc(12345);
2284        baz.getTimeArray()[2] = time;
2285        //System.out.println(time);
2286

2287        assertTrue( baz.getStringGlarchMap().size()==1 );
2288
2289        //The following test is disabled databases with no subselects
2290
if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect) ) {
2291            List JavaDoc list = s.find("select foo from Foo foo, Baz baz where foo in elements(baz.fooArray) and 3 = some elements(baz.intArray) and 4 > all indices(baz.intArray)");
2292            assertTrue( "collection.elements find", list.size()==2 );
2293        }
2294        if (!(getDialect() instanceof SAPDBDialect) ) { // SAPDB doesn't like distinct with binary type
2295
List JavaDoc list = s.find("select distinct foo from Baz baz join baz.fooArray foo");
2296            assertTrue( "collection.elements find", list.size()==2 );
2297        }
2298
2299        List JavaDoc list = s.find("select foo from Baz baz join baz.fooSet foo");
2300        assertTrue( "association.elements find", list.size()==1 );
2301        s.flush();
2302        s.connection().commit();
2303        s.close();
2304
2305        s = openSession();
2306        assertTrue( ( (Integer JavaDoc) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2307        baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2308        assertTrue( "collection of custom types - added element", baz.getCustoms().size()==4 && baz.getCustoms().get(0)!=null );
2309        assertTrue ( "component of component in collection", baz.getComponents()[1].getSubcomponent()!=null );
2310        assertTrue( baz.getComponents()[1].getBaz()==baz );
2311        assertTrue( "set of objects", ( (FooProxy) baz.getFooSet().iterator().next() ).getKey().equals( foo.getKey() ));
2312        assertTrue( "collection removed", baz.getStringArray().length==0 );
2313        assertTrue( "changed element", baz.getStringList().get(0).equals("new value"));
2314        assertTrue( "replaced set", baz.getStringSet().size()==0 );
2315        assertTrue( "array element change", baz.getTimeArray()[2]!=null );
2316        assertTrue( baz.getCascadingBars().size()==1 );
2317        //System.out.println( s.print(baz) );
2318
baz.getStringSet().add("two");
2319        baz.getStringSet().add("one");
2320        baz.getBag().add("three");
2321        s.flush();
2322        s.connection().commit();
2323        s.close();
2324        s = openSession();
2325        baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2326        assertTrue( baz.getStringSet().size()==2 );
2327        assertTrue( baz.getStringSet().first().equals("one") );
2328        assertTrue( baz.getStringSet().last().equals("two") );
2329        assertTrue( baz.getBag().size()==5 );
2330        baz.getStringSet().remove("two");
2331        baz.getBag().remove("duplicate");
2332        s.flush();
2333        s.connection().commit();
2334        s.close();
2335
2336        s = openSession();
2337        assertTrue( ( (Integer JavaDoc) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2338        baz = (Baz) s.load(Baz.class, baz.getCode());
2339        assertTrue( baz.getCascadingBars().size()==1 );
2340        Bar bar = new Bar();
2341        Bar bar2 = new Bar();
2342        s.save(bar); s.save(bar2);
2343        baz.setTopFoos( new HashSet JavaDoc() );
2344        baz.getTopFoos().add(bar);
2345        baz.getTopFoos().add(bar2);
2346        assertTrue( baz.getCascadingBars().size()==1 );
2347        baz.setTopGlarchez( new TreeMap JavaDoc() );
2348        GlarchProxy g = new Glarch();
2349        s.save(g);
2350        baz.getTopGlarchez().put( new Character JavaDoc('G'), g );
2351        HashMap JavaDoc map = new HashMap JavaDoc();
2352        map.put(bar, g);
2353        map.put(bar2, g);
2354        baz.setFooToGlarch(map);
2355        map = new HashMap JavaDoc();
2356        map.put( new FooComponent("name", 123, null, null), bar );
2357        map.put( new FooComponent("nameName", 12, null, null), bar );
2358        baz.setFooComponentToFoo(map);
2359        map = new HashMap JavaDoc();
2360        map.put(bar, g);
2361        baz.setGlarchToFoo(map);
2362        s.flush();
2363        s.connection().commit();
2364        s.close();
2365
2366        s = openSession();
2367        baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2368        assertTrue( baz.getCascadingBars().size()==1 );
2369
2370        Session s2 = openSession();
2371        assertTrue( ( (Integer JavaDoc) s2.iterate("select count(*) from Bar").next() ).intValue()==3 );
2372        Baz baz2 = (Baz) s2.find("select baz from Baz baz order by baz").get(0);
2373        Object JavaDoc o = baz2.getFooComponentToFoo().get( new FooComponent("name", 123, null, null) );
2374        assertTrue(
2375            o==baz2.getFooComponentToFoo().get( new FooComponent("nameName", 12, null, null) ) && o!=null
2376        );
2377        s2.connection().commit();
2378        s2.close();
2379
2380        assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) );
2381        assertTrue( baz.getTopFoos().size()==2 );
2382        assertTrue( baz.getTopGlarchez().size()==1 );
2383        assertTrue( baz.getTopFoos().iterator().next()!=null );
2384        assertTrue( baz.getStringSet().size()==1 );
2385        assertTrue( baz.getBag().size()==4 );
2386        assertTrue( baz.getFooToGlarch().size()==2 );
2387        assertTrue( baz.getFooComponentToFoo().size()==2 );
2388        assertTrue( baz.getGlarchToFoo().size()==1 );
2389        Iterator JavaDoc iter = baz.getFooToGlarch().keySet().iterator();
2390        for (int i=0; i<2; i++ ) assertTrue( iter.next() instanceof BarProxy );
2391        FooComponent fooComp = (FooComponent) baz.getFooComponentToFoo().keySet().iterator().next();
2392        assertTrue(
2393            ( (fooComp.getCount()==123 && fooComp.getName().equals("name"))
2394            || (fooComp.getCount()==12 && fooComp.getName().equals("nameName")) )
2395            && ( baz.getFooComponentToFoo().get(fooComp) instanceof BarProxy )
2396        );
2397        Glarch g2 = new Glarch();
2398        s.save(g2);
2399        g = (GlarchProxy) baz.getTopGlarchez().get( new Character JavaDoc('G') );
2400        baz.getTopGlarchez().put( new Character JavaDoc('H'), g );
2401        baz.getTopGlarchez().put( new Character JavaDoc('G'), g2 );
2402        s.flush();
2403        s.connection().commit();
2404        s.close();
2405
2406        s = openSession();
2407        baz = (Baz) s.load(Baz.class, baz.getCode());
2408        assertTrue( baz.getTopGlarchez().size()==2 );
2409        assertTrue( baz.getCascadingBars().size()==1 );
2410        s.connection().commit();
2411        s.close();
2412
2413        s = openSession();
2414        assertTrue( ( (Integer JavaDoc) s.iterate("select count(*) from Bar").next() ).intValue()==3 );
2415        baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2416        assertTrue( baz.getTopGlarchez().size()==2 );
2417        assertTrue( baz.getCascadingBars().size()==1 );
2418
2419        s.connection().commit();
2420
2421        s.disconnect();
2422
2423        s2 = (Session) SerializationHelper.deserialize( SerializationHelper.serialize(s) );
2424        s.close();
2425
2426        s2.reconnect();
2427        baz = (Baz) s2.load(Baz.class, baz.getCode());
2428        assertTrue( ( (Integer JavaDoc) s2.iterate("select count(*) from Bar").next() ).intValue()==3 );
2429        s2.delete(baz);
2430        s2.delete( baz.getTopGlarchez().get( new Character JavaDoc('G') ) );
2431        s2.delete( baz.getTopGlarchez().get( new Character JavaDoc('H') ) );
2432        int rows = s2.connection().createStatement().executeUpdate("update " + getDialect().openQuote() + "glarchez" + getDialect().closeQuote() + " set baz_map_id=null where baz_map_index='a'");
2433        assertTrue(rows==1);
2434        assertTrue( s2.delete("from Bar bar")==2 );
2435        FooProxy[] arr = baz.getFooArray();
2436        assertTrue( "new array of objects", arr.length==4 && arr[1].getKey().equals( foo.getKey() ) );
2437        for ( int i=1; i<arr.length; i++ ) {
2438            if ( arr[i]!=null) s2.delete(arr[i]);
2439        }
2440
2441        s2.load( Qux.class, new Long JavaDoc(666) ); //nonexistent
2442

2443        assertTrue( s2.delete("from Glarch g")==1 );
2444
2445        s2.flush();
2446        s2.connection().commit();
2447
2448        s2.disconnect();
2449
2450        Session s3 = (Session) SerializationHelper.deserialize( SerializationHelper.serialize(s2) );
2451        s2.close();
2452        //s3.reconnect();
2453
assertTrue( s3.load( Qux.class, new Long JavaDoc(666) )!=null ); //nonexistent
2454
//s3.disconnect();
2455
s3.close();
2456    }
2457
2458    public void testSaveFlush() throws Exception JavaDoc {
2459        Session s = openSession();
2460        Fee fee = new Fee();
2461        s.save( fee, "key" );
2462        fee.setFi("blah");
2463        s.flush();
2464        s.connection().commit();
2465        s.close();
2466        s = openSession();
2467        fee = (Fee) s.load( Fee.class, fee.getKey() );
2468        assertTrue( "blah".equals( fee.getFi() ) );
2469        assertTrue( "key".equals( fee.getKey() ) );
2470        s.delete(fee);
2471        s.flush();
2472        s.connection().commit();
2473        s.close();
2474
2475    }
2476
2477    public void testCreateUpdate() throws Exception JavaDoc {
2478        Session s = openSession();
2479        Foo foo = new Foo();
2480        s.save(foo);
2481        foo.setString("dirty");
2482        s.flush();
2483        s.connection().commit();
2484        s.close();
2485        s = openSession();
2486        Foo foo2 = new Foo();
2487        s.load( foo2, foo.getKey() );
2488        // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2489
assertTrue( "create-update", foo.equalsFoo(foo2) );
2490        //System.out.println( s.print(foo2) );
2491
s.delete(foo2);
2492        s.flush();
2493        s.connection().commit();
2494        s.close();
2495
2496        s = openSession();
2497        foo = new Foo();
2498        s.save(foo, "assignedid");
2499        foo.setString("dirty");
2500        s.flush();
2501        s.connection().commit();
2502        s.close();
2503        s = openSession();
2504        s.load(foo2, "assignedid");
2505        // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2506
assertTrue( "create-update", foo.equalsFoo(foo2) );
2507        //System.out.println( s.print(foo2) );
2508
s.delete(foo2);
2509        s.flush();
2510        s.connection().commit();
2511        s.close();
2512    }
2513
2514    public void testUpdateCollections() throws Exception JavaDoc {
2515        Session s = openSession();
2516        Holder baz = new Holder();
2517        baz.setName("123");
2518        Foo f1 = new Foo();
2519        Foo f2 = new Foo();
2520        Foo f3 = new Foo();
2521        One o = new One();
2522        baz.setOnes( new ArrayList JavaDoc() );
2523        baz.getOnes().add(o);
2524        Foo[] foos = new Foo[] { f1, null, f2 };
2525        baz.setFooArray(foos);
2526        baz.setFoos( new HashSet JavaDoc() );
2527        baz.getFoos().add(f1);
2528        s.save(f1);
2529        s.save(f2);
2530        s.save(f3);
2531        s.save(o);
2532        s.save(baz);
2533        s.flush();
2534        s.connection().commit();
2535        s.close();
2536
2537        baz.getOnes().set(0, null);
2538        baz.getOnes().add(o);
2539        baz.getFoos().add(f2);
2540        foos[0] = f3;
2541        foos[1] = f1;
2542
2543        s = openSession();
2544        s.saveOrUpdate(baz);
2545        s.flush();
2546        s.connection().commit();
2547        s.close();
2548
2549        s = openSession();
2550        Holder h = (Holder) s.load(Holder.class, baz.getId());
2551        assertTrue( h.getOnes().get(0)==null );
2552        assertTrue( h.getOnes().get(1)!=null );
2553        assertTrue( h.getFooArray()[0]!=null);
2554        assertTrue( h.getFooArray()[1]!=null);
2555        assertTrue( h.getFooArray()[2]!=null);
2556        assertTrue( h.getFoos().size()==2 );
2557        s.connection().commit();
2558        s.close();
2559
2560        baz.getFoos().remove(f1);
2561        baz.getFoos().remove(f2);
2562        baz.getFooArray()[0]=null;
2563        baz.getFooArray()[0]=null;
2564        baz.getFooArray()[0]=null;
2565        s = openSession();
2566        s.saveOrUpdate(baz);
2567        s.delete("from Foo");
2568        baz.getOnes().remove(o);
2569        s.delete("from One");
2570        s.delete(baz);
2571        s.flush();
2572        s.connection().commit();
2573        s.close();
2574
2575    }
2576
2577    public void testCreate() throws Exception JavaDoc {
2578        Session s = openSession();
2579        Foo foo = new Foo();
2580        s.save(foo);
2581        s.flush();
2582        s.connection().commit();
2583        s.close();
2584        s = openSession();
2585        Foo foo2 = new Foo();
2586        s.load( foo2, foo.getKey() );
2587        // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2588
assertTrue( "create", foo.equalsFoo(foo2) );
2589        s.delete(foo2);
2590        s.flush();
2591        s.connection().commit();
2592        s.close();
2593    }
2594
2595    public void testCallback() throws Exception JavaDoc {
2596        Session s = openSession();
2597        Qux q = new Qux("0");
2598        s.save(q);
2599        q.setChild( new Qux("1") );
2600        s.save( q.getChild() );
2601        Qux q2 = new Qux("2");
2602        q2.setChild( q.getChild() );
2603        Qux q3 = new Qux("3");
2604        q.getChild().setChild(q3);
2605        s.save(q3);
2606        Qux q4 = new Qux("4");
2607        q4.setChild(q3);
2608        s.save(q4);
2609        s.save(q2);
2610        s.flush();
2611        s.connection().commit();
2612        s.close();
2613        s = openSession();
2614        List JavaDoc l = s.find("from Qux");
2615        assertTrue( "", l.size()==5);
2616        s.delete( l.get(0) );
2617        s.delete( l.get(1) );
2618        s.delete( l.get(2) );
2619        s.delete( l.get(3) );
2620        s.delete( l.get(4) );
2621        s.flush();
2622        s.connection().commit();
2623        s.close();
2624    }
2625
2626    public void testPolymorphism() throws Exception JavaDoc {
2627        Session s = openSession();
2628        Bar bar = new Bar();
2629        s.save(bar);
2630        bar.setBarString("bar bar");
2631        s.flush();
2632        s.connection().commit();
2633        s.close();
2634        s = openSession();
2635        FooProxy foo = (FooProxy) s.load( Foo.class, bar.getKey() );
2636        assertTrue( "polymorphic", foo instanceof BarProxy );
2637        assertTrue( "subclass property", ( (BarProxy) foo ).getBarString().equals( bar.getBarString() ) );
2638        //System.out.println( s.print(foo) );
2639
s.delete(foo);
2640        s.flush();
2641        s.connection().commit();
2642        s.close();
2643    }
2644
2645    public void testRemoveContains() throws Exception JavaDoc {
2646        Session s = openSession();
2647        Baz baz = new Baz();
2648        baz.setDefaults();
2649        s.save(baz);
2650        s.flush();
2651        assertTrue( s.contains(baz) );
2652        s.evict(baz);
2653        assertFalse( s.contains(baz) );
2654        Baz baz2 = (Baz) s.load( Baz.class, baz.getCode() );
2655        assertFalse(baz==baz2);
2656        s.delete(baz2);
2657        s.flush();
2658        s.connection().commit();
2659        s.close();
2660    }
2661
2662    public void testCollectionOfSelf() throws Exception JavaDoc {
2663
2664        if (getDialect() instanceof HSQLDialect) return; //Why??
2665

2666        Session s = openSession();
2667        Bar bar = new Bar();
2668        s.save(bar);
2669        bar.setAbstracts( new HashSet JavaDoc() );
2670        bar.getAbstracts().add(bar);
2671        Bar bar2 = new Bar();
2672        bar.getAbstracts().add(bar2);
2673        bar.setFoo(bar);
2674        s.save(bar2);
2675        s.flush();
2676        s.connection().commit();
2677        s.close();
2678        bar.setAbstracts(null);
2679        s = openSession();
2680        s.load( bar, bar.getKey() );
2681        assertTrue( "collection contains self", bar.getAbstracts().size()==2 && bar.getAbstracts().contains(bar) );
2682        assertTrue( "association to self", bar.getFoo()==bar );
2683        Iterator JavaDoc iter = bar.getAbstracts().iterator();
2684        while ( iter.hasNext() ) {
2685            s.delete( iter.next() );
2686        }
2687        s.flush();
2688        s.connection().commit();
2689        s.close();
2690    }
2691
2692    public void testFind() throws Exception JavaDoc {
2693        Session s = openSession();
2694
2695        Bar bar = new Bar();
2696        s.save(bar);
2697        bar.setBarString("bar bar");
2698        bar.setString("xxx");
2699        Foo foo = new Foo();
2700        s.save(foo);
2701        foo.setString("foo bar");
2702        s.save( new Foo() );
2703        s.save( new Bar() );
2704        List JavaDoc list1 = s.find("select foo from Foo foo where foo.string='foo bar'");
2705        assertTrue( "find size", list1.size()==1 );
2706        assertTrue( "find ==", list1.get(0)==foo );
2707        List JavaDoc list2 = s.find("from Foo foo order by foo.string, foo.date");
2708        assertTrue( "find size", list2.size()==4 );
2709
2710        list1 = s.find("from Foo foo where foo.class='B'");
2711        assertTrue( "class special property", list1.size()==2);
2712        list1 = s.find("from Foo foo where foo.class=Bar");
2713        assertTrue( "class special property", list1.size()==2);
2714        list1 = s.find("from Foo foo where foo.class=Bar");
2715        list2 = s.find("select bar from Bar bar, Foo foo where bar.string = foo.string and not bar=foo");
2716        assertTrue( "class special property", list1.size()==2);
2717        assertTrue( "select from a subclass", list2.size()==1);
2718        Trivial t = new Trivial();
2719        s.save(t);
2720        s.flush();
2721        s.connection().commit();
2722        s.close();
2723
2724        s = openSession();
2725        list1 = s.find("from Foo foo where foo.string='foo bar'");
2726        assertTrue( "find size", list1.size()==1 );
2727        // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2728
assertTrue( "find equals", ( (Foo) list1.get(0) ).equalsFoo(foo) );
2729        list2 = s.find("select foo from Foo foo");
2730        assertTrue( "find size", list2.size()==5 );
2731        List JavaDoc list3 = s.find("from Bar bar where bar.barString='bar bar'");
2732        assertTrue( "find size", list3.size()==1 );
2733        assertTrue( "find same instance", list2.contains( list1.get(0) ) && list2.contains( list2.get(0) ) );
2734        assertTrue( s.find("from Trivial").size()==1 );
2735        s.delete("from Trivial");
2736
2737        list2 = s.find("from Foo foo where foo.date = ?", new java.sql.Date JavaDoc(123), Hibernate.DATE);
2738        assertTrue ( "find by date", list2.size()==4 );
2739        Iterator JavaDoc iter = list2.iterator();
2740        while ( iter.hasNext() ) {
2741            s.delete( iter.next() );
2742        }
2743        list2 = s.find("from Foo foo");
2744        assertTrue( "find deleted", list2.size()==0);
2745        s.flush();
2746        s.connection().commit();
2747        s.close();
2748    }
2749
2750    public void testDeleteRecursive() throws Exception JavaDoc {
2751        Session s = openSession();
2752        Foo x = new Foo();
2753        Foo y = new Foo();
2754        x.setFoo(y);
2755        y.setFoo(x);
2756        s.save(x);
2757        s.save(y);
2758        s.flush();
2759        s.delete(y);
2760        s.delete(x);
2761        s.flush();
2762        s.connection().commit();
2763        s.close();
2764    }
2765
2766    /*public void testSubcollections() throws Exception {
2767        Session s = sessionsopenSession();
2768        Baz baz = new Baz();
2769        s.save(baz);
2770        baz.setDefaults();
2771        s.flush();
2772        s.connection().commit();
2773        s.close();
2774        s = sessionsopenSession();
2775        baz = (Baz) s.load( Baz.class, baz.getCode() );
2776        Set[] setArray = baz.getSetArray();
2777        baz.setSetArray(null);
2778        baz.setAnotherSetArray(setArray);
2779        baz.setAnotherSetList( baz.getSetList() );
2780        baz.setSetList(null);
2781        s.flush();
2782        s.connection().commit();
2783        s.close();
2784        s = sessionsopenSession();
2785        baz = (Baz) s.load( Baz.class, baz.getCode() );
2786        assertTrue( baz.getAnotherSetArray().length==2 && baz.getAnotherSetArray()[0]!=null, "subcollection moved property");
2787        assertTrue( baz.getSetArray()==null, "subcollection moved property");
2788        assertTrue( baz.getAnotherSetList().size()==4 && baz.getAnotherSetList().get(2)!=null, "subcollection moved role");
2789        assertTrue( baz.getSetList()==null, "subcollection moved role");
2790        s.delete(baz);
2791        s.flush();
2792        s.connection().commit();
2793        s.close();
2794    }*/

2795
2796
2797    public void testReachability() throws Exception JavaDoc {
2798        //first for unkeyed collections
2799
Session s = openSession();
2800        Baz baz1 = new Baz();
2801        s.save(baz1);
2802        Baz baz2 = new Baz();
2803        s.save(baz2);
2804        baz1.setIntArray( new int[] {1 ,2, 3, 4} );
2805        baz1.setFooSet( new HashSet JavaDoc() );
2806        Foo foo = new Foo();
2807        s.save(foo);
2808        baz1.getFooSet().add(foo);
2809        s.flush();
2810        s.connection().commit();
2811        s.close();
2812
2813        s = openSession();
2814        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2815        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2816        baz2.setFooSet( baz1.getFooSet() ); baz1.setFooSet(null);
2817        baz2.setIntArray( baz1.getIntArray() ); baz1.setIntArray(null);
2818        s.flush();
2819        s.connection().commit();
2820        s.close();
2821
2822        s = openSession();
2823        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2824        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2825        assertTrue( "unkeyed reachability", baz2.getIntArray().length==4 );
2826        assertTrue( "unkeyed reachability", baz2.getFooSet().size()==1 );
2827        assertTrue( "unkeyed reachability", baz1.getIntArray().length==0 );
2828        assertTrue( "unkeyed reachability", baz1.getFooSet().size()==0 );
2829        //System.out.println( s.print(baz1) + s.print(baz2) );
2830
FooProxy fp = (FooProxy) baz2.getFooSet().iterator().next();
2831        s.delete(fp);
2832        s.delete(baz1);
2833        s.delete(baz2);
2834        s.flush();
2835        s.connection().commit();
2836        s.close();
2837
2838        //now for collections of collections
2839
s = openSession();
2840        baz1 = new Baz();
2841        s.save(baz1);
2842        baz2 = new Baz();
2843        s.save(baz2);
2844        s.flush();
2845        s.connection().commit();
2846        s.close();
2847        s = openSession();
2848        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2849        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2850        s.flush();
2851        s.connection().commit();
2852        s.close();
2853        s = openSession();
2854        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2855        s.flush();
2856        s.connection().commit();
2857        s.close();
2858        s = openSession();
2859        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2860        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2861        //System.out.println( s.print(baz1) + s.print(baz2) );
2862
//System.out.println( s.print(baz1) + s.print(baz2) );
2863
s.delete(baz1);
2864        s.delete(baz2);
2865        s.flush();
2866        s.connection().commit();
2867        s.close();
2868
2869        //now for keyed collections
2870
s = openSession();
2871        baz1 = new Baz();
2872        s.save(baz1);
2873        baz2 = new Baz();
2874        s.save(baz2);
2875        Foo foo1 = new Foo();
2876        Foo foo2 = new Foo();
2877        s.save(foo1); s.save(foo2);
2878        baz1.setFooArray( new Foo[] { foo1, null, foo2 } );
2879        baz1.setStringDateMap( new TreeMap JavaDoc() );
2880        baz1.getStringDateMap().put("today", new Date JavaDoc( System.currentTimeMillis() ) );
2881        baz1.getStringDateMap().put("tomorrow", new Date JavaDoc( System.currentTimeMillis() + 86400000 ) );
2882        s.flush();
2883        s.connection().commit();
2884        s.close();
2885        s = openSession();
2886        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2887        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2888        baz2.setFooArray( baz1.getFooArray() ); baz1.setFooArray(null);
2889        baz2.setStringDateMap( baz1.getStringDateMap() ); baz1.setStringDateMap(null);
2890        s.flush();
2891        s.connection().commit();
2892        s.close();
2893        s = openSession();
2894        baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2895        baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2896        assertTrue( "reachability", baz2.getStringDateMap().size()==2 );
2897        assertTrue( "reachability", baz2.getFooArray().length==3 );
2898        assertTrue( "reachability", baz1.getStringDateMap().size()==0 );
2899        assertTrue( "reachability", baz1.getFooArray().length==0 );
2900        //System.out.println( s.print(baz1) + s.print(baz2) );
2901
assertTrue( "null element", baz2.getFooArray()[1]==null );
2902        assertTrue( "non-null element", baz2.getStringDateMap().get("today")!=null );
2903        assertTrue( "non-null element", baz2.getStringDateMap().get("tomorrow")!=null );
2904        assertTrue( "null element", baz2.getStringDateMap().get("foo")==null );
2905        s.delete( baz2.getFooArray()[0] );
2906        s.delete( baz2.getFooArray()[2] );
2907        s.delete(baz1);
2908        s.delete(baz2);
2909        s.flush();
2910        assertTrue( s.find("from java.lang.Object").size()==0 );
2911        s.connection().commit();
2912        s.close();
2913    }
2914
2915    public void testPersistentLifecycle() throws Exception JavaDoc {
2916        Session s = openSession();
2917        Qux q = new Qux();
2918        s.save(q);
2919        q.setStuff("foo bar baz qux");
2920        s.flush();
2921        s.connection().commit();
2922        s.close();
2923        s = openSession();
2924        q = (Qux) s.load( Qux.class, q.getKey() );
2925        assertTrue( "lifecycle create", q.getCreated() );
2926        assertTrue( "lifecycle load", q.getLoaded() );
2927        assertTrue( "lifecycle subobject", q.getFoo()!=null );
2928        s.delete(q);
2929        assertTrue( "lifecycle delete", q.getDeleted() );
2930        s.flush();
2931        s.connection().commit();
2932        s.close();
2933        s = openSession();
2934        assertTrue( "subdeletion", s.find("from Foo foo").size()==0);
2935        s.flush();
2936        s.connection().commit();
2937        s.close();
2938    }
2939
2940    public void testIterators() throws Exception JavaDoc {
2941        Session s = openSession();
2942        for ( int i=0; i<10; i++ ) {
2943            Qux q = new Qux();
2944            Object JavaDoc qid = s.save(q);
2945            assertTrue("not null", qid!=null);
2946        }
2947        s.flush();
2948        s.connection().commit();
2949        s.close();
2950
2951        s = openSession();
2952        Iterator JavaDoc iter = s.iterate("from Qux q where q.stuff is null");
2953        int count=0;
2954        while ( iter.hasNext() ) {
2955            Qux q = (Qux) iter.next();
2956            q.setStuff("foo");
2957            if (count==0 || count==5) iter.remove();
2958            count++;
2959        }
2960        assertTrue("iterate", count==10);
2961        s.flush();
2962        s.connection().commit();
2963        s.close();
2964
2965        s = openSession();
2966        assertTrue(
2967            "delete by query",
2968            s.delete("from Qux q where q.stuff=?", "foo", Hibernate.STRING)==8
2969        );
2970        s.flush();
2971        s.connection().commit();
2972        s.close();
2973
2974        s = openSession();
2975        iter = s.iterate("from Qux q");
2976        assertTrue( "empty iterator", !iter.hasNext() );
2977        s.flush();
2978        s.connection().commit();
2979        s.close();
2980    }
2981
2982    public void testVersioning() throws Exception JavaDoc {
2983        Session s = openSession();
2984        GlarchProxy g = new Glarch();
2985        s.save(g);
2986        GlarchProxy g2 = new Glarch();
2987        s.save(g2);
2988        Serializable JavaDoc gid = s.getIdentifier(g);
2989        Serializable JavaDoc g2id = s.getIdentifier(g2);
2990        g.setName("glarch");
2991        s.flush();
2992        s.connection().commit();
2993        s.close();
2994
2995        getSessions().evict(Glarch.class);
2996
2997        s = openSession();
2998        g = (GlarchProxy) s.load( Glarch.class, gid );
2999        s.lock(g, LockMode.UPGRADE);
3000        g2 = (GlarchProxy) s.load( Glarch.class, g2id );
3001        assertTrue( "version", g.getVersion()==1 );
3002        assertTrue( "version", g.getDerivedVersion()==1 );
3003        assertTrue( "version", g2.getVersion()==0 );
3004        g.setName("foo");
3005        assertTrue(
3006            "find by version",
3007            s.find("from Glarch g where g.version=2").size()==1
3008        );
3009        g.setName("bar");
3010        s.flush();
3011        s.connection().commit();
3012        s.close();
3013
3014        getSessions().evict(Glarch.class);
3015
3016        s = openSession();
3017        g = (GlarchProxy) s.load( Glarch.class, gid );
3018        g2 = (GlarchProxy) s.load( Glarch.class, g2id );
3019        assertTrue( "version", g.getVersion()==3 );
3020        assertTrue( "version", g.getDerivedVersion()==3 );
3021        assertTrue( "version", g2.getVersion()==0 );
3022        g.setNext(null);
3023        g2.setNext(g);
3024        s.delete(g2);
3025        s.delete(g);
3026        s.flush();
3027        s.connection().commit();
3028        s.close();
3029    }
3030
3031    public void testVersionedCollections() throws Exception JavaDoc {
3032        Session s = openSession();
3033        GlarchProxy g = new Glarch();
3034        s.save(g);
3035        g.setProxyArray( new GlarchProxy[] { g } );
3036        String JavaDoc gid = (String JavaDoc) s.getIdentifier(g);
3037        ArrayList JavaDoc list = new ArrayList JavaDoc();
3038        list.add("foo");
3039        g.setStrings(list);
3040        HashSet JavaDoc set = new HashSet JavaDoc();
3041        set.add(g);
3042        g.setProxySet(set);
3043        s.flush();
3044        s.connection().commit();
3045        s.close();
3046
3047        s = openSession();
3048        g = (GlarchProxy) s.load(Glarch.class, gid);
3049        assertTrue( g.getStrings().size()==1 );
3050        assertTrue( g.getProxyArray().length==1 );
3051        assertTrue( g.getProxySet().size()==1 );
3052        assertTrue( "versioned collection before", g.getVersion()==1 );
3053        s.flush();
3054        s.connection().commit();
3055        s.close();
3056
3057        s = openSession();
3058        g = (GlarchProxy) s.load(Glarch.class, gid);
3059        assertTrue( g.getStrings().get(0).equals("foo") );
3060        assertTrue( g.getProxyArray()[0]==g );
3061        assertTrue( g.getProxySet().iterator().next()==g );
3062        assertTrue( "versioned collection before", g.getVersion()==1 );
3063        s.flush();
3064        s.connection().commit();
3065        s.close();
3066
3067        s = openSession();
3068        g = (GlarchProxy) s.load(Glarch.class, gid);
3069        assertTrue( "versioned collection before", g.getVersion()==1 );
3070        g.getStrings().add("bar");
3071        s.flush();
3072        s.connection().commit();
3073        s.close();
3074
3075        s = openSession();
3076        g = (GlarchProxy) s.load(Glarch.class, gid);
3077        assertTrue( "versioned collection after", g.getVersion()==2 );
3078        assertTrue( "versioned collection after", g.getStrings().size()==2 );
3079        g.setProxyArray(null);
3080        s.flush();
3081        s.connection().commit();
3082        s.close();
3083
3084        s = openSession();
3085        g = (GlarchProxy) s.load(Glarch.class, gid);
3086        assertTrue( "versioned collection after", g.getVersion()==3 );
3087        assertTrue( "versioned collection after", g.getProxyArray().length==0 );
3088        g.setFooComponents( new ArrayList JavaDoc() );
3089        g.setProxyArray(null);
3090        s.flush();
3091        s.connection().commit();
3092        s.close();
3093
3094        s = openSession();
3095        g = (GlarchProxy) s.load(Glarch.class, gid);
3096        assertTrue( "versioned collection after", g.getVersion()==4 );
3097        s.delete(g);
3098        s.flush();
3099        assertTrue( s.find("from java.lang.Object").size()==0 );
3100        s.connection().commit();
3101        s.close();
3102    }
3103
3104    /*public void testVersionedSubcollections() throws Exception {
3105        Session s = sessionsopenSession();
3106
3107        assertTrue( !s.iterate("from Fee fee").hasNext() );
3108
3109        GlarchProxy g = new Glarch();
3110        s.save(g);
3111        String gid = (String) s.getIdentifier(g);
3112        HashMap map = new HashMap();
3113        HashSet subSet = new HashSet();
3114        map.put("xxx", subSet);
3115        subSet.add("foo"); subSet.add("bar");
3116        g.setStringSets(map);
3117        s.flush();
3118        s.connection().commit();
3119        s.close();
3120        s = sessionsopenSession();
3121        g = (GlarchProxy) s.load(Glarch.class, gid);
3122        assertTrue( g.getVersion()==1, "versioned collection before" );
3123        //System.out.println( g.getStringSets().get("xxx") );
3124        assertTrue( ( (Set) g.getStringSets().get("xxx") ).size()==2, "versioned collection before" );
3125        ( (Set) g.getStringSets().get("xxx") ).add("baz");
3126        s.flush();
3127        s.connection().commit();
3128        s.close();
3129        s = sessionsopenSession();
3130        g = (GlarchProxy) s.load(Glarch.class, gid);
3131        assertTrue( g.getVersion()==2, "versioned collection after" );
3132        //System.out.println( g.getStringSets().get("xxx") );
3133        assertTrue( ( (Set) g.getStringSets().get("xxx") ).size()==3, "versioned collection after" );
3134        s.delete(g);
3135        s.flush();
3136        s.connection().commit();
3137        s.close();
3138
3139        //with components! (note: this also tests some stuff testPersistCollections misses)
3140        s = sessionsopenSession();
3141        g = new Glarch(); //(GlarchProxy) s.create(Glarch.class);
3142        List list = new ArrayList();
3143        Date[] dates = new Date[] { null, null, new Date(), new Date(0) };
3144        list.add(null);
3145        list.add( new FooComponent("foo", 69, dates, new FooComponent("bar", 96, null, null, new Fee() ) ) );
3146        g.setFooComponents(list);
3147        String EIGHT_CHARS = "abcdefgh";
3148        s.save(g, EIGHT_CHARS + EIGHT_CHARS + EIGHT_CHARS + EIGHT_CHARS);
3149        gid = (String) s.getIdentifier(g);
3150        assertTrue( s.iterate("from Fee fee").hasNext() );
3151        g.getFooComponents().add( new FooComponent("bar", 96, null, null) );
3152        s.flush();
3153        s.connection().commit();
3154        s.close();
3155        s = sessionsopenSession();
3156        g = (GlarchProxy) s.load(Glarch.class, gid);
3157        assertTrue( g.getVersion()==2, "versioned collection before" );
3158        ( (FooComponent) g.getFooComponents().get(1) ).getImportantDates()[0] = new Date(123567890);
3159        s.flush();
3160        s.connection().commit();
3161        s.close();
3162        s = sessionsopenSession();
3163        g = (GlarchProxy) s.load(Glarch.class, gid);
3164        assertTrue( g.getVersion()==3, "versioned collection after" );
3165        ( (FooComponent) g.getFooComponents().get(1) ).getSubcomponent().setName("new name");
3166        assertTrue( ( (FooComponent) g.getFooComponents().get(1) ).getImportantDates()[0]!=null, "versioned collection after" );
3167        s.flush();
3168        s.connection().commit();
3169        s.close();
3170        s = sessionsopenSession();
3171        g = (GlarchProxy) s.load(Glarch.class, gid);
3172        assertTrue( ( (FooComponent) g.getFooComponents().get(1) ).getSubcomponent().getName().equals("new name"), "versioned collection after" );
3173        assertTrue( g.getVersion()==4, "versioned collection after" );
3174        s.delete(g);
3175        assertTrue( !s.iterate("from Fee fee").hasNext() );
3176        s.flush();
3177        s.connection().commit();
3178        s.close();
3179
3180
3181    }*/

3182
3183    public void testRecursiveLoad() throws Exception JavaDoc {
3184        //Non polymorphic class (there is an implementation optimization
3185
//being tested here)
3186
Session s = openSession();
3187        GlarchProxy last = new Glarch();
3188        s.save(last);
3189        last.setOrder( (short) 0 );
3190        for (int i=0; i<5; i++) {
3191            GlarchProxy next = new Glarch();
3192            s.save(next);
3193            last.setNext(next);
3194            last = next;
3195            last.setOrder( (short) (i+1) );
3196        }
3197        Iterator JavaDoc iter = s.iterate("from Glarch g");
3198        while ( iter.hasNext() ) {
3199            iter.next();
3200        }
3201        List JavaDoc list = s.find("from Glarch g");
3202        assertTrue( "recursive find", list.size()==6 );
3203        s.flush();
3204        s.connection().commit();
3205        s.close();
3206
3207        s = openSession();
3208        list = s.find("from Glarch g");
3209        assertTrue( "recursive iter", list.size()==6 );
3210        list = s.find("from Glarch g where g.next is not null");
3211        assertTrue( "recursive iter", list.size()==5 );
3212        s.flush();
3213        s.connection().commit();
3214        s.close();
3215
3216        s = openSession();
3217        iter = s.iterate("from Glarch g order by g.order asc");
3218        while ( iter.hasNext() ) {
3219            GlarchProxy g = (GlarchProxy) iter.next();
3220            assertTrue( "not null", g!=null );
3221            iter.remove();
3222        }
3223        s.flush();
3224        s.connection().commit();
3225        s.close();
3226
3227        //Same thing but using polymorphic class (no optimisation possible):
3228
s = openSession();
3229        FooProxy flast = new Bar();
3230        s.save(flast);
3231        flast.setString( "foo0" );
3232        for (int i=0; i<5; i++) {
3233            FooProxy foo = new Bar();
3234            s.save(foo);
3235            flast.setFoo(foo);
3236            flast = flast.getFoo();
3237            flast.setString( "foo" + (i+1) );
3238        }
3239        iter = s.iterate("from Foo foo");
3240        while ( iter.hasNext() ) {
3241            iter.next();
3242        }
3243        list = s.find("from Foo foo");
3244        assertTrue( "recursive find", list.size()==6 );
3245        s.flush();
3246        s.connection().commit();
3247        s.close();
3248
3249        s = openSession();
3250        list = s.find("from Foo foo");
3251        assertTrue( "recursive iter", list.size()==6 );
3252        iter = list.iterator();
3253        while ( iter.hasNext() ) {
3254            assertTrue( "polymorphic recursive load", iter.next() instanceof BarProxy );
3255        }
3256        s.flush();
3257        s.connection().commit();
3258        s.close();
3259
3260        s = openSession();
3261        iter = s.iterate("from Foo foo order by foo.string asc");
3262        while ( iter.hasNext() ) {
3263            BarProxy bar = (BarProxy) iter.next();
3264            assertTrue( "not null", bar!=null );
3265            iter.remove();
3266        }
3267        s.flush();
3268        s.connection().commit();
3269        s.close();
3270    }
3271
3272    public void testScrollableIterator() throws Exception JavaDoc {
3273        if ( getDialect() instanceof DB2Dialect || getDialect() instanceof OracleDialect || getDialect() instanceof SybaseDialect || getDialect() instanceof HSQLDialect ) {
3274            Session s = openSession();
3275            s.save( new Foo() );
3276            s.save( new Foo() );
3277            s.save( new Foo() );
3278            s.save( new Bar() );
3279            Query query = s.createQuery("select f, f.integer from Foo f");
3280            assertTrue( query.getReturnTypes().length==2 );
3281            ScrollableResults iter = query.scroll();
3282            assertTrue( iter.next() );
3283            assertTrue( iter.scroll(1) );
3284            FooProxy f2 = (FooProxy) iter.get()[0];
3285            assertTrue( f2!=null );
3286            assertTrue( iter.scroll(-1) );
3287            Object JavaDoc f1 = iter.get(0);
3288            iter.next();
3289            assertTrue( f1!=null && iter.get(0)==f2 );
3290            iter.getInteger(1);
3291
3292            assertTrue( !iter.scroll(100) );
3293            assertTrue( iter.first() );
3294            assertTrue( iter.scroll(3) );
3295            Object JavaDoc f4 = iter.get(0);
3296            assertTrue( f4!=null );
3297            assertTrue( !iter.next() );
3298            assertTrue( iter.first() );
3299            assertTrue( iter.get(0)==f1 );
3300            assertTrue( iter.last() );
3301            assertTrue( iter.get(0)==f4 );
3302            assertTrue( iter.previous() );
3303            s.connection().commit();
3304            s.close();
3305
3306            s = openSession();
3307            query = s.createQuery("select f, f.integer from Foo f");
3308            assertTrue( query.getReturnTypes().length==2 );
3309            iter = query.scroll();
3310            assertTrue( iter.next() );
3311            assertTrue( iter.scroll(1) );
3312            f2 = (FooProxy) iter.get()[0];
3313            assertTrue( f2!=null );
3314            assertTrue( f2.getString()!=null && f2.getComponent().getImportantDates().length > 0 );
3315            assertTrue( iter.scroll(-1) );
3316            f1 = iter.get(0);
3317            iter.next();
3318            assertTrue( f1!=null && iter.get(0)==f2 );
3319            iter.getInteger(1);
3320
3321            assertTrue( !iter.scroll(100) );
3322            assertTrue( iter.first() );
3323            assertTrue( iter.scroll(3) );
3324            f4 = iter.get(0);
3325            assertTrue( f4!=null );
3326            assertTrue( !iter.next() );
3327            assertTrue( iter.first() );
3328            assertTrue( iter.get(0)==f1 );
3329            assertTrue( iter.last() );
3330            assertTrue( iter.get(0)==f4 );
3331            assertTrue( iter.previous() );
3332            assertTrue( s.delete("from Foo")==4 );
3333            s.flush();
3334            assertTrue( s.find("from java.lang.Object").size()==0 );
3335            s.connection().commit();
3336            s.close();
3337        }
3338    }
3339
3340    public void testMultiColumnQueries() throws Exception JavaDoc {
3341        Session s = openSession();
3342        Foo foo = new Foo();
3343        s.save(foo);
3344        Foo foo1 = new Foo();
3345        s.save(foo1);
3346        foo.setFoo(foo1);
3347        List JavaDoc l = s.find("select parent, child from Foo parent, Foo child where parent.foo = child");
3348        assertTrue( "multi-column find", l.size()==1 );
3349
3350        Iterator JavaDoc rs = s.iterate("select count(distinct child.id), count(distinct parent.id) from Foo parent, Foo child where parent.foo = child");
3351        Object JavaDoc[] row = (Object JavaDoc[]) rs.next();
3352        assertTrue( "multi-column count", ( (Integer JavaDoc) row[0] ).intValue()==1 );
3353        assertTrue( "multi-column count", ( (Integer JavaDoc) row[1] ).intValue()==1 );
3354        assertTrue( !rs.hasNext() );
3355
3356        rs = s.iterate("select child.id, parent.id, child.long from Foo parent, Foo child where parent.foo = child");
3357        row = (Object JavaDoc[]) rs.next();
3358        assertTrue( "multi-column id", row[0].equals( foo.getFoo().getKey() ) );
3359        assertTrue( "multi-column id", row[1].equals( foo.getKey() ) );
3360        assertTrue( "multi-column property", row[2].equals( foo.getFoo().getLong() ) );
3361        assertTrue( !rs.hasNext() );
3362
3363        rs = s.iterate("select child.id, parent.id, child.long, child, parent.foo from Foo parent, Foo child where parent.foo = child");
3364        row = (Object JavaDoc[]) rs.next();
3365        assertTrue(
3366            foo.getFoo().getKey().equals( row[0] ) &&
3367            foo.getKey().equals( row[1] ) &&
3368            foo.getFoo().getLong().equals( row[2] ) &&
3369            row[3] == foo.getFoo() &&
3370            row[3]==row[4]
3371        );
3372        assertTrue( !rs.hasNext() );
3373
3374        row = (Object JavaDoc[]) l.get(0);
3375        assertTrue( "multi-column find", row[0]==foo && row[1]==foo.getFoo() );
3376        s.flush();
3377        s.connection().commit();
3378        s.close();
3379        
3380        s = openSession();
3381        Iterator JavaDoc iter = s.iterate("select parent, child from Foo parent, Foo child where parent.foo = child and parent.string='a string'");
3382        int deletions=0;
3383        while ( iter.hasNext() ) {
3384            Object JavaDoc[] pnc = (Object JavaDoc[]) iter.next();
3385            s.delete( pnc[0] );
3386            s.delete( pnc[1] );
3387            deletions++;
3388        }
3389        assertTrue("multi-column iterate", deletions==1);
3390        s.flush();
3391        s.connection().commit();
3392        s.close();
3393    }
3394
3395    public void testDeleteTransient() throws Exception JavaDoc {
3396        Fee fee = new Fee();
3397        Fee fee2 = new Fee();
3398        fee2.setAnotherFee(fee);
3399        Session s = openSession();
3400        Transaction tx = s.beginTransaction();
3401        s.save(fee);
3402        s.save(fee2);
3403        s.flush();
3404        fee.setCount(123);
3405        tx.commit();
3406        s.close();
3407        s = openSession();
3408        tx = s.beginTransaction();
3409        s.delete(fee);
3410        s.delete(fee2);
3411        //foo.setAnotherFee(null);
3412
tx.commit();
3413        s.close();
3414        s = openSession();
3415        tx = s.beginTransaction();
3416        assertTrue( s.find("from Fee fee").size()==0 );
3417        tx.commit();
3418        s.close();
3419    }
3420
3421    public void testDeleteUpdatedTransient() throws Exception JavaDoc {
3422        Fee fee = new Fee();
3423        Fee fee2 = new Fee();
3424        fee2.setAnotherFee(fee);
3425        Session s = openSession();
3426        Transaction tx = s.beginTransaction();
3427        s.save(fee);
3428        s.save(fee2);
3429        s.flush();
3430        fee.setCount(123);
3431        tx.commit();
3432        s.close();
3433        s = openSession();
3434        tx = s.beginTransaction();
3435        s.update(fee);
3436        //fee2.setAnotherFee(null);
3437
s.update(fee2);
3438        s.delete(fee);
3439        s.delete(fee2);
3440        tx.commit();
3441        s.close();
3442        s = openSession();
3443        tx = s.beginTransaction();
3444        assertTrue( s.find("from Fee fee").size()==0 );
3445        tx.commit();
3446        s.close();
3447    }
3448
3449    public void testUpdateOrder() throws Exception JavaDoc {
3450        Session s = openSession();
3451        Fee fee1 = new Fee();
3452        s.save(fee1);
3453        Fee fee2 = new Fee();
3454        fee1.setFee(fee2);
3455        fee2.setFee(fee1);
3456        fee2.setFees( new HashSet JavaDoc() );
3457        Fee fee3 = new Fee();
3458        fee3.setFee(fee1);
3459        fee3.setAnotherFee(fee2);
3460        fee2.setAnotherFee(fee3);
3461        s.save(fee3);
3462        s.save(fee2);
3463        s.flush();
3464        s.connection().commit();
3465        s.close();
3466
3467        s = openSession();
3468        fee1.setCount(10);
3469        fee2.setCount(20);
3470        fee3.setCount(30);
3471        s.update(fee1);
3472        s.update(fee2);
3473        s.update(fee3);
3474        s.flush();
3475        s.delete(fee1);
3476        s.delete(fee2);
3477        s.delete(fee3);
3478        s.flush();
3479        s.connection().commit();
3480        s.close();
3481
3482        s = openSession();
3483        Transaction tx = s.beginTransaction();
3484        assertTrue( s.find("from Fee fee").size()==0 );
3485        tx.commit();
3486        s.close();
3487    }
3488
3489    public void testUpdateFromTransient() throws Exception JavaDoc {
3490        Session s = openSession();
3491        Fee fee1 = new Fee();
3492        s.save(fee1);
3493        Fee fee2 = new Fee();
3494        fee1.setFee(fee2);
3495        fee2.setFee(fee1);
3496        fee2.setFees( new HashSet JavaDoc() );
3497        Fee fee3 = new Fee();
3498        fee3.setFee(fee1);
3499        fee3.setAnotherFee(fee2);
3500        fee2.setAnotherFee(fee3);
3501        s.save(fee3);
3502        s.save(fee2);
3503        s.flush();
3504        s.connection().commit();
3505        s.close();
3506
3507        fee1.setFi("changed");
3508        s = openSession();
3509        s.saveOrUpdate(fee1);
3510        s.flush();
3511        s.connection().commit();
3512        s.close();
3513
3514        Qux q = new Qux("quxxy");
3515        q.setTheKey(0);
3516        fee1.setQux(q);
3517        s = openSession();
3518        s.saveOrUpdate(fee1);
3519        s.flush();
3520        s.connection().commit();
3521        s.close();
3522
3523
3524        s = openSession();
3525        fee1 = (Fee) s.load( Fee.class, fee1.getKey() );
3526        assertTrue( "updated from transient", fee1.getFi().equals("changed") );
3527        assertTrue( "unsaved value", fee1.getQux()!=null );
3528        s.delete( fee1.getQux() );
3529        fee1.setQux(null);
3530        s.flush();
3531        s.connection().commit();
3532        s.close();
3533
3534        fee2.setFi("CHANGED");
3535        fee2.getFees().add("an element");
3536        fee1.setFi("changed again");
3537        s = openSession();
3538        s.saveOrUpdate(fee2);
3539        s.update( fee1, fee1.getKey() );
3540        s.flush();
3541        s.connection().commit();
3542        s.close();
3543
3544        s = openSession();
3545        Fee fee = new Fee();
3546        s.load( fee, fee2.getKey() );
3547        fee1 = (Fee) s.load( Fee.class, fee1.getKey() );
3548        assertTrue( "updated from transient", fee1.getFi().equals("changed again") );
3549        assertTrue( "updated from transient", fee.getFi().equals("CHANGED") );
3550        assertTrue( "updated collection", fee.getFees().contains("an element") );
3551        s.flush();
3552        s.connection().commit();
3553        s.close();
3554
3555        fee.getFees().clear();
3556        fee.getFees().add("new element");
3557        fee1.setFee(null);
3558        s = openSession();
3559        s.saveOrUpdate(fee);
3560        s.saveOrUpdate(fee1);
3561        s.flush();
3562        s.connection().commit();
3563        s.close();
3564
3565        s = openSession();
3566        s.load( fee, fee.getKey() );
3567        assertTrue( "update", fee.getAnotherFee()!=null );
3568        assertTrue( "update", fee.getFee()!=null );
3569        assertTrue( "update", fee.getAnotherFee().getFee()==fee.getFee() );
3570        assertTrue( "updated collection", fee.getFees().contains("new element") );
3571        assertTrue( "updated collection", !fee.getFees().contains("an element") );
3572        s.flush();
3573        s.connection().commit();
3574        s.close();
3575
3576        fee.setQux( new Qux("quxy") );
3577        s = openSession();
3578        s.saveOrUpdate(fee);
3579        s.flush();
3580        s.connection().commit();
3581        s.close();
3582
3583        fee.getQux().setStuff("xxx");
3584        s = openSession();
3585        s.saveOrUpdate(fee);
3586        s.flush();
3587        s.connection().commit();
3588        s.close();
3589
3590        s = openSession();
3591        s.load( fee, fee.getKey() );
3592        assertTrue( "cascade update", fee.getQux()!=null );
3593        assertTrue( "cascade update", fee.getQux().getStuff().equals("xxx") );
3594        assertTrue( "update", fee.getAnotherFee()!=null );
3595        assertTrue( "update", fee.getFee()!=null );
3596        assertTrue( "update", fee.getAnotherFee().getFee()==fee.getFee() );
3597        fee.getAnotherFee().setAnotherFee(null);
3598        s.delete(fee);
3599        s.delete("from Fee fee");
3600        s.flush();
3601        s.connection().commit();
3602        s.close();
3603
3604        s = openSession();
3605        Transaction tx = s.beginTransaction();
3606        assertTrue( s.find("from Fee fee").size()==0 );
3607        tx.commit();
3608        s.close();
3609    }
3610
3611    public void testArraysOfTimes() throws Exception JavaDoc {
3612        Session s = openSession();
3613        Baz baz = new Baz() ;
3614        s.save(baz);
3615        baz.setDefaults();
3616        s.flush();
3617        s.connection().commit();
3618        s.close();
3619        s = openSession();
3620
3621        baz.getTimeArray()[2] = new Date JavaDoc(123);
3622        baz.getTimeArray()[3] = new java.sql.Time JavaDoc(1234);
3623        s.flush();
3624        s.connection().commit();
3625        s.close();
3626        s = openSession();
3627        baz = (Baz) s.load( Baz.class, baz.getCode() );
3628        s.delete(baz);
3629        s.flush();
3630        s.connection().commit();
3631        s.close();
3632    }
3633
3634    public void testComponents() throws Exception JavaDoc {
3635        Session s = openSession();
3636        Foo foo = new Foo();
3637        foo.setComponent( new FooComponent("foo", 69, null, new FooComponent("bar", 96, null, null) ) );
3638        s.save(foo);
3639        foo.getComponent().setName("IFA");
3640        s.flush();
3641        s.connection().commit();
3642        s.close();
3643        foo.setComponent(null);
3644        s = openSession();
3645        s.load( foo, foo.getKey() );
3646        assertTrue(
3647            "save components",
3648            foo.getComponent().getName().equals("IFA") &&
3649            foo.getComponent().getSubcomponent().getName().equals("bar")
3650        );
3651        assertTrue( "cascade save via component", foo.getComponent().getGlarch()!=null);
3652        foo.getComponent().getSubcomponent().setName("baz");
3653        s.flush();
3654        s.connection().commit();
3655        s.close();
3656        foo.setComponent(null);
3657        s = openSession();
3658        s.load( foo, foo.getKey() );
3659        assertTrue(
3660            "update components",
3661            foo.getComponent().getName().equals("IFA") &&
3662            foo.getComponent().getSubcomponent().getName().equals("baz")
3663        );
3664        s.delete(foo);
3665        s.flush();
3666        s.connection().commit();
3667        s.close();
3668
3669        s = openSession();
3670        foo = new Foo();
3671        s.save(foo);
3672        foo.setCustom( new String JavaDoc[] { "one", "two" } );
3673        assertTrue( s.find("from Foo foo where foo.custom.s1 = 'one'").get(0)==foo );
3674        s.delete(foo);
3675        s.flush();
3676        s.connection().commit();
3677        s.close();
3678
3679    }
3680
3681    public void testNoForeignKeyViolations() throws Exception JavaDoc {
3682        Session s = openSession();
3683        Glarch g1 = new Glarch();
3684        Glarch g2 = new Glarch();
3685        g1.setNext(g2);
3686        g2.setNext(g1);
3687        s.save(g1);
3688        s.save(g2);
3689        s.flush();
3690        s.connection().commit();
3691        s.close();
3692        s = openSession();
3693        List JavaDoc l = s.find("from Glarch g where g.next is not null");
3694        s.delete( l.get(0) );
3695        s.delete( l.get(1) );
3696        s.flush();
3697        s.connection().commit();
3698        s.close();
3699    }
3700
3701    public void testLazyCollections() throws Exception JavaDoc {
3702        Session s = openSession();
3703        Qux q = new Qux();
3704        s.save(q);
3705        s.flush();
3706        s.connection().commit();
3707        s.close();
3708
3709        s = openSession();
3710        q = (Qux) s.load( Qux.class, q.getKey() );
3711        s.flush();
3712        s.connection().commit();
3713        s.close();
3714
3715        System.out.println("Two exceptions are supposed to occur:");
3716        boolean ok = false;
3717        try {
3718            q.getMoreFums().isEmpty();
3719        }
3720        catch (LazyInitializationException e) {
3721            ok = true;
3722        }
3723        assertTrue( "lazy collection with one-to-many", ok );
3724
3725        ok = false;
3726        try {
3727            q.getFums().isEmpty();
3728        }
3729        catch (LazyInitializationException e) {
3730            ok = true;
3731        }
3732        assertTrue( "lazy collection with many-to-many", ok );
3733
3734        s = openSession();
3735        q = (Qux) s.load( Qux.class, q.getKey() );
3736        s.delete(q);
3737        s.flush();
3738        s.connection().commit();
3739        s.close();
3740    }
3741
3742    public void testNewSessionLifecycle() throws Exception JavaDoc {
3743        Session s = openSession();
3744        Serializable JavaDoc fid = null;
3745        try {
3746            Foo f = new Foo();
3747            s.save(f);
3748            fid = s.getIdentifier(f);
3749            s.flush();
3750            s.connection().commit();
3751        }
3752        catch (Exception JavaDoc e) {
3753            s.connection().rollback();
3754            throw e;
3755        }
3756        finally {
3757            s.close();
3758        }
3759        s = openSession();
3760        try {
3761            Foo f = new Foo();
3762            s.delete(f);
3763            s.flush();
3764            s.connection().commit();
3765        }
3766        catch (Exception JavaDoc e) {
3767            s.connection().rollback();
3768        }
3769        finally {
3770            s.close();
3771        }
3772        s = openSession();
3773        try {
3774            Foo f = (Foo) s.load(Foo.class, fid, LockMode.UPGRADE);
3775            s.delete(f);
3776            s.flush();
3777            s.connection().commit();
3778        }
3779        catch (Exception JavaDoc e) {
3780            s.connection().rollback();
3781            throw e;
3782        }
3783        finally {
3784            assertTrue( s.close()==null );
3785        }
3786    }
3787
3788    public void testDisconnect() throws Exception JavaDoc {
3789        Session s = openSession();
3790        Foo foo = new Foo();
3791        Foo foo2 = new Foo();
3792        s.save(foo);
3793        s.save(foo2);
3794        foo2.setFoo(foo);
3795        s.flush();
3796        s.connection().commit();
3797        s.disconnect();
3798        s.reconnect();
3799        s.delete(foo);
3800        foo2.setFoo(null);
3801        s.flush();
3802        s.connection().commit();
3803        s.disconnect();
3804        s.reconnect();
3805        s.delete(foo2);
3806        s.flush();
3807        s.connection().commit();
3808        s.close();
3809    }
3810
3811
3812
3813    public void testOrderBy() throws Exception JavaDoc {
3814
3815        Session s = openSession();
3816        Transaction tx = s.beginTransaction();
3817        Foo foo = new Foo();
3818        s.save(foo);
3819        List JavaDoc list = s.find("select foo from Foo foo, Fee fee where foo.dependent = fee order by foo.string desc, foo.component.count asc, fee.id");
3820        assertTrue( "order by", list.size()==1 );
3821        Foo foo2 = new Foo();
3822        s.save(foo2);
3823        foo.setFoo(foo2);
3824        list = s.find("select foo.foo, foo.dependent from Foo foo order by foo.foo.string desc, foo.component.count asc, foo.dependent.id");
3825        assertTrue( "order by", list.size()==1 );
3826        list = s.find("select foo from Foo foo order by foo.dependent.id, foo.dependent.fi");
3827        assertTrue( "order by", list.size()==2 );
3828        s.delete(foo);
3829        s.delete(foo2);
3830        tx.commit();
3831        s.close();
3832
3833        s = openSession();
3834        Many manyB = new Many();
3835        s.save(manyB);
3836        One oneB = new One();
3837        s.save(oneB);
3838        oneB.setValue("b");
3839        manyB.setOne(oneB);
3840        Many manyA = new Many();
3841        s.save(manyA);
3842        One oneA = new One();
3843        s.save(oneA);
3844        oneA.setValue("a");
3845        manyA.setOne(oneA);
3846        s.flush();
3847        s.connection().commit();
3848        s.close();
3849
3850        s = openSession();
3851        Iterator JavaDoc it = s.iterate(
3852            "SELECT one FROM " +
3853            One.class.getName() +
3854            " one ORDER BY one.value ASC"
3855        );
3856        int count = 0;
3857        while ( it.hasNext() ) {
3858            One one = (One)it.next();
3859            switch (count) {
3860                case 0:
3861                assertTrue("ordering failed", "a".equals(one.getValue()));
3862                break;
3863                case 1:
3864                assertTrue("ordering failed", "b".equals(one.getValue()));
3865                break;
3866                default:
3867                assertTrue("more than two elements", false);
3868                break;
3869            }
3870            count ++;
3871        }
3872        s.flush();
3873        s.connection().commit();
3874        s.close();
3875
3876        s = openSession();
3877        it = s.iterate(
3878            "SELECT many.one FROM " +
3879            Many.class.getName() +
3880            " many ORDER BY many.one.value ASC, many.one.id"
3881        );
3882        count = 0;
3883        while ( it.hasNext() ) {
3884            One one = (One)it.next();
3885            switch (count) {
3886                case 0:
3887                assertTrue("'a' isn't first element", "a".equals(one.getValue()));
3888                break;
3889                case 1:
3890                assertTrue("'b' isn't second element", "b".equals(one.getValue()));
3891                break;
3892                default:
3893                assertTrue("more than two elements", false);
3894                break;
3895            }
3896            count ++;
3897        }
3898        s.flush();
3899        s.connection().commit();
3900        s.close();
3901
3902        s = openSession();
3903        oneA = (One)s.load(One.class, oneA.getKey());
3904        manyA = (Many)s.load(Many.class, manyA.getKey());
3905        oneB = (One)s.load(One.class, oneB.getKey());
3906        manyB = (Many)s.load(Many.class, manyB.getKey());
3907        s.delete(manyA);
3908        s.delete(oneA);
3909        s.delete(manyB);
3910        s.delete(oneB);
3911        s.flush();
3912        s.connection().commit();
3913        s.close();
3914    }
3915
3916    public void testManyToOne() throws Exception JavaDoc {
3917        Session s = openSession();
3918        One one = new One();
3919        s.save(one);
3920        one.setValue("yada");
3921        Many many = new Many();
3922        many.setOne(one);
3923        s.save(many);
3924        s.flush();
3925        s.connection().commit();
3926        s.close();
3927
3928        s = openSession();
3929        one = (One) s.load( One.class, one.getKey() );
3930        one.getManies().size();
3931        s.connection().commit();
3932        s.close();
3933
3934
3935        s = openSession();
3936        many = (Many) s.load( Many.class, many.getKey() );
3937        assertTrue( "many-to-one assoc", many.getOne()!=null );
3938        s.delete( many.getOne() );
3939        s.delete(many);
3940        s.flush();
3941        s.connection().commit();
3942        s.close();
3943    }
3944
3945    public void testSaveDelete() throws Exception JavaDoc {
3946        Session s = openSession();
3947        Foo f = new Foo();
3948        s.save(f);
3949        s.flush();
3950        s.connection().commit();
3951        s.close();
3952
3953        s = openSession();
3954        s.delete( s.load( Foo.class, f.getKey() ) );
3955        s.flush();
3956        s.connection().commit();
3957        s.close();
3958    }
3959
3960    /*public void testIdNotFound() throws Exception {
3961        // The following test is only enabled for MySQL which has no foreign key constraints.
3962        // I disabled this test cos it didn't clean up after itself so other tests failed
3963        if (db.equals("mysql")) {
3964            Session s = sessionsopenSession();
3965            Glarch g = (Glarch) s.create(Glarch.class);
3966            Glarch g2 = (Glarch) s.create(Glarch.class);
3967            g.setNext(g2);
3968            Serializable gid = s.getID(g);
3969            Serializable g2id = s.getID(g2);
3970            s.commit();
3971
3972            s = sessionsopenSession();
3973            g2 = (Glarch) s.load( Glarch.class, g2id );
3974            s.delete(g2);
3975            s.commit();
3976
3977            s = sessionsopenSession();
3978            boolean ok = false;
3979            try {
3980                g = (Glarch) s.load( Glarch.class, gid );
3981            }
3982            catch (HibernateException e) {
3983                ok = "id not found or provided object was wrong class".equals(e.getMessage());
3984            }
3985            catch (java.lang.StackOverflowError soe) {
3986                ok = false;
3987            }
3988            assertTrue( ok, "id not found");
3989            s.cancel();
3990        }
3991    }*/

3992
3993    public void testProxyArray() throws Exception JavaDoc {
3994        Session s = openSession();
3995        GlarchProxy g = new Glarch();
3996        Glarch g1 = new Glarch();
3997        Glarch g2 = new Glarch();
3998        g.setProxyArray( new GlarchProxy[] { g1, g2 } );
3999        Glarch g3 = new Glarch();
4000        s.save(g3);
4001        g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
4002        Set JavaDoc set = new HashSet JavaDoc();
4003        set.add(g1);
4004        set.add(g2);
4005        g.setProxySet(set);
4006        s.save(g);
4007        s.save(g1);
4008        s.save(g2);
4009        Serializable JavaDoc id = s.getIdentifier(g);
4010        s.flush();
4011        s.connection().commit();
4012        s.close();
4013
4014        s = openSession();
4015        g = (GlarchProxy) s.load(Glarch.class, id);
4016        assertTrue( "array of proxies", g.getProxyArray().length==2 );
4017        assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
4018        assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
4019        assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
4020        assertTrue( "set of proxies", g.getProxySet().size()==2 );
4021        Iterator JavaDoc iter = s.iterate("from Glarch g");
4022        while ( iter.hasNext() ) {
4023            iter.next();
4024            iter.remove();
4025        }
4026
4027        s.flush();
4028        s.connection().commit();
4029        s.disconnect();
4030        SerializationHelper.deserialize( SerializationHelper.serialize(s) );
4031        s.close();
4032    }
4033
4034    public void testCache() throws Exception JavaDoc {
4035        Session s = openSession();
4036        Immutable im = new Immutable();
4037        s.save(im);
4038        s.flush();
4039        s.connection().commit();
4040        s.close();
4041        s = openSession();
4042        s.load( im, im.getId() );
4043        s.connection().commit();
4044        s.close();
4045        s = openSession();
4046        s.load( im, im.getId() );
4047        assertTrue(
4048            "cached object identity",
4049            s.find(
4050                "from Immutable im where im = ?",
4051                im,
4052                Hibernate.entity(Immutable.class)
4053            ).get(0)==im &&
4054            im == s.load( Immutable.class, im.getId() )
4055        );
4056        s.connection().createStatement().executeUpdate("delete from immut");
4057        s.connection().commit();
4058        s.close();
4059    }
4060
4061    public void testFindLoad() throws Exception JavaDoc {
4062        Session s = openSession();
4063        FooProxy foo = new Foo();
4064        s.save(foo);
4065        s.flush();
4066        s.connection().commit();
4067        s.close();
4068        s = openSession();
4069        foo = (FooProxy) s.find("from Foo foo").get(0);
4070        FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
4071        assertTrue("find returns same object as load", foo==foo2);
4072        s.flush();
4073        s.connection().commit();
4074        s.close();
4075        s = openSession();
4076        foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
4077        foo = (FooProxy) s.find("from Foo foo").get(0);
4078        assertTrue("find returns same object as load", foo==foo2);
4079        s.delete("from Foo foo");
4080        s.flush();
4081        s.connection().commit();
4082        s.close();
4083    }
4084
4085    public void testRefresh() throws Exception JavaDoc {
4086        Session s = openSession();
4087        Foo foo = new Foo();
4088        s.save(foo);
4089        s.flush();
4090        s.connection().createStatement().executeUpdate("update "+getDialect().openQuote()+"foos"+getDialect().closeQuote()+" set long_ = -3");
4091        s.refresh(foo);
4092        assertTrue( foo.getLong().longValue()==-3l );
4093        assertTrue( s.getCurrentLockMode(foo)==LockMode.READ );
4094        s.refresh(foo, LockMode.UPGRADE);
4095        if ( getDialect().supportsOuterJoinForUpdate() ) {
4096            assertTrue( s.getCurrentLockMode(foo)==LockMode.UPGRADE );
4097        }
4098        s.delete(foo);
4099        s.flush();
4100        s.connection().commit();
4101        s.close();
4102    }
4103
4104    public void testAutoFlush() throws Exception JavaDoc {
4105        Session s = openSession();
4106        FooProxy foo = new Foo();
4107        s.save(foo);
4108        assertTrue( "autoflush create", s.find("from Foo foo").size()==1 );
4109        foo.setChar( new Character JavaDoc('X') );
4110        assertTrue( "autoflush update", s.find("from Foo foo where foo.char='X'").size()==1 );
4111        s.connection().commit();
4112        s.close();
4113        s = openSession();
4114        foo = (FooProxy) s.load( Foo.class, foo.getKey() );
4115        //s.update( new Foo(), foo.getKey() );
4116
//assertTrue( s.find("from Foo foo where not foo.char='X'").size()==1, "autoflush update" );
4117
if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect) ) {
4118            foo.setBytes( "osama".getBytes() );
4119            assertTrue( "autoflush collection update", s.find("from Foo foo where 111 in elements(foo.bytes)").size()==1 );
4120            foo.getBytes()[0] = 69;
4121            assertTrue( "autoflush collection update", s.find("from Foo foo where 69 in elements(foo.bytes)").size()==1 );
4122        }
4123        s.delete(foo);
4124        assertTrue( "autoflush delete", s.find("from Foo foo").size()==0 );
4125        s.connection().commit();
4126        s.close();
4127    }
4128
4129    public void testVeto() throws Exception JavaDoc {
4130        Session s = openSession();
4131        Vetoer v = new Vetoer();
4132        s.save(v); Serializable JavaDoc id = s.save(v);
4133        s.flush();
4134        s.connection().commit();
4135        s.close();
4136        s = openSession();
4137        s.update(v, id); s.update(v, id);
4138        s.delete(v); s.delete(v);
4139        s.flush();
4140        s.connection().commit();
4141        s.close();
4142    }
4143
4144    public void testSerializableType() throws Exception JavaDoc {
4145        Session s = openSession();
4146        Vetoer v = new Vetoer();
4147        v.setStrings( new String JavaDoc[] { "foo", "bar", "baz" } );
4148        s.save(v); Serializable JavaDoc id = s.save(v);
4149        v.getStrings()[1] = "osama";
4150        s.flush();
4151        s.connection().commit();
4152        s.close();
4153        s = openSession();
4154        v = (Vetoer) s.load(Vetoer.class, id);
4155        assertTrue( "serializable type", v.getStrings()[1].equals("osama") );
4156        s.delete(v); s.delete(v);
4157        s.flush();
4158        s.connection().commit();
4159        s.close();
4160    }
4161
4162    public void testAutoFlushCollections() throws Exception JavaDoc {
4163        Session s = openSession();
4164        Transaction tx = s.beginTransaction();
4165        Baz baz = new Baz();
4166        baz.setDefaults();
4167        s.save(baz);
4168        tx.commit();
4169        s.close();
4170
4171        s = openSession();
4172        tx = s.beginTransaction();
4173        baz = (Baz) s.load(Baz.class, baz.getCode());
4174        baz.getStringArray()[0] = "bark";
4175        Iterator JavaDoc i = s.iterate("select elements(baz.stringArray) from Baz baz");
4176        boolean found = false;
4177        while ( i.hasNext() ) {
4178            if ( "bark".equals( i.next() ) ) found = true;
4179        }
4180        assertTrue(found);
4181        baz.setStringArray(null);
4182        i = s.iterate("select distinct elements(baz.stringArray) from Baz baz");
4183        assertTrue( !i.hasNext() );
4184        baz.setStringArray( new String JavaDoc[] { "foo", "bar" } );
4185        i = s.iterate("select elements(baz.stringArray) from Baz baz");
4186        assertTrue( i.hasNext() );
4187
4188        Foo foo = new Foo();
4189        s.save(foo);
4190        s.flush();
4191        baz.setFooArray( new Foo[] {foo} );
4192
4193        i = s.iterate("select foo from Baz baz join baz.fooArray foo");
4194        found = false;
4195        while ( i.hasNext() ) {
4196            if ( foo==i.next() ) found = true;
4197        }
4198        assertTrue(found);
4199
4200        baz.getFooArray()[0] = null;
4201        i = s.iterate("select foo from Baz baz join baz.fooArray foo");
4202        assertTrue( !i.hasNext() );
4203        baz.getFooArray()[0] = foo;
4204        i = s.iterate("select elements(baz.fooArray) from Baz baz");
4205        assertTrue( i.hasNext() );
4206
4207        if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof InterbaseDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof SAPDBDialect) ) {
4208            baz.getFooArray()[0] = null;
4209            i = s.iterate(
4210                "from Baz baz where ? in elements(baz.fooArray)",
4211                foo, Hibernate.entity(Foo.class)
4212            );
4213            assertTrue( !i.hasNext() );
4214            baz.getFooArray()[0] = foo;
4215            i = s.iterate(
4216                "select foo from Foo foo where foo in "
4217                + "(select elt from Baz baz join baz.fooArray elt)"
4218            );
4219            assertTrue( i.hasNext() );
4220        }
4221        s.delete(foo);
4222        s.delete(baz);
4223        tx.commit();
4224        s.close();
4225
4226    }
4227
4228    public void testUserProvidedConnection() throws Exception JavaDoc {
4229        ConnectionProvider dcp = new DriverManagerConnectionProvider();
4230        dcp.configure( Environment.getProperties() );
4231        Session s = getSessions().openSession( dcp.getConnection() );
4232        Transaction tx = s.beginTransaction();
4233        s.find("from Fo");
4234        tx.commit();
4235        Connection JavaDoc c = s.disconnect();
4236        assertTrue( c!=null );
4237        s.reconnect(c);
4238        tx = s.beginTransaction();
4239        s.find("from Fo");
4240        tx.commit();
4241        assertTrue( s.close()==c );
4242        c.close();
4243    }
4244
4245    public void testCachedCollection() throws Exception JavaDoc {
4246        Session s = openSession();
4247        Baz baz = new Baz();
4248        baz.setDefaults();
4249        s.save(baz);
4250        s.flush();
4251        s.connection().commit();
4252        s.close();
4253        s = openSession();
4254        baz = (Baz) s.load( Baz.class, baz.getCode() );
4255        ( (FooComponent) baz.getTopComponents().get(0) ).setCount(99);
4256        s.flush();
4257        s.connection().commit();
4258        s.close();
4259        s = openSession();
4260        baz = (Baz) s.load( Baz.class, baz.getCode() );
4261        assertTrue( ( (FooComponent) baz.getTopComponents().get(0) ).getCount()==99 );
4262        s.delete(baz);
4263        s.flush();
4264        s.connection().commit();
4265        s.close();
4266    }
4267
4268    public void testComplicatedQuery() throws Exception JavaDoc {
4269        Session s = openSession();
4270        Foo foo = new Foo();
4271        Serializable JavaDoc id = s.save(foo);
4272        assertTrue(id!=null);
4273        Qux q = new Qux("q");
4274        foo.getDependent().setQux(q);
4275        s.save(q);
4276        q.getFoo().setString("foo2");
4277        //s.flush();
4278
//s.connection().commit();
4279
assertTrue(
4280            s.iterate("from Foo foo where foo.dependent.qux.foo.string = 'foo2'").hasNext()
4281        );
4282        s.delete(foo);
4283        s.flush();
4284        s.connection().commit();
4285        s.close();
4286    }
4287
4288    public void testLoadAfterDelete() throws Exception JavaDoc {
4289        Session s = openSession();
4290        Foo foo = new Foo();
4291        Serializable JavaDoc id = s.save(foo);
4292        s.flush();
4293        s.delete(foo);
4294        boolean err=false;
4295        try {
4296            s.load(Foo.class, id);
4297        }
4298        catch (ObjectDeletedException ode) {
4299            err=true;
4300        }
4301        assertTrue(err);
4302        s.flush();
4303        err=false;
4304        try {
4305            ( (FooProxy) s.load(Foo.class, id) ).getBool();
4306        }
4307        catch (ObjectNotFoundException onfe) {
4308            err=true;
4309        }
4310        assertTrue(err);
4311        Fo fo = Fo.newFo();
4312        id = new FumTest("").fumKey("abc"); //yuck!!
4313
s.save(fo, id);
4314        s.flush();
4315        s.delete(fo);
4316        err=false;
4317        try {
4318            s.load(Fo.class, id);
4319        }
4320        catch (ObjectDeletedException ode) {
4321            err=true;
4322        }
4323        assertTrue(err);
4324        s.flush();
4325        err=false;
4326        try {
4327            s.load(Fo.class, id);
4328        }
4329        catch (ObjectNotFoundException onfe) {
4330            err=true;
4331        }
4332        assertTrue(err);
4333        s.connection().commit();
4334        s.close();
4335    }
4336
4337    public void testObjectType() throws Exception JavaDoc {
4338        Session s = openSession();
4339        GlarchProxy g = new Glarch();
4340        Foo foo = new Foo();
4341        g.setAny(foo);
4342        Serializable JavaDoc gid = s.save(g);
4343        s.save(foo);
4344        s.flush();
4345        s.connection().commit();
4346        s.close();
4347        s = openSession();
4348        g = (GlarchProxy) s.load(Glarch.class, gid);
4349        assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy );
4350        s.delete( g.getAny() );
4351        s.delete(g);
4352        //s.delete( g.getAny() );
4353
s.flush();
4354        s.connection().commit();
4355        s.close();
4356    }
4357
4358
4359    public void testAny() throws Exception JavaDoc {
4360        Session s = openSession();
4361        One one = new One();
4362        BarProxy foo = new Bar();
4363        foo.setObject(one);
4364        //Serializable oid = s.save(one);
4365
Serializable JavaDoc fid = s.save(foo);
4366        Serializable JavaDoc oid = one.getKey();
4367        s.flush();
4368        s.connection().commit();
4369        s.close();
4370        s = openSession();
4371        assertTrue( s.find(
4372            "from Bar bar where bar.object.id = ? and bar.object.class = ?",
4373            new Object JavaDoc[] { oid, new Character JavaDoc('O') },
4374            new Type[] { Hibernate.LONG, Hibernate.CHARACTER }
4375        ).size()==1 );
4376        assertTrue( s.find(
4377            "select one from One one, Bar bar where bar.object.id = one.id and bar.object.class = 'O'"
4378        ).size()==1 );
4379        s.flush();
4380        s.connection().commit();
4381        s.close();
4382        s = openSession();
4383        foo = (BarProxy) s.load(Foo.class, fid);
4384        assertTrue( foo.getObject()!=null && foo.getObject() instanceof One && s.getIdentifier( foo.getObject() ).equals(oid) );
4385        //s.delete( foo.getObject() );
4386
s.delete(foo);
4387        s.flush();
4388        s.connection().commit();
4389        s.close();
4390    }
4391
4392    public void testEmbeddedCompositeID() throws Exception JavaDoc {
4393        Session s = openSession();
4394        Location l = new Location();
4395        l.setCountryCode("AU");
4396        l.setDescription("foo bar");
4397        l.setLocale( Locale.getDefault() );
4398        l.setStreetName("Brunswick Rd");
4399        l.setStreetNumber(300);
4400        l.setCity("Melbourne");
4401        s.save(l);
4402        s.flush();
4403        s.connection().commit();
4404        s.close();
4405        s = openSession();
4406        s.setFlushMode(FlushMode.NEVER);
4407        l = (Location) s.find("from Location l where l.countryCode = 'AU' and l.description='foo bar'").get(0);
4408        assertTrue( l.getCountryCode().equals("AU") );
4409        assertTrue( l.getCity().equals("Melbourne") );
4410        assertTrue( l.getLocale().equals( Locale.getDefault() ) );
4411        assertTrue( s.createCriteria(Location.class).add( Expression.eq( "streetNumber", new Integer JavaDoc(300) ) ).list().size()==1 );
4412        s.connection().commit();
4413        s.close();
4414
4415        s = openSession();
4416        l.setDescription("sick're");
4417        s.update(l);
4418        s.flush();
4419        s.connection().commit();
4420        s.close();
4421        s = openSession();
4422        l = new Location();
4423        l.setCountryCode("AU");
4424        l.setDescription("foo bar");
4425        l.setLocale(Locale.ENGLISH);
4426        l.setStreetName("Brunswick Rd");
4427        l.setStreetNumber(300);
4428        l.setCity("Melbourne");
4429        assertTrue( l==s.load(Location.class, l) );
4430        assertTrue( l.getLocale().equals( Locale.getDefault() ) );
4431        s.delete(l);
4432        s.flush();
4433        s.connection().commit();
4434        s.close();
4435    }
4436
4437    public void testAutosaveChildren() throws Exception JavaDoc {
4438        Session s = openSession();
4439        Transaction t = s.beginTransaction();
4440        Baz baz = new Baz();
4441        Set JavaDoc bars = new HashSet JavaDoc();
4442        baz.setCascadingBars(bars);
4443        s.save(baz);
4444        t.commit();
4445        s.close();
4446
4447        s = openSession();
4448        t = s.beginTransaction();
4449        baz = (Baz) s.load( Baz.class, baz.getCode() );
4450        baz.getCascadingBars().add( new Bar() );
4451        baz.getCascadingBars().add( new Bar() );
4452        t.commit();
4453        s.close();
4454
4455        s = openSession();
4456        t = s.beginTransaction();
4457        baz = (Baz) s.load( Baz.class, baz.getCode() );
4458        assertTrue( baz.getCascadingBars().size()==2 );
4459        assertTrue( baz.getCascadingBars().iterator().next()!=null );
4460        baz.getCascadingBars().clear(); //test all-delete-orphan;
4461
s.flush();
4462        assertTrue( s.find("from Bar bar").size()==0 );
4463        s.delete(baz);
4464        t.commit();
4465        s.close();
4466    }
4467
4468    public void testOrphanDelete() throws Exception JavaDoc {
4469        Session s = openSession();
4470        Transaction t = s.beginTransaction();
4471        Baz baz = new Baz();
4472        Set JavaDoc bars = new HashSet JavaDoc();
4473        baz.setCascadingBars(bars);
4474        bars.add( new Bar() );
4475        bars.add( new Bar() );
4476        bars.add( new Bar() );
4477        bars.add( new Bar() );
4478        s.save(baz);
4479        t.commit();
4480        s.close();
4481
4482        s = openSession();
4483        t = s.beginTransaction();
4484        baz = (Baz) s.load( Baz.class, baz.getCode() );
4485        bars = baz.getCascadingBars();
4486        assertEquals( 4, bars.size() );
4487        bars.remove( bars.iterator().next() );
4488        assertEquals( 3, s.find("From Bar bar").size() );
4489        t.commit();
4490        s.close();
4491
4492        s = openSession();
4493        t = s.beginTransaction();
4494        baz = (Baz) s.load( Baz.class, baz.getCode() );
4495        bars = baz.getCascadingBars();
4496        assertEquals( 3, bars.size() );
4497        bars.remove( bars.iterator().next() );
4498        s.delete(baz);
4499        bars.remove( bars.iterator().next() );
4500        assertEquals( 0, s.find("From Bar bar").size() );
4501        t.commit();
4502        s.close();
4503
4504    }
4505
4506    public void testTransientOrphanDelete() throws Exception JavaDoc {
4507        Session s = openSession();
4508        Transaction t = s.beginTransaction();
4509        Baz baz = new Baz();
4510        Set JavaDoc bars = new HashSet JavaDoc();
4511        baz.setCascadingBars(bars);
4512        bars.add( new Bar() );
4513        bars.add( new Bar() );
4514        bars.add( new Bar() );
4515        List JavaDoc foos = new ArrayList JavaDoc();
4516        foos.add( new Foo() );
4517        foos.add( new Foo() );
4518        baz.setFooBag(foos);
4519        s.save(baz);
4520        Iterator JavaDoc i = new JoinedIterator( new Iterator JavaDoc[] {foos.iterator(), bars.iterator()} );
4521        while ( i.hasNext() ) {
4522            FooComponent cmp = ( (Foo) i.next() ).getComponent();
4523            s.delete( cmp.getGlarch() );
4524            cmp.setGlarch(null);
4525        }
4526        t.commit();
4527        s.close();
4528
4529        bars.remove( bars.iterator().next() );
4530        foos.remove(1);
4531        s = openSession();
4532        t = s.beginTransaction();
4533        s.update(baz);
4534        assertEquals( 2, s.find("From Bar bar").size() );
4535        assertEquals( 3, s.find("From Foo foo").size() );
4536        t.commit();
4537        s.close();
4538
4539        foos.remove(0);
4540        s = openSession();
4541        t = s.beginTransaction();
4542        s.update(baz);
4543        bars.remove( bars.iterator().next() );
4544        assertEquals( 1, s.find("From Foo foo").size() );
4545        s.delete(baz);
4546        //s.flush();
4547
assertEquals( 0, s.find("From Foo foo").size() );
4548        t.commit();
4549        s.close();
4550
4551    }
4552
4553    public void testProxiesInCollections() throws Exception JavaDoc {
4554        Session s = openSession();
4555        Baz baz = new Baz();
4556        Bar bar = new Bar();
4557        Bar bar2 = new Bar();
4558        s.save(bar);
4559        Serializable JavaDoc bar2id = s.save(bar2);
4560        baz.setFooArray( new Foo[] { bar, bar2 } );
4561        HashSet JavaDoc set = new HashSet JavaDoc();
4562        bar = new Bar();
4563        s.save(bar);
4564        set.add(bar);
4565        baz.setFooSet(set);
4566        set = new HashSet JavaDoc();
4567        set.add( new Bar() );
4568        set.add( new Bar() );
4569        baz.setCascadingBars(set);
4570        ArrayList JavaDoc list = new ArrayList JavaDoc();
4571        list.add( new Foo() );
4572        baz.setFooBag(list);
4573        Serializable JavaDoc id = s.save(baz);
4574        Serializable JavaDoc bid = ( (Bar) baz.getCascadingBars().iterator().next() ).getKey();
4575        s.flush();
4576        s.connection().commit();
4577        s.close();
4578
4579        s = openSession();
4580        BarProxy barprox = (BarProxy) s.load(Bar.class, bid);
4581        BarProxy bar2prox = (BarProxy) s.load(Bar.class, bar2id);
4582        assertTrue(bar2prox instanceof HibernateProxy);
4583        assertTrue(barprox instanceof HibernateProxy);
4584        baz = (Baz) s.load(Baz.class, id);
4585        Iterator JavaDoc i = baz.getCascadingBars().iterator();
4586        BarProxy b1 = (BarProxy) i.next();
4587        BarProxy b2 = (BarProxy) i.next();
4588        assertTrue( ( b1==barprox && !(b2 instanceof HibernateProxy) ) || ( b2==barprox && !(b1 instanceof HibernateProxy) ) ); //one-to-many
4589
assertTrue( baz.getFooArray()[0] instanceof HibernateProxy ); //many-to-many
4590
assertTrue( baz.getFooArray()[1]==bar2prox );
4591        if ( !isOuterJoinFetchingDisabled() ) assertTrue( !(baz.getFooBag().iterator().next() instanceof HibernateProxy) ); //many-to-many outer-join="true"
4592
assertTrue( !(baz.getFooSet().iterator().next() instanceof HibernateProxy) ); //one-to-many
4593
s.delete("from Baz");
4594        s.delete("from Foo");
4595        s.flush();
4596        s.connection().commit();
4597        s.close();
4598    }
4599
4600    public void testService() throws Exception JavaDoc {
4601        HibernateService hs = new HibernateService();
4602        hs.setJndiName("SessionFactory");
4603        hs.setMapResources("net/sf/hibernate/test/Simple.hbm.xml, net/sf/hibernate/test/Blobber.hbm.xml");
4604        hs.setShowSqlEnabled("true");
4605        hs.start();
4606        hs.stop();
4607        hs.setProperty("foo", "bar");
4608        hs.start();
4609        hs.stop();
4610    }
4611
4612    public void testPSCache() throws Exception JavaDoc {
4613        Session s = openSession();
4614        for ( int i=0; i<10; i++ ) s.save( new Foo() );
4615        Query q = s.createQuery("from Foo");
4616        q.setMaxResults(2);
4617        q.setFirstResult(5);
4618        assertTrue( q.list().size()==2 );
4619        q = s.createQuery("from Foo");
4620        assertTrue( q.list().size()==10 );
4621        assertTrue( q.list().size()==10 );
4622        q.setMaxResults(3);
4623        q.setFirstResult(3);
4624        assertTrue( q.list().size()==3 );
4625        q = s.createQuery("from Foo");
4626        assertTrue( q.list().size()==10 );
4627        s.connection().commit();
4628        s.close();
4629        s = openSession();
4630        q = s.createQuery("from Foo");
4631        assertTrue( q.list().size()==10 );
4632        q.setMaxResults(5);
4633        assertTrue( q.list().size()==5 );
4634        s.delete("from Foo");
4635        s.connection().commit();
4636        s.close();
4637
4638    }
4639
4640    public void testForCertain() throws Exception JavaDoc {
4641        Glarch g = new Glarch();
4642        Glarch g2 = new Glarch();
4643        List JavaDoc set = new ArrayList JavaDoc();
4644        set.add("foo");
4645        g2.setStrings(set);
4646        Session s = openSession();
4647        Transaction t = s.beginTransaction();
4648        Serializable JavaDoc gid = (Serializable JavaDoc) s.save(g);
4649        Serializable JavaDoc g2id = (Serializable JavaDoc) s.save(g2);
4650        t.commit();
4651        assertTrue( g.getVersion()==0 );
4652        assertTrue( g2.getVersion()==0 );
4653        s.close();
4654
4655        s = openSession();
4656        t = s.beginTransaction();
4657        g = (Glarch) s.get(Glarch.class, gid);
4658        g2 = (Glarch) s.get(Glarch.class, g2id);
4659        assertTrue( g2.getStrings().size()==1 );
4660        s.delete(g);
4661        s.delete(g2);
4662        t.commit();
4663        s.close();
4664
4665    }
4666
4667    public void testBagMultipleElements() throws Exception JavaDoc {
4668        Session s = openSession();
4669        Transaction t = s.beginTransaction();
4670        Baz baz = new Baz();
4671        baz.setBag( new ArrayList JavaDoc() );
4672        baz.setByteBag( new ArrayList JavaDoc() );
4673        s.save(baz);
4674        baz.getBag().add("foo");
4675        baz.getBag().add("bar");
4676        baz.getByteBag().add( "foo".getBytes() );
4677        baz.getByteBag().add( "bar".getBytes() );
4678        t.commit();
4679        s.close();
4680
4681        s = openSession();
4682        t = s.beginTransaction();
4683        //put in cache
4684
baz = (Baz) s.get( Baz.class, baz.getCode() );
4685        assertTrue( baz.getBag().size()==2 );
4686        assertTrue( baz.getByteBag().size()==2 );
4687        t.commit();
4688        s.close();
4689
4690        s = openSession();
4691        t = s.beginTransaction();
4692        baz = (Baz) s.get( Baz.class, baz.getCode() );
4693        assertTrue( baz.getBag().size()==2 );
4694        assertTrue( baz.getByteBag().size()==2 );
4695        baz.getBag().remove("bar");
4696        baz.getBag().add("foo");
4697        baz.getByteBag().add( "bar".getBytes() );
4698        t.commit();
4699        s.close();
4700
4701        s = openSession();
4702        t = s.beginTransaction();
4703        baz = (Baz) s.get( Baz.class, baz.getCode() );
4704        assertTrue( baz.getBag().size()==2 );
4705        assertTrue( baz.getByteBag().size()==3 );
4706        s.delete(baz);
4707        t.commit();
4708        s.close();
4709    }
4710
4711    public void testWierdSession() throws Exception JavaDoc {
4712        Session s = openSession();
4713        Transaction t = s.beginTransaction();
4714        Serializable JavaDoc id = s.save( new Foo() );
4715        t.commit();
4716        s.close();
4717
4718        s = openSession();
4719        s.setFlushMode(FlushMode.NEVER);
4720        t = s.beginTransaction();
4721        Foo foo = (Foo) s.get(Foo.class, id);
4722        t.commit();
4723        s.disconnect();
4724
4725        s.reconnect();
4726        t = s.beginTransaction();
4727        s.flush();
4728        t.commit();
4729        s.close();
4730
4731        s = openSession();
4732        t = s.beginTransaction();
4733        foo = (Foo) s.get(Foo.class, id);
4734        s.delete(foo);
4735        t.commit();
4736        s.close();
4737    }
4738
4739    public static Test suite() {
4740        return new TestSuite(FooBarTest.class);
4741    }
4742
4743    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
4744        TestRunner.run( suite() );
4745    }
4746
4747    protected void configure(Configuration cfg) {
4748        if ( getDialect() instanceof OracleDialect ) {
4749            ( (RootClass) cfg.getClassMapping("org.hibernate.test.legacy.Foo") ).setForceDiscriminator(false);
4750        }
4751    }
4752
4753    protected String JavaDoc[] getMappings() {
4754        return new String JavaDoc[] {
4755            "legacy/FooBar.hbm.xml",
4756            "legacy/Baz.hbm.xml",
4757            "legacy/Qux.hbm.xml",
4758            "legacy/Glarch.hbm.xml",
4759            "legacy/Fum.hbm.xml",
4760            "legacy/Fumm.hbm.xml",
4761            "legacy/Fo.hbm.xml",
4762            "legacy/One.hbm.xml",
4763            "legacy/Many.hbm.xml",
4764            "legacy/Immutable.hbm.xml",
4765            "legacy/Fee.hbm.xml",
4766            "legacy/Vetoer.hbm.xml",
4767            "legacy/Holder.hbm.xml",
4768            "legacy/Location.hbm.xml",
4769            "legacy/Stuff.hbm.xml",
4770            "legacy/Container.hbm.xml",
4771            "legacy/Simple.hbm.xml",
4772            "legacy/XY.hbm.xml"
4773        };
4774    }
4775
4776}
4777
Popular Tags