Source code: org/hibernate/test/legacy/FooBarTest.java
1 //$Id: FooBarTest.java,v 1.28 2005/03/16 02:45:46 steveebersole Exp $
2 package org.hibernate.test.legacy;
3
4 import java.io.Serializable;
5 import java.sql.Connection;
6 import java.sql.Time;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Locale;
16 import java.util.Set;
17 import java.util.SortedSet;
18 import java.util.TimeZone;
19 import java.util.TreeMap;
20 import java.util.TreeSet;
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 arg) {
74 super(arg);
75 }
76
77 public void testSaveOrUpdateCopyAny() throws Exception {
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 {
101 Session s = openSession();
102 Glarch g = new Glarch();
103 Serializable 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 {
116
117 if (getDialect() instanceof MySQLDialect) return;
118
119 Session s = openSession();
120 Baz baz = new Baz();
121 baz.subs = new ArrayList();
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 {
138 Session s = openSession();
139 Baz baz = new Baz();
140 baz.setByteBag( new ArrayList() );
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 {
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( 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 {
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 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 {
214 Session s = openSession();
215 Baz baz = new Baz();
216 baz.setFooSet( new HashSet() );
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 {
260 Session s = openSession();
261 Baz baz = new Baz();
262 Baz baz2 = new Baz();
263 baz.setFooSet( new HashSet() );
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 {
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 {
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 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[] row1 = (Object[]) 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[]) 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 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(), 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[] row = (Object[]) iter.next();
475 assertTrue( row[0] instanceof String && ( row[1]==null || row[1] instanceof Date ) );
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 );
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() );
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 rs = s.iterate("select count(distinct foo.foo) from Foo foo");
517 assertTrue( "count", ( (Integer) rs.next() ).intValue()==2 );
518 assertTrue( !rs.hasNext() );
519 rs = s.iterate("select count(foo.foo.boolean) from Foo foo");
520 assertTrue( "count", ( (Integer) 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[]) rs.next() )[0].equals( new Integer(3) ) );
524 assertTrue( !rs.hasNext() );
525 rs = s.iterate("select sum(foo.foo.int) from Foo foo");
526 assertTrue( "sum", ( (Integer) 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) rs.next() ).intValue()==1 );
530 assertTrue( !rs.hasNext() );
531
532 list = s.find( "from Foo foo where foo.boolean = ?", new Boolean(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[] row = (Object[]) rs.next();
562 assertTrue( row[0] instanceof Long );
563 assertTrue( row[1] instanceof String );
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[] row = (Object[]) rs.next();
574 assertTrue( row[0] instanceof Float );
575 assertTrue( row[1] instanceof String );
576 assertTrue( row[2] instanceof Integer );
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[] row = (Object[]) rs.next();
585 assertTrue( row[0] instanceof Long );
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[] row = (Object[]) 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(), 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[]) 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 {
707 Session s = openSession();
708 Baz baz = new Baz();
709 List list = new ArrayList();
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();
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 {
760 Session s = openSession();
761 Baz baz = new Baz();
762 Foo foo = new Foo();
763 List bag = new ArrayList();
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 {
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 {
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[] {"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 {
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 hid = s.save(h);
848 Qux q = new Qux();
849 q.setHolder(h2);
850 Serializable 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[] res = (Object[]) 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 {
871 Session s = openSession();
872 Baz baz = new Baz();
873 baz.setDefaults();
874 s.save(baz);
875 Glarch g = new Glarch();
876 Serializable 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 {
920 Session s = openSession();
921 Baz baz = new Baz();
922 SortedSet stringSet = new TreeSet();
923 stringSet.add("foo");
924 stringSet.add("bar");
925 Set fooSet = new HashSet();
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();
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();
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 iter = new JoinedIterator( new Iterator[] { 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 {
977 Session s = openSession();
978 Baz baz = new Baz();
979 Collection fooBag = new ArrayList();
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 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 {
1005 Session s = openSession();
1006 Baz baz = new Baz();
1007 List l = new ArrayList();
1008 baz.setStringList(l);
1009 l.add("foo");
1010 Serializable 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 {
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(false) );
1042 foo2.setBytes( new byte[] { 1,2,3} );
1043 foo2.setDate(null);
1044 foo2.setShort( new Short("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 {
1077 Session s = openSession();
1078 Baz b = new Baz();
1079 List stringList = new ArrayList();
1080 List feeList = new ArrayList();
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 {
1113 Session s = openSession();
1114 Baz baz = new Baz();
1115 Collection fooBag = new ArrayList();
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 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 {
1144 Session s = openSession();
1145 Baz b = new Baz();
1146 b.setName("name");
1147 SortedSet ss = new TreeSet();
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 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 {
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 list = new ArrayList();
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[]) 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 {
1218 Session s = openSession();
1219 Baz baz = new Baz();
1220 List list = new ArrayList();
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 {
1238
1239 Session s = openSession();
1240 Bar bar = new Bar();
1241 s.save(bar);
1242 s.flush();
1243 bar.setString("changed");
1244 Baz baz = new Baz();
1245 baz.setFoo(bar);
1246 s.save(baz);
1247 Query q = s.createQuery("from Foo foo, Bar bar");
1248 if ( !(getDialect() instanceof DB2Dialect) ) {
1249 q.setLockMode("bar", LockMode.UPGRADE);
1250 }
1251 Object[] result = (Object[]) q.uniqueResult();
1252 Object b = result[0];
1253 assertTrue( s.getCurrentLockMode(b)==LockMode.WRITE && s.getCurrentLockMode( result[1] )==LockMode.WRITE );
1254 s.flush();
1255 s.connection().commit();
1256 s.disconnect();
1257
1258 s.reconnect();
1259 assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1260 s.find("from Foo foo");
1261 assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1262 q = s.createQuery("from Foo foo");
1263 q.setLockMode("foo", LockMode.READ);
1264 q.list();
1265 assertTrue( s.getCurrentLockMode(b)==LockMode.READ);
1266 s.evict(baz);
1267 s.connection().commit();
1268 s.disconnect();
1269 s.reconnect();
1270 assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1271 s.delete( s.load( Baz.class, baz.getCode() ) );
1272 assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
1273 s.flush();
1274 s.connection().commit();
1275 s.close();
1276
1277 s = openSession();
1278 q = s.createQuery("from Foo foo, Bar bar, Bar bar2");
1279 if ( !(getDialect() instanceof DB2Dialect) ) {
1280 q.setLockMode("bar", LockMode.UPGRADE);
1281 }
1282 q.setLockMode("bar2", LockMode.READ);
1283 result = (Object[]) q.list().get(0);
1284 if ( !(getDialect() instanceof DB2Dialect) ) {
1285 assertTrue( s.getCurrentLockMode( result[0] )==LockMode.UPGRADE && s.getCurrentLockMode( result[1] )==LockMode.UPGRADE );
1286 }
1287 s.delete( result[0] );
1288 s.flush();
1289 s.connection().commit();
1290 s.close();
1291 }
1292
1293 public void testManyToManyBag() throws Exception {
1294
1295 Session s = openSession();
1296 Baz baz = new Baz();
1297 Serializable id = s.save(baz);
1298 s.flush();
1299 s.connection().commit();
1300 s.close();
1301
1302 s = openSession();
1303 baz = (Baz) s.load(Baz.class, id);
1304 baz.getFooBag().add( new Foo() );
1305 s.flush();
1306 s.connection().commit();
1307 s.close();
1308
1309 s = openSession();
1310 baz = (Baz) s.load(Baz.class, id);
1311 assertTrue( !Hibernate.isInitialized( baz.getFooBag() ) );
1312 assertTrue( baz.getFooBag().size()==1 );
1313 if ( !(getDialect() instanceof HSQLDialect) ) assertTrue( Hibernate.isInitialized( baz.getFooBag().iterator().next() ) );
1314 s.delete(baz);
1315 s.flush();
1316 s.connection().commit();
1317 s.close();
1318 }
1319
1320 public void testIdBag() throws Exception {
1321 Session s = openSession();
1322 Baz baz = new Baz();
1323 s.save(baz);
1324 List l = new ArrayList();
1325 List l2 = new ArrayList();
1326 baz.setIdFooBag(l);
1327 baz.setByteBag(l2);
1328 l.add( new Foo() );
1329 l.add( new Bar() );
1330 byte[] bytes = "ffo".getBytes();
1331 l2.add(bytes);
1332 l2.add( "foo".getBytes() );
1333 s.flush();
1334 l.add( new Foo() );
1335 l.add( new Bar() );
1336 l2.add( "bar".getBytes() );
1337 s.flush();
1338 s.delete( l.remove(3) );
1339 bytes[1]='o';
1340 s.flush();
1341 s.connection().commit();
1342 s.close();
1343
1344 s = openSession();
1345 baz = (Baz) s.load(Baz.class, baz.getCode());
1346 assertTrue( baz.getIdFooBag().size()==3 );
1347 assertTrue( baz.getByteBag().size()==3 );
1348 bytes = "foobar".getBytes();
1349 Iterator iter = baz.getIdFooBag().iterator();
1350 while ( iter.hasNext() ) s.delete( iter.next() );
1351 baz.setIdFooBag(null);
1352 baz.getByteBag().add(bytes);
1353 baz.getByteBag().add(bytes);
1354 assertTrue( baz.getByteBag().size()==5 );
1355 s.flush();
1356 s.connection().commit();
1357 s.close();
1358
1359 s = openSession();
1360 baz = (Baz) s.load(Baz.class, baz.getCode());
1361 assertTrue( baz.getIdFooBag().size()==0 );
1362 assertTrue( baz.getByteBag().size()==5 );
1363 baz.getIdFooBag().add( new Foo() );
1364 iter = baz.getByteBag().iterator();
1365 iter.next();
1366 iter.remove();
1367 s.flush();
1368 s.connection().commit();
1369 s.close();
1370
1371 s = openSession();
1372 baz = (Baz) s.load(Baz.class, baz.getCode());
1373 assertTrue( baz.getIdFooBag().size()==1 );
1374 assertTrue( baz.getByteBag().size()==4 );
1375 s.delete(baz);
1376 s.flush();
1377 s.connection().commit();
1378 s.close();
1379 }
1380
1381 private boolean isOuterJoinFetchingDisabled() {
1382 return new Integer(0).equals( ( (SessionFactoryImplementor) getSessions() ).getSettings().getMaximumFetchDepth() );
1383 }
1384
1385 public void testForceOuterJoin() throws Exception {
1386
1387 if ( isOuterJoinFetchingDisabled() ) return;
1388
1389 Session s = openSession();
1390 Glarch g = new Glarch();
1391 FooComponent fc = new FooComponent();
1392 fc.setGlarch(g);
1393 FooProxy f = new Foo();
1394 FooProxy f2 = new Foo();
1395 f.setComponent(fc);
1396 f.setFoo(f2);
1397 s.save(f2);
1398 Serializable id = s.save(f);
1399 Serializable gid = s.getIdentifier( f.getComponent().getGlarch() );
1400 s.flush();
1401 s.connection().commit();
1402 s.close();
1403
1404 getSessions().evict(Foo.class);
1405
1406 s = openSession();
1407 f = (FooProxy) s.load(Foo.class, id);
1408 assertFalse( Hibernate.isInitialized(f) );
1409 assertTrue( Hibernate.isInitialized( f.getComponent().getGlarch() ) ); //outer-join="true"
1410 assertFalse( Hibernate.isInitialized( f.getFoo() ) ); //outer-join="auto"
1411 assertEquals( s.getIdentifier( f.getComponent().getGlarch() ), gid );
1412 s.delete(f);
1413 s.delete( f.getFoo() );
1414 s.flush();
1415 s.connection().commit();
1416 s.close();
1417 }
1418
1419 public void testEmptyCollection() throws Exception {
1420 Session s = openSession();
1421 Serializable id = s.save( new Baz() );
1422 s.flush();
1423 s.connection().commit();
1424 s.close();
1425 s = openSession();
1426 Baz baz = (Baz) s.load(Baz.class, id);
1427 Set foos = baz.getFooSet();
1428 assertTrue( foos.size()==0 );
1429 Foo foo = new Foo();
1430 foos.add(foo);
1431 s.save(foo);
1432 s.flush();
1433 s.delete(foo);
1434 s.delete(baz);
1435 s.flush();
1436 s.connection().commit();
1437 s.close();
1438 }
1439
1440 public void testOneToOneGenerator() throws Exception {
1441 Session s = openSession();
1442 X x = new X();
1443 Y y = new Y();
1444 x.setY(y);
1445 y.setTheX(x);
1446 x.getXxs().add( new X.XX(x) );
1447 x.getXxs().add( new X.XX(x) );
1448 Serializable id = s.save(y);
1449 assertEquals( id, s.save(x) );
1450 s.flush();
1451 assertTrue( s.contains(y) && s.contains(x) );
1452 s.connection().commit();
1453 s.close();
1454 assertEquals( new Long(x.getId()), y.getId() );
1455
1456 s = openSession();
1457 x = new X();
1458 y = new Y();
1459 x.setY(y);
1460 y.setTheX(x);
1461 x.getXxs().add( new X.XX(x) );
1462 s.save(y);
1463 s.flush();
1464 assertTrue( s.contains(y) && s.contains(x) );
1465 s.connection().commit();
1466 s.close();
1467 assertEquals( new Long(x.getId()), y.getId() );
1468
1469 s = openSession();
1470 x = new X();
1471 y = new Y();
1472 x.setY(y);
1473 y.setTheX(x);
1474 x.getXxs().add( new X.XX(x) );
1475 x.getXxs().add( new X.XX(x) );
1476 id = s.save(x);
1477 assertEquals( id, y.getId() );
1478 assertEquals( id, new Long( x.getId() ) );
1479 s.flush();
1480 assertTrue( s.contains(y) && s.contains(x) );
1481 s.delete("from X x");
1482 s.flush();
1483 s.connection().commit();
1484 s.close();
1485
1486 }
1487
1488 public void testLimit() throws Exception {
1489 Session s = openSession();
1490 for ( int i=0; i<10; i++ ) s.save( new Foo() );
1491 Iterator iter = s.createQuery("from Foo foo")
1492 .setMaxResults(4)
1493 .setFirstResult(2)
1494 .iterate();
1495 int count=0;
1496 while ( iter.hasNext() ) {
1497 iter.next();
1498 count++;
1499 }
1500 assertTrue(count==4);
1501 iter = s.createQuery("select distinct foo from Foo foo")
1502 .setMaxResults(2)
1503 .setFirstResult(2)
1504 .list()
1505 .iterator();
1506 count=0;
1507 while ( iter.hasNext() ) {
1508 iter.next();
1509 count++;
1510 }
1511 assertTrue(count==2);
1512 iter = s.createQuery("select distinct foo from Foo foo")
1513 .setMaxResults(3)
1514 .list()
1515 .iterator();
1516 count=0;
1517 while ( iter.hasNext() ) {
1518 iter.next();
1519 count++;
1520 }
1521 assertTrue(count==3);
1522 assertTrue( s.delete("from Foo foo")==10 );
1523 s.flush();
1524 s.connection().commit();
1525 s.close();
1526 }
1527
1528 public void testCustom() throws Exception {
1529 GlarchProxy g = new Glarch();
1530 Multiplicity m = new Multiplicity();
1531 m.count = 12;
1532 m.glarch = (Glarch) g;
1533 g.setMultiple(m);
1534 Session s = openSession();
1535 Serializable gid = s.save(g);
1536 s.flush();
1537 s.connection().commit();
1538 s.close();
1539
1540 s = openSession();
1541 g = (Glarch) s.find("from Glarch g where g.multiple.count=12").get(0);
1542 s.connection().commit();
1543 s.close();
1544
1545 s = openSession();
1546 g = (Glarch) s.find("from Glarch g where g.multiple.glarch=g and g.multiple.count=12").get(0);
1547 assertTrue( g.getMultiple()!=null );
1548 assertEquals( g.getMultiple().count, 12 );
1549 assertSame(g.getMultiple().glarch, g);
1550 s.flush();
1551 s.connection().commit();
1552 s.close();
1553
1554 s = openSession();
1555 g = (GlarchProxy) s.load(Glarch.class, gid);
1556 assertTrue( g.getMultiple()!=null );
1557 assertEquals( g.getMultiple().count, 12 );
1558 assertSame(g.getMultiple().glarch, g);
1559 s.delete(g);
1560 s.flush();
1561 s.connection().commit();
1562 s.close();
1563 }
1564
1565 public void testSaveAddDelete() throws Exception {
1566 Session s = openSession();
1567 Baz baz = new Baz();
1568 Set bars = new HashSet();
1569 baz.setCascadingBars(bars);
1570 s.save(baz);
1571 s.flush();
1572 baz.getCascadingBars().add( new Bar() );
1573 s.delete(baz);
1574 s.flush();
1575 s.connection().commit();
1576 s.close();
1577 }
1578
1579 public void testNamedParams() throws Exception {
1580 Bar bar = new Bar();
1581 Bar bar2 = new Bar();
1582 bar.setName("Bar");
1583 bar2.setName("Bar Two");
1584 bar.setX(10);
1585 bar2.setX(1000);Baz baz = new Baz();
1586 baz.setCascadingBars( new HashSet() );
1587 baz.getCascadingBars().add(bar);
1588 bar.setBaz(baz);
1589 Session s = openSession();
1590 s.save(baz);
1591 s.save(bar2);
1592
1593 List list = s.find("from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'");
1594 Object row = list.iterator().next();
1595 assertTrue( row instanceof Object[] && ( (Object[]) row ).length==3 );
1596
1597 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%'");
1598 list = q.list();
1599 if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
1600
1601 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");
1602 HashSet nameList = new HashSet();
1603 nameList.add("bar");
1604 nameList.add("Bar");
1605 nameList.add("Bar Two");
1606 q.setParameterList("nameList", nameList);
1607 q.setParameter("stringVal", "a string");
1608 list = q.list();
1609 if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
1610
1611 try {
1612 q.setParameterList("nameList", (Collection)null);
1613 fail("Should throw an queryexception when passing a null!");
1614 } catch (QueryException qe) {
1615 //should happen
1616 }
1617
1618
1619 if (
1620 !( getDialect() instanceof Oracle9Dialect) &&
1621 !( getDialect() instanceof MySQLDialect) &&
1622 !( getDialect() instanceof DB2Dialect) &&
1623 !( getDialect() instanceof HSQLDialect) &&
1624 !( getDialect() instanceof SQLServerDialect) &&
1625 !( getDialect() instanceof SybaseDialect) &&
1626 !( getDialect() instanceof PostgreSQLDialect) &&
1627 !( getDialect() instanceof TimesTenDialect)
1628 ) { // oracle barfs on "x in ()"
1629 q.setParameterList("nameList", Collections.EMPTY_LIST);
1630 list = q.list();
1631 assertTrue( list.size()==0 );
1632 }
1633
1634 q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");
1635 Object result = q.uniqueResult();
1636 assertTrue( result!=null );
1637 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");
1638 q.setString("name", "Bar%");
1639 list = q.list();
1640 assertTrue( list.size()==1 );
1641
1642
1643 // This test added for issue HB-297 - there is an named parameter in the Order By clause
1644 q = s.createQuery("select bar from Bar bar order by ((bar.x - :valueX)*(bar.x - :valueX))");
1645 q.setInteger("valueX", bar.getX()+1);
1646 list = q.list();
1647 assertTrue( ((Bar)list.get(0)).getX() == bar.getX());
1648 q.setInteger("valueX", bar2.getX()+1);
1649 list = q.list();
1650 assertTrue( ((Bar)list.get(0)).getX() == bar2.getX());
1651
1652 s.delete(baz);
1653 s.delete(bar2);
1654 s.flush();
1655 s.connection().commit();
1656 s.close();
1657 }
1658
1659 public void testParameterCheck() throws HibernateException {
1660 Session s = openSession();
1661 try {
1662 Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");
1663 q.list();
1664 fail("Should throw QueryException for missing myX");
1665 }
1666 catch (QueryException iae) {
1667 // should happen
1668 }
1669 finally {
1670 s.close();
1671 }
1672
1673 s = openSession();
1674 try {
1675 Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");
1676 q.list();
1677 fail("Should throw QueryException for missing ?");
1678 }
1679 catch (QueryException iae) {
1680 // should happen
1681 }
1682 finally {
1683 s.close();
1684 }
1685
1686 s = openSession();
1687 try {
1688 Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");
1689 q.setInteger(0, 1);
1690 q.list();
1691 }
1692 catch (QueryException iae) {
1693 fail("Should not throw QueryException for missing ?");
1694 }
1695 finally {
1696 s.close();
1697 }
1698
1699 s = openSession();
1700 try {
1701 Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'");
1702 q.list();
1703 }
1704 catch (QueryException iae) {
1705 fail("Should not throw QueryException for ? in quotes");
1706 }
1707 finally {
1708 s.close();
1709 }
1710
1711 s = openSession();
1712 try {
1713 Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");
1714 q.setParameter(0, "bull");
1715 q.setParameter(2, "shit");
1716 q.list();
1717 fail("should throw exception telling me i have not set parameter 1");
1718 }
1719 catch (QueryException iae) {
1720 // should happen!
1721 }
1722 finally {
1723 s.close();
1724 }
1725 }
1726 public void testDyna() throws Exception {
1727 Session s = openSession();
1728 GlarchProxy g = new Glarch();
1729 g.setName("G");
1730 Serializable id = s.save(g);
1731 s.flush();
1732 s.connection().commit();
1733 s.close();
1734
1735 s = openSession();
1736 g = (GlarchProxy) s.load(Glarch.class, id);
1737 assertTrue( g.getName().equals("G") );
1738 assertTrue( g.getDynaBean().get("foo").equals("foo") && g.getDynaBean().get("bar").equals( new Integer(66) ) );
1739 assertTrue( ! (g instanceof Glarch) );
1740 g.getDynaBean().put("foo", "bar");
1741 s.flush();
1742 s.connection().commit();
1743 s.close();
1744
1745 s = openSession();
1746 g = (GlarchProxy) s.load(Glarch.class, id);
1747 assertTrue( g.getDynaBean().get("foo").equals("bar") && g.getDynaBean().get("bar").equals( new Integer(66) ) );
1748 g.setDynaBean(null);
1749 s.flush();
1750 s.connection().commit();
1751 s.close();
1752
1753 s = openSession();
1754 g = (GlarchProxy) s.load(Glarch.class, id);
1755 assertTrue( g.getDynaBean()==null );
1756 s.delete(g);
1757 s.flush();
1758 s.connection().commit();
1759 s.close();
1760 }
1761
1762 public void testFindByCriteria() throws Exception {
1763 if ( getDialect() instanceof DB2Dialect ) return;
1764 Session s = openSession();
1765 Foo f = new Foo();
1766 s.save(f);
1767 s.flush();
1768
1769 if ( ! (getDialect() instanceof HSQLDialect) ) {
1770 List list = s.createCriteria(Foo.class)
1771 .add( Expression.eq( "integer", f.getInteger() ) )
1772 .add( Expression.eqProperty("integer", "integer") )
1773 .add( Expression.like( "string", f.getString().toUpperCase() ).ignoreCase() )
1774 .add( Expression.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) )
1775 .setFetchMode("foo", FetchMode.EAGER)
1776 .setFetchMode("baz", FetchMode.LAZY)
1777 .setFetchMode("abstracts", FetchMode.EAGER)
1778 .list();
1779 assertTrue( list.size()==1 && list.get(0)==f );
1780
1781 list = s.createCriteria(Foo.class).add(
1782 Expression.disjunction()
1783 .add( Expression.eq( "integer", f.getInteger() ) )
1784 .add( Expression.like( "string", f.getString() ) )
1785 .add( Expression.eq( "boolean", f.getBoolean() ) )
1786 )
1787 .add( Expression.isNotNull("boolean") )
1788 .list();
1789 assertTrue( list.size()==1 && list.get(0)==f );
1790 }
1791
1792 Foo example = new Foo();
1793 example.setString("a STRing");
1794 List 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 if (getDialect() instanceof HSQLDialect) {
1860 s = openSession();
1861 s.delete("from Foo foo");
1862 s.flush();
1863 s.connection().commit();
1864 s.close();
1865 return;
1866 }
1867
1868 s = openSession();
1869 list = s.createCriteria(Foo.class)
1870 .add( Expression.eq( "integer", f.getInteger() ) )
1871 .add( Expression.like( "string", f.getString() ) )
1872 .add( Expression.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) )
1873 .add( Expression.isNotNull("foo") )
1874 .setFetchMode("foo", FetchMode.EAGER)
1875 .setFetchMode("baz", FetchMode.LAZY)
1876 .setFetchMode("component.glarch", FetchMode.LAZY)
1877 .setFetchMode("foo.baz", FetchMode.LAZY)
1878 .setFetchMode("foo.component.glarch", FetchMode.LAZY)
1879 .list();
1880 f = (Foo) list.get(0);
1881 assertTrue( Hibernate.isInitialized( f.getFoo() ) );
1882 assertTrue( !Hibernate.isInitialized( f.getComponent().getGlarch() ) );
1883
1884 s.save( new Bar() );
1885 list = s.createCriteria(Bar.class)
1886 .list();
1887 assertTrue( list.size()==1 );
1888 assertTrue( s.createCriteria(Foo.class).list().size()==3 );
1889 s.delete( list.get(0) );
1890
1891 s.delete( f.getFoo() );
1892 s.delete(f);
1893 s.flush();
1894 s.connection().commit();
1895 s.close();
1896 }
1897
1898 public void testAfterDelete() throws Exception {
1899 Session s = openSession();
1900 Foo foo = new Foo();
1901 s.save(foo);
1902 s.flush();
1903 s.delete(foo);
1904 s.save(foo);
1905 s.delete(foo);
1906 s.flush();
1907 s.connection().commit();
1908 s.close();
1909 }
1910
1911 public void testCollectionWhere() throws Exception {
1912 Foo foo1 = new Foo();
1913 Foo foo2 = new Foo();
1914 Baz baz = new Baz();
1915 Foo[] arr = new Foo[10];
1916 arr[0] = foo1;
1917 arr[9] = foo2;
1918 Session s = openSession();
1919 s.save(foo1);
1920 s.save(foo2);
1921 baz.setFooArray(arr);
1922 s.save(baz);
1923 s.flush();
1924 s.connection().commit();
1925 s.close();
1926
1927 s = openSession();
1928 baz = (Baz) s.load( Baz.class, baz.getCode() );
1929 assertTrue( baz.getFooArray().length==1 );
1930 assertTrue( s.find("from Baz baz join baz.fooArray foo").size()==1 );
1931 assertTrue( s.find("from Foo foo").size()==2 );
1932 assertTrue( s.filter( baz.getFooArray(), "" ).size()==1 );
1933 //assertTrue( s.delete("from java.lang.Object o")==9 );
1934 s.delete("from Foo foo");
1935 String bazid = baz.getCode();
1936 s.delete(baz);
1937 int rows=s.connection().createStatement().executeUpdate(
1938 "delete from fooArray where id_='" + bazid + "' and i>=8"
1939 );
1940 assertTrue(rows==1);
1941 s.flush();
1942 s.connection().commit();
1943 s.close();
1944 }
1945
1946 public void testComponentParent() throws Exception {
1947 Session s = openSession();
1948 Transaction t = s.beginTransaction();
1949 BarProxy bar = new Bar();
1950 bar.setBarComponent( new FooComponent() );
1951 Baz baz = new Baz();
1952 baz.setComponents( new FooComponent[] { new FooComponent(), new FooComponent() } );
1953 s.save(bar);
1954 s.save(baz);
1955 t.commit();
1956 s.close();
1957 s = openSession();
1958 t = s.beginTransaction();
1959 bar = (BarProxy) s.load(Bar.class, bar.getKey());
1960 s.load(baz, baz.getCode());
1961 assertTrue( bar.getBarComponent().getParent()==bar );
1962 assertTrue( baz.getComponents()[0].getBaz()==baz && baz.getComponents()[1].getBaz()==baz );
1963 s.delete(baz);
1964 s.delete(bar);
1965 t.commit();
1966 s.close();
1967 }
1968
1969 public void testCollectionCache() throws Exception {
1970 Session s = openSession();
1971 Baz baz = new Baz();
1972 baz.setDefaults();
1973 s.save(baz);
1974 s.flush();
1975 s.connection().commit();
1976 s.close();
1977
1978 s = openSession();
1979 s.load( Baz.class, baz.getCode() );
1980 s.flush();
1981 s.connection().commit();
1982 s.close();
1983
1984 s = openSession();
1985 baz = (Baz) s.load( Baz.class, baz.getCode() );
1986 s.delete(baz);
1987 s.flush();
1988 s.connection().commit();
1989 s.close();
1990 }
1991
1992 public void ntestAssociationId() throws Exception {
1993 Session s = openSession();
1994 Transaction t = s.beginTransaction();
1995 Bar bar = new Bar();
1996 String id = (String) s.save(bar);
1997 MoreStuff more = new MoreStuff();
1998 more.setName("More Stuff");
1999 more.setIntId(12);
2000 more.setStringId("id");
2001 Stuff stuf = new Stuff();
2002 stuf.setMoreStuff(more);
2003 more.setStuffs( new ArrayList() );
2004 more.getStuffs().add(stuf);
2005 stuf.setFoo(bar);
2006 stuf.setId(1234);
2007 stuf.setProperty( TimeZone.getDefault() );
2008 s.save(more);
2009 t.commit();
2010 s.close();
2011
2012 s = openSession();
2013 t = s.beginTransaction();
2014 assertTrue( s.find(
2015 "from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?",
2016 new Object[] { bar, new Long(1234), new Integer(12), "id" },
2017 new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING }
2018 ).size()==1 );
2019 assertTrue( s.find(
2020 "from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?",
2021 new Object[] { bar, new Long(1234), "More Stuff" },
2022 new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING }
2023 ).size()==1 );
2024 s.find("from Stuff as s where s.foo.string is not null");
2025 assertTrue(
2026 s.find("from Stuff as s where s.foo > '0' order by s.foo").size()==1
2027 );
2028 //s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list();
2029 t.commit();
2030 s.close();
2031
2032 s = openSession();
2033 t = s.beginTransaction();
2034 FooProxy foo = (FooProxy) s.load(Foo.class, id);
2035 s.load(more, more);
2036 t.commit();
2037 s.close();
2038
2039 s = openSession();
2040 t = s.beginTransaction();
2041 Stuff stuff = new Stuff();
2042 stuff.setFoo(foo);
2043 stuff.setId(1234);
2044 stuff.setMoreStuff(more);
2045 s.load(stuff, stuff);
2046 assertTrue( stuff.getProperty().equals( TimeZone.getDefault() ) );
2047 assertTrue( stuff.getMoreStuff().getName().equals("More Stuff") );
2048 s.delete("from MoreStuff");
2049 s.delete("from Foo foo");
2050 t.commit();
2051 s.close();
2052 }
2053
2054 public void testCascadeSave() throws Exception {
2055 Session s = openSession();
2056 Transaction t = s.beginTransaction();
2057 Baz baz = new Baz();
2058 List list = new ArrayList();
2059 list.add( new Fee() );
2060 list.add( new Fee() );
2061 baz.setFees(list);
2062 s.save(baz);
2063 t.commit();
2064 s.close();
2065
2066 s = openSession();
2067 t = s.beginTransaction();
2068 baz = (Baz) s.load( Baz.class, baz.getCode() );
2069 assertTrue( baz.getFees().size()==2 );
2070 s.delete(baz);
2071 assertTrue( !s.iterate("from Fee fee").hasNext() );
2072 t.commit();
2073 s.close();
2074
2075 }
2076
2077 public void testCollectionsInSelect() throws Exception {
2078 Session s = openSession();
2079 Transaction t = s.beginTransaction();
2080 Foo[] foos = new Foo[] { null, new Foo() };
2081 s.save( foos[1] );
2082 Baz baz = new Baz();
2083 baz.setDefaults();
2084 baz.setFooArray(foos);
2085 s.save(baz);
2086 Baz baz2 = new Baz();
2087 baz2.setDefaults();
2088 s.save(baz2);
2089
2090 Bar bar = new Bar();
2091 bar.setBaz(baz);
2092 s.save(bar);
2093
2094 List list = s.find("select new Result(foo.string, foo.long, foo.integer) from Foo foo");
2095 assertTrue( list.size()==2 && ( list.get(0) instanceof Result ) && ( list.get(1) instanceof Result ) );
2096 /*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");
2097 assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
2098 Result r = ((Result) list.get(0) );
2099 assertEquals( r.getName(), baz.getName() );
2100 assertEquals( r.getCount(), 1 );
2101 assertEquals( r.getAmount(), foos[1].getLong().longValue() );*/
2102 list = s.find("select new Result( baz.name, max(foo.long), count(foo) ) from Baz baz join baz.fooArray foo group by baz.name");
2103 assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
2104 Result r = ((Result) list.get(0) );
2105 assertEquals( r.getName(), baz.getName() );
2106 assertEquals( r.getCount(), 1 );
2107 assertTrue( r.getAmount() > 696969696969696000l );
2108
2109
2110 //s.find("select max( elements(bar.baz.fooArray) ) from Bar as bar");
2111 //The following test is disabled for databases with no subselects...also for Interbase (not sure why).
2112 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) /*&& !(dialect instanceof MckoiDialect)*/ && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) ) {
2113 s.find("select count(*) from Baz as baz where 1 in indices(baz.fooArray)");
2114 s.find("select count(*) from Bar as bar where 'abc' in elements(bar.baz.fooArray)");
2115 s.find("select count(*) from Bar as bar where 1 in indices(bar.baz.fooArray)");
2116 if ( !(getDialect() instanceof DB2Dialect) && !(getDialect() instanceof Oracle9Dialect) ) {
2117 s.find("select count(*) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)");
2118 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)");
2119 }
2120 s.find("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')");
2121 s.find("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')");
2122 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)");
2123 }
2124
2125 list = s.find("from Baz baz left join baz.fooToGlarch join fetch baz.fooArray foo left join fetch foo.foo");
2126 assertTrue( list.size()==1 && ( (Object[]) list.get(0) ).length==2 );
2127
2128 s.find("select baz.name from Bar bar inner join bar.baz baz inner join baz.fooSet foo where baz.name = bar.string");
2129 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");
2130
2131 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");
2132
2133 s.find("select baz.name from Bar bar join bar.baz baz join baz.fooSet foo where baz.name = bar.string");
2134 s.find("SELECT baz.name FROM Bar AS bar JOIN bar.baz AS baz JOIN baz.fooSet AS foo WHERE baz.name = bar.string");
2135
2136 if ( !( getDialect() instanceof HSQLDialect ) ) {
2137 s.find("select baz.name from Bar bar left join bar.baz baz left join baz.fooSet foo where baz.name = bar.string");
2138 s.find("select foo.string from Bar bar left join bar.baz.fooSet foo where bar.string = foo.string");
2139 }
2140
2141 s.find("select baz.name from Bar bar left join bar.baz baz left join baz.fooArray foo where baz.name = bar.string");
2142 s.find("select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string");
2143
2144 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'");
2145 s.find("select foo from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo");
2146 s.find("select foo from Bar bar inner join bar.baz.fooSet as foo");
2147
2148 s.find("select bar.string, foo.string from Bar bar join bar.baz as baz join baz.fooSet as foo where baz.name = 'name'");
2149 s.find("select foo from Bar bar join bar.baz as baz join baz.fooSet as foo");
2150 s.find("select foo from Bar bar join bar.baz.fooSet as foo");
2151
2152 assertTrue( s.find("from Bar bar join bar.baz.fooArray foo").size()==1 );
2153
2154 assertTrue( s.find("from Bar bar join bar.baz.fooSet foo").size()==0 );
2155 assertTrue( s.find("from Bar bar join bar.baz.fooArray foo").size()==1 );
2156
2157 s.delete(bar);
2158
2159 if ( getDialect() instanceof DB2Dialect || getDialect() instanceof PostgreSQLDialect ) {
2160 s.iterate("select one from One one join one.manies many group by one order by count(many)");
2161 s.iterate("select one from One one join one.manies many group by one having count(many) < 5");
2162 }
2163
2164 s.find("from One one join one.manies many where one.id = 1 and many.id = 1");
2165 s.iterate("select one.id, elements(one.manies) from One one");
2166 s.iterate("select max( elements(one.manies) ) from One one");
2167 s.find("select one, elements(one.manies) from One one");
2168 //s.iterate("select one, max( elements(one.manies) ) from One one group by one");
2169 Iterator iter = s.iterate("select elements(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2170 //WAS: assertTrue( iter.next()==null && iter.next()==foos[1] && !iter.hasNext() );
2171 assertTrue( iter.next()==foos[1] && !iter.hasNext() );
2172 list = s.find("select elements(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2173 //WAS: assertTrue( list.size()==2 );
2174 assertTrue( list.size()==1 );
2175 iter = s.iterate("select indices(baz.fooArray) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING);
2176 //WAS: assertTrue( iter.next().equals( new Integer(0) ) && iter.next().equals( new Integer(1) ) && !iter.hasNext() );
2177 assertTrue( iter.next().equals( new Integer(1) ) && !iter.hasNext() );
2178
2179 //assertTrue( s.iterate("select max( elements(baz.timeArray) ) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next() instanceof Time );
2180 //assertTrue( s.iterate("select max( elements(baz.stringSet) ) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next().equals("foo") );
2181 assertTrue( s.iterate("select size(baz.stringSet) from Baz baz where baz.id=?", baz.getCode(), Hibernate.STRING).next().equals( new Integer(3) ) );
2182 //s.find("from One one where sum one.manies.elements =0 or 1 = min one.manies.elements");
2183
2184 s.find("from Foo foo where foo.component.glarch.id is not null");
2185
2186 //iter = s.iterate("select baz, max( elements(baz.timeArray) ) from Baz baz group by baz");
2187 //while ( iter.hasNext() ) { Object[] arr = (Object[]) iter.next(); System.out.println( arr[0] + " " + arr[1] ); }
2188 iter = s.iterate("select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from Baz baz group by baz");
2189 while ( iter.hasNext() ) { Object[] arr = (Object[]) iter.next(); System.out.println( arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3] ); }
2190
2191 s.delete(baz);
2192 s.delete(baz2);
2193 s.delete( foos[1] );
2194 t.commit();
2195 s.close();
2196 }
2197
2198 public void testNewFlushing() throws Exception {
2199 Session s = openSession();
2200 Baz baz = new Baz();
2201 baz.setDefaults();
2202 s.save(baz);
2203 s.flush();
2204 baz.getStringArray()[0] = "a new value";
2205 Iterator iter = s.iterate("from Baz baz");//no flush
2206 assertTrue( iter.next()==baz );
2207 iter = s.iterate("select elements(baz.stringArray) from Baz baz");
2208 boolean found = false;
2209 while ( iter.hasNext() ) {
2210 if ( iter.next().equals("a new value") ) found = true;
2211 }
2212 assertTrue(found);
2213 baz.setStringArray(null);
2214 s.iterate("from Baz baz"); //no flush
2215 iter = s.iterate("select elements(baz.stringArray) from Baz baz");
2216 assertTrue( !iter.hasNext() );
2217 baz.getStringList().add("1E1");
2218 iter = s.iterate("from Foo foo");//no flush
2219 assertTrue( !iter.hasNext() );
2220 iter = s.iterate("select elements(baz.stringList) from Baz baz");
2221 found = false;
2222 while ( iter.hasNext() ) {
2223 if ( iter.next().equals("1E1") ) found = true;
2224 }
2225 assertTrue(found);
2226 baz.getStringList().remove("1E1");
2227 iter = s.iterate("select elements(baz.stringArray) from Baz baz"); //no flush
2228 iter = s.iterate("select elements(baz.stringList) from Baz baz");
2229 found = false;
2230 while ( iter.hasNext() ) {
2231 if ( iter.next().equals("1E1") ) found = true;
2232 }
2233 assertTrue(!found);
2234
2235 List newList = new ArrayList();
2236 newList.add("value");
2237 baz.setStringList(newList);
2238 iter = s.iterate("from Foo foo");//no flush
2239 baz.setStringList(null);
2240 iter = s.iterate("select elements(baz.stringList) from Baz baz");
2241 assertTrue( !iter.hasNext() );
2242
2243 baz.setStringList(newList);
2244 iter = s.iterate("from Foo foo");//no flush
2245 iter = s.iterate("select elements(baz.stringList) from Baz baz");
2246 assertTrue( iter.hasNext() );
2247
2248 s.delete(baz);
2249 s.flush();
2250 s.connection().commit();
2251 s.close();
2252 }
2253
2254 public void testPersistCollections() throws Exception {
2255
2256 Session s = openSession();
2257 assertTrue( ( (Integer) s.iterate("select count(*) from Bar").next() ).intValue()==0 );
2258 assertTrue( s.iterate("select count(*) from Bar b").next().equals( new Integer(0) ) );
2259 assertFalse( s.iterate("from Glarch g").hasNext() );
2260
2261 Baz baz = new Baz();
2262 s.save(baz);
2263 baz.setDefaults();
2264 baz.setStringArray( new String[] { "stuff" } );
2265 Set bars = new HashSet();
2266 bars.add( new Bar() );
2267 baz.setCascadingBars(bars);
2268 HashMap sgm = new HashMap();
2269 sgm.put( "a", new Glarch() );
2270 sgm.put( "b", new Glarch() );
2271 baz.setStringGlarchMap(sgm);
2272 //System.out.println( s.print(baz) );
2273 s.flush();
2274 s.connection().commit();
2275 s.close();
2276
2277 s = openSession();
2278 assertTrue( ( (Integer) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2279 baz = (Baz) ( (Object[]) s.find("select baz, baz from Baz baz").get(0) )[1];
2280 assertTrue( baz.getCascadingBars().size()==1 );
2281 //System.out.println( s.print(baz) );
2282 Foo foo = new Foo();
2283 s.save(foo);
2284 Foo foo2 = new Foo() ;
2285 s.save(foo2);
2286 baz.setFooArray( new Foo[] { foo, foo, null, foo2 } );
2287 baz.getFooSet().add(foo);
2288 baz.getCustoms().add( new String[] { "new", "custom" } );
2289 baz.setStringArray(null);
2290 baz.getStringList().set(0, "new value");
2291 baz.setStringSet( new TreeSet() );
2292 Time time = new java.sql.Time(12345);
2293 baz.getTimeArray()[2] = time;
2294 //System.out.println(time);
2295
2296 assertTrue( baz.getStringGlarchMap().size()==1 );
2297
2298 //The following test is disabled databases with no subselects
2299 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect) ) {
2300 List 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)");
2301 assertTrue( "collection.elements find", list.size()==2 );
2302 }
2303 if (!(getDialect() instanceof SAPDBDialect) ) { // SAPDB doesn't like distinct with binary type
2304 List list = s.find("select distinct foo from Baz baz join baz.fooArray foo");
2305 assertTrue( "collection.elements find", list.size()==2 );
2306 }
2307
2308 List list = s.find("select foo from Baz baz join baz.fooSet foo");
2309 assertTrue( "association.elements find", list.size()==1 );
2310 s.flush();
2311 s.connection().commit();
2312 s.close();
2313
2314 s = openSession();
2315 assertTrue( ( (Integer) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2316 baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2317 assertTrue( "collection of custom types - added element", baz.getCustoms().size()==4 && baz.getCustoms().get(0)!=null );
2318 assertTrue ( "component of component in collection", baz.getComponents()[1].getSubcomponent()!=null );
2319 assertTrue( baz.getComponents()[1].getBaz()==baz );
2320 assertTrue( "set of objects", ( (FooProxy) baz.getFooSet().iterator().next() ).getKey().equals( foo.getKey() ));
2321 assertTrue( "collection removed", baz.getStringArray().length==0 );
2322 assertTrue( "changed element", baz.getStringList().get(0).equals("new value"));
2323 assertTrue( "replaced set", baz.getStringSet().size()==0 );
2324 assertTrue( "array element change", baz.getTimeArray()[2]!=null );
2325 assertTrue( baz.getCascadingBars().size()==1 );
2326 //System.out.println( s.print(baz) );
2327 baz.getStringSet().add("two");
2328 baz.getStringSet().add("one");
2329 baz.getBag().add("three");
2330 s.flush();
2331 s.connection().commit();
2332 s.close();
2333 s = openSession();
2334 baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2335 assertTrue( baz.getStringSet().size()==2 );
2336 assertTrue( baz.getStringSet().first().equals("one") );
2337 assertTrue( baz.getStringSet().last().equals("two") );
2338 assertTrue( baz.getBag().size()==5 );
2339 baz.getStringSet().remove("two");
2340 baz.getBag().remove("duplicate");
2341 s.flush();
2342 s.connection().commit();
2343 s.close();
2344
2345 s = openSession();
2346 assertTrue( ( (Integer) s.iterate("select count(*) from Bar").next() ).intValue()==1 );
2347 baz = (Baz) s.load(Baz.class, baz.getCode());
2348 assertTrue( baz.getCascadingBars().size()==1 );
2349 Bar bar = new Bar();
2350 Bar bar2 = new Bar();
2351 s.save(bar); s.save(bar2);
2352 baz.setTopFoos( new HashSet() );
2353 baz.getTopFoos().add(bar);
2354 baz.getTopFoos().add(bar2);
2355 assertTrue( baz.getCascadingBars().size()==1 );
2356 baz.setTopGlarchez( new TreeMap() );
2357 GlarchProxy g = new Glarch();
2358 s.save(g);
2359 baz.getTopGlarchez().put( new Character('G'), g );
2360 HashMap map = new HashMap();
2361 map.put(bar, g);
2362 map.put(bar2, g);
2363 baz.setFooToGlarch(map);
2364 map = new HashMap();
2365 map.put( new FooComponent("name", 123, null, null), bar );
2366 map.put( new FooComponent("nameName", 12, null, null), bar );
2367 baz.setFooComponentToFoo(map);
2368 map = new HashMap();
2369 map.put(bar, g);
2370 baz.setGlarchToFoo(map);
2371 s.flush();
2372 s.connection().commit();
2373 s.close();
2374
2375 s = openSession();
2376 baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2377 assertTrue( baz.getCascadingBars().size()==1 );
2378
2379 Session s2 = openSession();
2380 assertTrue( ( (Integer) s2.iterate("select count(*) from Bar").next() ).intValue()==3 );
2381 Baz baz2 = (Baz) s2.find("select baz from Baz baz order by baz").get(0);
2382 Object o = baz2.getFooComponentToFoo().get( new FooComponent("name", 123, null, null) );
2383 assertTrue(
2384 o==baz2.getFooComponentToFoo().get( new FooComponent("nameName", 12, null, null) ) && o!=null
2385 );
2386 s2.connection().commit();
2387 s2.close();
2388
2389 assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) );
2390 assertTrue( baz.getTopFoos().size()==2 );
2391 assertTrue( baz.getTopGlarchez().size()==1 );
2392 assertTrue( baz.getTopFoos().iterator().next()!=null );
2393 assertTrue( baz.getStringSet().size()==1 );
2394 assertTrue( baz.getBag().size()==4 );
2395 assertTrue( baz.getFooToGlarch().size()==2 );
2396 assertTrue( baz.getFooComponentToFoo().size()==2 );
2397 assertTrue( baz.getGlarchToFoo().size()==1 );
2398 Iterator iter = baz.getFooToGlarch().keySet().iterator();
2399 for (int i=0; i<2; i++ ) assertTrue( iter.next() instanceof BarProxy );
2400 FooComponent fooComp = (FooComponent) baz.getFooComponentToFoo().keySet().iterator().next();
2401 assertTrue(
2402 ( (fooComp.getCount()==123 && fooComp.getName().equals("name"))
2403 || (fooComp.getCount()==12 && fooComp.getName().equals("nameName")) )
2404 && ( baz.getFooComponentToFoo().get(fooComp) instanceof BarProxy )
2405 );
2406 Glarch g2 = new Glarch();
2407 s.save(g2);
2408 g = (GlarchProxy) baz.getTopGlarchez().get( new Character('G') );
2409 baz.getTopGlarchez().put( new Character('H'), g );
2410 baz.getTopGlarchez().put( new Character('G'), g2 );
2411 s.flush();
2412 s.connection().commit();
2413 s.close();
2414
2415 s = openSession();
2416 baz = (Baz) s.load(Baz.class, baz.getCode());
2417 assertTrue( baz.getTopGlarchez().size()==2 );
2418 assertTrue( baz.getCascadingBars().size()==1 );
2419 s.connection().commit();
2420 s.close();
2421
2422 s = openSession();
2423 assertTrue( ( (Integer) s.iterate("select count(*) from Bar").next() ).intValue()==3 );
2424 baz = (Baz) s.find("select baz from Baz baz order by baz").get(0);
2425 assertTrue( baz.getTopGlarchez().size()==2 );
2426 assertTrue( baz.getCascadingBars().size()==1 );
2427
2428 s.connection().commit();
2429
2430 s.disconnect();
2431
2432 s2 = (Session) SerializationHelper.deserialize( SerializationHelper.serialize(s) );
2433 s.close();
2434
2435 s2.reconnect();
2436 baz = (Baz) s2.load(Baz.class, baz.getCode());
2437 assertTrue( ( (Integer) s2.iterate("select count(*) from Bar").next() ).intValue()==3 );
2438 s2.delete(baz);
2439 s2.delete( baz.getTopGlarchez().get( new Character('G') ) );
2440 s2.delete( baz.getTopGlarchez().get( new Character('H') ) );
2441 int rows = s2.connection().createStatement().executeUpdate("update " + getDialect().openQuote() + "glarchez" + getDialect().closeQuote() + " set baz_map_id=null where baz_map_index='a'");
2442 assertTrue(rows==1);
2443 assertTrue( s2.delete("from Bar bar")==2 );
2444 FooProxy[] arr = baz.getFooArray();
2445 assertTrue( "new array of objects", arr.length==4 && arr[1].getKey().equals( foo.getKey() ) );
2446 for ( int i=1; i<arr.length; i++ ) {
2447 if ( arr[i]!=null) s2.delete(arr[i]);
2448 }
2449
2450 s2.load( Qux.class, new Long(666) ); //nonexistent
2451
2452 assertTrue( s2.delete("from Glarch g")==1 );
2453
2454 s2.flush();
2455 s2.connection().commit();
2456
2457 s2.disconnect();
2458
2459 Session s3 = (Session) SerializationHelper.deserialize( SerializationHelper.serialize(s2) );
2460 s2.close();
2461 //s3.reconnect();
2462 assertTrue( s3.load( Qux.class, new Long(666) )!=null ); //nonexistent
2463 //s3.disconnect();
2464 s3.close();
2465 }
2466
2467 public void testSaveFlush() throws Exception {
2468 Session s = openSession();
2469 Fee fee = new Fee();
2470 s.save( fee, "key" );
2471 fee.setFi("blah");
2472 s.flush();
2473 s.connection().commit();
2474 s.close();
2475 s = openSession();
2476 fee = (Fee) s.load( Fee.class, fee.getKey() );
2477 assertTrue( "blah".equals( fee.getFi() ) );
2478 assertTrue( "key".equals( fee.getKey() ) );
2479 s.delete(fee);
2480 s.flush();
2481 s.connection().commit();
2482 s.close();
2483
2484 }
2485
2486 public void testCreateUpdate() throws Exception {
2487 Session s = openSession();
2488 Foo foo = new Foo();
2489 s.save(foo);
2490 foo.setString("dirty");
2491 s.flush();
2492 s.connection().commit();
2493 s.close();
2494 s = openSession();
2495 Foo foo2 = new Foo();
2496 s.load( foo2, foo.getKey() );
2497 // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2498 assertTrue( "create-update", foo.equalsFoo(foo2) );
2499 //System.out.println( s.print(foo2) );
2500 s.delete(foo2);
2501 s.flush();
2502 s.connection().commit();
2503 s.close();
2504
2505 s = openSession();
2506 foo = new Foo();
2507 s.save(foo, "assignedid");
2508 foo.setString("dirty");
2509 s.flush();
2510 s.connection().commit();
2511 s.close();
2512 s = openSession();
2513 s.load(foo2, "assignedid");
2514 // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2515 assertTrue( "create-update", foo.equalsFoo(foo2) );
2516 //System.out.println( s.print(foo2) );
2517 s.delete(foo2);
2518 s.flush();
2519 s.connection().commit();
2520 s.close();
2521 }
2522
2523 public void testUpdateCollections() throws Exception {
2524 Session s = openSession();
2525 Holder baz = new Holder();
2526 baz.setName("123");
2527 Foo f1 = new Foo();
2528 Foo f2 = new Foo();
2529 Foo f3 = new Foo();
2530 One o = new One();
2531 baz.setOnes( new ArrayList() );
2532 baz.getOnes().add(o);
2533 Foo[] foos = new Foo[] { f1, null, f2 };
2534 baz.setFooArray(foos);
2535 baz.setFoos( new HashSet() );
2536 baz.getFoos().add(f1);
2537 s.save(f1);
2538 s.save(f2);
2539 s.save(f3);
2540 s.save(o);
2541 s.save(baz);
2542 s.flush();
2543 s.connection().commit();
2544 s.close();
2545
2546 baz.getOnes().set(0, null);
2547 baz.getOnes().add(o);
2548 baz.getFoos().add(f2);
2549 foos[0] = f3;
2550 foos[1] = f1;
2551
2552 s = openSession();
2553 s.saveOrUpdate(baz);
2554 s.flush();
2555 s.connection().commit();
2556 s.close();
2557
2558 s = openSession();
2559 Holder h = (Holder) s.load(Holder.class, baz.getId());
2560 assertTrue( h.getOnes().get(0)==null );
2561 assertTrue( h.getOnes().get(1)!=null );
2562 assertTrue( h.getFooArray()[0]!=null);
2563 assertTrue( h.getFooArray()[1]!=null);
2564 assertTrue( h.getFooArray()[2]!=null);
2565 assertTrue( h.getFoos().size()==2 );
2566 s.connection().commit();
2567 s.close();
2568
2569 baz.getFoos().remove(f1);
2570 baz.getFoos().remove(f2);
2571 baz.getFooArray()[0]=null;
2572 baz.getFooArray()[0]=null;
2573 baz.getFooArray()[0]=null;
2574 s = openSession();
2575 s.saveOrUpdate(baz);
2576 s.delete("from Foo");
2577 baz.getOnes().remove(o);
2578 s.delete("from One");
2579 s.delete(baz);
2580 s.flush();
2581 s.connection().commit();
2582 s.close();
2583
2584 }
2585
2586 public void testCreate() throws Exception {
2587 Session s = openSession();
2588 Foo foo = new Foo();
2589 s.save(foo);
2590 s.flush();
2591 s.connection().commit();
2592 s.close();
2593 s = openSession();
2594 Foo foo2 = new Foo();
2595 s.load( foo2, foo.getKey() );
2596 // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2597 assertTrue( "create", foo.equalsFoo(foo2) );
2598 s.delete(foo2);
2599 s.flush();
2600 s.connection().commit();
2601 s.close();
2602 }
2603
2604 public void testCallback() throws Exception {
2605 Session s = openSession();
2606 Qux q = new Qux("0");
2607 s.save(q);
2608 q.setChild( new Qux("1") );
2609 s.save( q.getChild() );
2610 Qux q2 = new Qux("2");
2611 q2.setChild( q.getChild() );
2612 Qux q3 = new Qux("3");
2613 q.getChild().setChild(q3);
2614 s.save(q3);
2615 Qux q4 = new Qux("4");
2616 q4.setChild(q3);
2617 s.save(q4);
2618 s.save(q2);
2619 s.flush();
2620 s.connection().commit();
2621 s.close();
2622 s = openSession();
2623 List l = s.find("from Qux");
2624 assertTrue( "", l.size()==5);
2625 s.delete( l.get(0) );
2626 s.delete( l.get(1) );
2627 s.delete( l.get(2) );
2628 s.delete( l.get(3) );
2629 s.delete( l.get(4) );
2630 s.flush();
2631 s.connection().commit();
2632 s.close();
2633 }
2634
2635 public void testPolymorphism() throws Exception {
2636 Session s = openSession();
2637 Bar bar = new Bar();
2638 s.save(bar);
2639 bar.setBarString("bar bar");
2640 s.flush();
2641 s.connection().commit();
2642 s.close();
2643 s = openSession();
2644 FooProxy foo = (FooProxy) s.load( Foo.class, bar.getKey() );
2645 assertTrue( "polymorphic", foo instanceof BarProxy );
2646 assertTrue( "subclass property", ( (BarProxy) foo ).getBarString().equals( bar.getBarString() ) );
2647 //System.out.println( s.print(foo) );
2648 s.delete(foo);
2649 s.flush();
2650 s.connection().commit();
2651 s.close();
2652 }
2653
2654 public void testRemoveContains() throws Exception {
2655 Session s = openSession();
2656 Baz baz = new Baz();
2657 baz.setDefaults();
2658 s.save(baz);
2659 s.flush();
2660 assertTrue( s.contains(baz) );
2661 s.evict(baz);
2662 assertFalse( s.contains(baz) );
2663 Baz baz2 = (Baz) s.load( Baz.class, baz.getCode() );
2664 assertFalse(baz==baz2);
2665 s.delete(baz2);
2666 s.flush();
2667 s.connection().commit();
2668 s.close();
2669 }
2670
2671 public void testCollectionOfSelf() throws Exception {
2672
2673 if (getDialect() instanceof HSQLDialect) return; //Why??
2674
2675 Session s = openSession();
2676 Bar bar = new Bar();
2677 s.save(bar);
2678 bar.setAbstracts( new HashSet() );
2679 bar.getAbstracts().add(bar);
2680 Bar bar2 = new Bar();
2681 bar.getAbstracts().add(bar2);
2682 bar.setFoo(bar);
2683 s.save(bar2);
2684 s.flush();
2685 s.connection().commit();
2686 s.close();
2687 bar.setAbstracts(null);
2688 s = openSession();
2689 s.load( bar, bar.getKey() );
2690 assertTrue( "collection contains self", bar.getAbstracts().size()==2 && bar.getAbstracts().contains(bar) );
2691 assertTrue( "association to self", bar.getFoo()==bar );
2692 Iterator iter = bar.getAbstracts().iterator();
2693 while ( iter.hasNext() ) {
2694 s.delete( iter.next() );
2695 }
2696 s.flush();
2697 s.connection().commit();
2698 s.close();
2699 }
2700
2701 public void testFind() throws Exception {
2702 Session s = openSession();
2703
2704 Bar bar = new Bar();
2705 s.save(bar);
2706 bar.setBarString("bar bar");
2707 bar.setString("xxx");
2708 Foo foo = new Foo();
2709 s.save(foo);
2710 foo.setString("foo bar");
2711 s.save( new Foo() );
2712 s.save( new Bar() );
2713 List list1 = s.find("select foo from Foo foo where foo.string='foo bar'");
2714 assertTrue( "find size", list1.size()==1 );
2715 assertTrue( "find ==", list1.get(0)==foo );
2716 List list2 = s.find("from Foo foo order by foo.string, foo.date");
2717 assertTrue( "find size", list2.size()==4 );
2718
2719 list1 = s.find("from Foo foo where foo.class='B'");
2720 assertTrue( "class special property", list1.size()==2);
2721 list1 = s.find("from Foo foo where foo.class=Bar");
2722 assertTrue( "class special property", list1.size()==2);
2723 list1 = s.find("from Foo foo where foo.class=Bar");
2724 list2 = s.find("select bar from Bar bar, Foo foo where bar.string = foo.string and not bar=foo");
2725 assertTrue( "class special property", list1.size()==2);
2726 assertTrue( "select from a subclass", list2.size()==1);
2727 Trivial t = new Trivial();
2728 s.save(t);
2729 s.flush();
2730 s.connection().commit();
2731 s.close();
2732
2733 s = openSession();
2734 list1 = s.find("from Foo foo where foo.string='foo bar'");
2735 assertTrue( "find size", list1.size()==1 );
2736 // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
2737 assertTrue( "find equals", ( (Foo) list1.get(0) ).equalsFoo(foo) );
2738 list2 = s.find("select foo from Foo foo");
2739 assertTrue( "find size", list2.size()==5 );
2740 List list3 = s.find("from Bar bar where bar.barString='bar bar'");
2741 assertTrue( "find size", list3.size()==1 );
2742 assertTrue( "find same instance", list2.contains( list1.get(0) ) && list2.contains( list2.get(0) ) );
2743 assertTrue( s.find("from Trivial").size()==1 );
2744 s.delete("from Trivial");
2745
2746 list2 = s.find("from Foo foo where foo.date = ?", new java.sql.Date(123), Hibernate.DATE);
2747 assertTrue ( "find by date", list2.size()==4 );
2748 Iterator iter = list2.iterator();
2749 while ( iter.hasNext() ) {
2750 s.delete( iter.next() );
2751 }
2752 list2 = s.find("from Foo foo");
2753 assertTrue( "find deleted", list2.size()==0);
2754 s.flush();
2755 s.connection().commit();
2756 s.close();
2757 }
2758
2759 public void testDeleteRecursive() throws Exception {
2760 Session s = openSession();
2761 Foo x = new Foo();
2762 Foo y = new Foo();
2763 x.setFoo(y);
2764 y.setFoo(x);
2765 s.save(x);
2766 s.save(y);
2767 s.flush();
2768 s.delete(y);
2769 s.delete(x);
2770 s.flush();
2771 s.connection().commit();
2772 s.close();
2773 }
2774
2775 /*public void testSubcollections() throws Exception {
2776 Session s = sessionsopenSession();
2777 Baz baz = new Baz();
2778 s.save(baz);
2779 baz.setDefaults();
2780 s.flush();
2781 s.connection().commit();
2782 s.close();
2783 s = sessionsopenSession();
2784 baz = (Baz) s.load( Baz.class, baz.getCode() );
2785 Set[] setArray = baz.getSetArray();
2786 baz.setSetArray(null);
2787 baz.setAnotherSetArray(setArray);
2788 baz.setAnotherSetList( baz.getSetList() );
2789 baz.setSetList(null);
2790 s.flush();
2791 s.connection().commit();
2792 s.close();
2793 s = sessionsopenSession();
2794 baz = (Baz) s.load( Baz.class, baz.getCode() );
2795 assertTrue( baz.getAnotherSetArray().length==2 && baz.getAnotherSetArray()[0]!=null, "subcollection moved property");
2796 assertTrue( baz.getSetArray()==null, "subcollection moved property");
2797 assertTrue( baz.getAnotherSetList().size()==4 && baz.getAnotherSetList().get(2)!=null, "subcollection moved role");
2798 assertTrue( baz.getSetList()==null, "subcollection moved role");
2799 s.delete(baz);
2800 s.flush();
2801 s.connection().commit();
2802 s.close();
2803 }*/
2804
2805
2806 public void testReachability() throws Exception {
2807 //first for unkeyed collections
2808 Session s = openSession();
2809 Baz baz1 = new Baz();
2810 s.save(baz1);
2811 Baz baz2 = new Baz();
2812 s.save(baz2);
2813 baz1.setIntArray( new int[] {1 ,2, 3, 4} );
2814 baz1.setFooSet( new HashSet() );
2815 Foo foo = new Foo();
2816 s.save(foo);
2817 baz1.getFooSet().add(foo);
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 baz2.setFooSet( baz1.getFooSet() ); baz1.setFooSet(null);
2826 baz2.setIntArray( baz1.getIntArray() ); baz1.setIntArray(null);
2827 s.flush();
2828 s.connection().commit();
2829 s.close();
2830
2831 s = openSession();
2832 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2833 baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2834 assertTrue( "unkeyed reachability", baz2.getIntArray().length==4 );
2835 assertTrue( "unkeyed reachability", baz2.getFooSet().size()==1 );
2836 assertTrue( "unkeyed reachability", baz1.getIntArray().length==0 );
2837 assertTrue( "unkeyed reachability", baz1.getFooSet().size()==0 );
2838 //System.out.println( s.print(baz1) + s.print(baz2) );
2839 FooProxy fp = (FooProxy) baz2.getFooSet().iterator().next();
2840 s.delete(fp);
2841 s.delete(baz1);
2842 s.delete(baz2);
2843 s.flush();
2844 s.connection().commit();
2845 s.close();
2846
2847 //now for collections of collections
2848 s = openSession();
2849 baz1 = new Baz();
2850 s.save(baz1);
2851 baz2 = new Baz();
2852 s.save(baz2);
2853 s.flush();
2854 s.connection().commit();
2855 s.close();
2856 s = openSession();
2857 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2858 baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2859 s.flush();
2860 s.connection().commit();
2861 s.close();
2862 s = openSession();
2863 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2864 s.flush();
2865 s.connection().commit();
2866 s.close();
2867 s = openSession();
2868 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2869 baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2870 //System.out.println( s.print(baz1) + s.print(baz2) );
2871 //System.out.println( s.print(baz1) + s.print(baz2) );
2872 s.delete(baz1);
2873 s.delete(baz2);
2874 s.flush();
2875 s.connection().commit();
2876 s.close();
2877
2878 //now for keyed collections
2879 s = openSession();
2880 baz1 = new Baz();
2881 s.save(baz1);
2882 baz2 = new Baz();
2883 s.save(baz2);
2884 Foo foo1 = new Foo();
2885 Foo foo2 = new Foo();
2886 s.save(foo1); s.save(foo2);
2887 baz1.setFooArray( new Foo[] { foo1, null, foo2 } );
2888 baz1.setStringDateMap( new TreeMap() );
2889 baz1.getStringDateMap().put("today", new Date( System.currentTimeMillis() ) );
2890 baz1.getStringDateMap().put("tomorrow", new Date( System.currentTimeMillis() + 86400000 ) );
2891 s.flush();
2892 s.connection().commit();
2893 s.close();
2894 s = openSession();
2895 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2896 baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2897 baz2.setFooArray( baz1.getFooArray() ); baz1.setFooArray(null);
2898 baz2.setStringDateMap( baz1.getStringDateMap() ); baz1.setStringDateMap(null);
2899 s.flush();
2900 s.connection().commit();
2901 s.close();
2902 s = openSession();
2903 baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
2904 baz1 = (Baz) s.load( Baz.class, baz1.getCode() );
2905 assertTrue( "reachability", baz2.getStringDateMap().size()==2 );
2906 assertTrue( "reachability", baz2.getFooArray().length==3 );
2907 assertTrue( "reachability", baz1.getStringDateMap().size()==0 );
2908 assertTrue( "reachability", baz1.getFooArray().length==0 );
2909 //System.out.println( s.print(baz1) + s.print(baz2) );
2910 assertTrue( "null element", baz2.getFooArray()[1]==null );
2911 assertTrue( "non-null element", baz2.getStringDateMap().get("today")!=null );
2912 assertTrue( "non-null element", baz2.getStringDateMap().get("tomorrow")!=null );
2913 assertTrue( "null element", baz2.getStringDateMap().get("foo")==null );
2914 s.delete( baz2.getFooArray()[0] );
2915 s.delete( baz2.getFooArray()[2] );
2916 s.delete(baz1);
2917 s.delete(baz2);
2918 s.flush();
2919 assertTrue( s.find("from java.lang.Object").size()==0 );
2920 s.connection().commit();
2921 s.close();
2922 }
2923
2924 public void testPersistentLifecycle() throws Exception {
2925 Session s = openSession();
2926 Qux q = new Qux();
2927 s.save(q);
2928 q.setStuff("foo bar baz qux");
2929 s.flush();
2930 s.connection().commit();
2931 s.close();
2932 s = openSession();
2933 q = (Qux) s.load( Qux.class, q.getKey() );
2934 assertTrue( "lifecycle create", q.getCreated() );
2935 assertTrue( "lifecycle load", q.getLoaded() );
2936 assertTrue( "lifecycle subobject", q.getFoo()!=null );
2937 s.delete(q);
2938 assertTrue( "lifecycle delete", q.getDeleted() );
2939 s.flush();
2940 s.connection().commit();
2941 s.close();
2942 s = openSession();
2943 assertTrue( "subdeletion", s.find("from Foo foo").size()==0);
2944 s.flush();
2945 s.connection().commit();
2946 s.close();
2947 }
2948
2949 public void testIterators() throws Exception {
2950 Session s = openSession();
2951 for ( int i=0; i<10; i++ ) {
2952 Qux q = new Qux();
2953 Object qid = s.save(q);
2954 assertTrue("not null", qid!=null);
2955 }
2956 s.flush();
2957 s.connection().commit();
2958 s.close();
2959
2960 s = openSession();
2961 Iterator iter = s.iterate("from Qux q where q.stuff is null");
2962 int count=0;
2963 while ( iter.hasNext() ) {
2964 Qux q = (Qux) iter.next();
2965 q.setStuff("foo");
2966 if (count==0 || count==5) iter.remove();
2967 count++;
2968 }
2969 assertTrue("iterate", count==10);
2970 s.flush();
2971 s.connection().commit();
2972 s.close();
2973
2974 s = openSession();
2975 assertTrue(
2976 "delete by query",
2977 s.delete("from Qux q where q.stuff=?", "foo", Hibernate.STRING)==8
2978 );
2979 s.flush();
2980 s.connection().commit();
2981 s.close();
2982
2983 s = openSession();
2984 iter = s.iterate("from Qux q");
2985 assertTrue( "empty iterator", !iter.hasNext() );
2986 s.flush();
2987 s.connection().commit();
2988 s.close();
2989 }
2990
2991 public void testVersioning() throws Exception {
2992 Session s = openSession();
2993 GlarchProxy g = new Glarch();
2994 s.save(g);
2995 GlarchProxy g2 = new Glarch();
2996 s.save(g2);
2997 Serializable gid = s.getIdentifier(g);
2998 Serializable g2id = s.getIdentifier(g2);
2999 g.setName("glarch");
3000 s.flush();
3001 s.connection().commit();
3002 s.close();
3003
3004 getSessions().evict(Glarch.class);
3005
3006 s = openSession();
3007 g = (GlarchProxy) s.load( Glarch.class, gid );
3008 s.lock(g, LockMode.UPGRADE);
3009 g2 = (GlarchProxy) s.load( Glarch.class, g2id );
3010 assertTrue( "version", g.getVersion()==1 );
3011 assertTrue( "version", g.getDerivedVersion()==1 );
3012 assertTrue( "version", g2.getVersion()==0 );
3013 g.setName("foo");
3014 assertTrue(
3015 "find by version",
3016 s.find("from Glarch g where g.version=2").size()==1
3017 );
3018 g.setName("bar");
3019 s.flush();
3020 s.connection().commit();
3021 s.close();
3022
3023 getSessions().evict(Glarch.class);
3024
3025 s = openSession();
3026 g = (GlarchProxy) s.load( Glarch.class, gid );
3027 g2 = (GlarchProxy) s.load( Glarch.class, g2id );
3028 assertTrue( "version", g.getVersion()==3 );
3029 assertTrue( "version", g.getDerivedVersion()==3 );
3030 assertTrue( "version", g2.getVersion()==0 );
3031 g.setNext(null);
3032 g2.setNext(g);
3033 s.delete(g2);
3034 s.delete(g);
3035 s.flush();
3036 s.connection().commit();
3037 s.close();
3038 }
3039
3040 public void testVersionedCollections() throws Exception {
3041 Session s = openSession();
3042 GlarchProxy g = new Glarch();
3043 s.save(g);
3044 g.setProxyArray( new GlarchProxy[] { g } );
3045 String gid = (String) s.getIdentifier(g);
3046 ArrayList list = new ArrayList();
3047 list.add("foo");
3048 g.setStrings(list);
3049 HashSet set = new HashSet();
3050 set.add(g);
3051 g.setProxySet(set);
3052 s.flush();
3053 s.connection().commit();
3054 s.close();
3055
3056 s = openSession();
3057 g = (GlarchProxy) s.load(Glarch.class, gid);
3058 assertTrue( g.getStrings().size()==1 );
3059 assertTrue( g.getProxyArray().length==1 );
3060 assertTrue( g.getProxySet().size()==1 );
3061 assertTrue( "versioned collection before", g.getVersion()==1 );
3062 s.flush();
3063 s.connection().commit();
3064 s.close();
3065
3066 s = openSession();
3067 g = (GlarchProxy) s.load(Glarch.class, gid);
3068 assertTrue( g.getStrings().get(0).equals("foo") );
3069 assertTrue( g.getProxyArray()[0]==g );
3070 assertTrue( g.getProxySet().iterator().next()==g );
3071 assertTrue( "versioned collection before", g.getVersion()==1 );
3072 s.flush();
3073 s.connection().commit();
3074 s.close();
3075
3076 s = openSession();
3077 g = (GlarchProxy) s.load(Glarch.class, gid);
3078 assertTrue( "versioned collection before", g.getVersion()==1 );
3079 g.getStrings().add("bar");
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()==2 );
3087 assertTrue( "versioned collection after", g.getStrings().size()==2 );
3088 g.setProxyArray(null);
3089 s.flush();
3090 s.connection().commit();
3091 s.close();
3092
3093 s = openSession();
3094 g = (GlarchProxy) s.load(Glarch.class, gid);
3095 assertTrue( "versioned collection after", g.getVersion()==3 );
3096 assertTrue( "versioned collection after", g.getProxyArray().length==0 );
3097 g.setFooComponents( new ArrayList() );
3098 g.setProxyArray(null);
3099 s.flush();
3100 s.connection().commit();
3101 s.close();
3102
3103 s = openSession();
3104 g = (GlarchProxy) s.load(Glarch.class, gid);
3105 assertTrue( "versioned collection after", g.getVersion()==4 );
3106 s.delete(g);
3107 s.flush();
3108 assertTrue( s.find("from java.lang.Object").size()==0 );
3109 s.connection().commit();
3110 s.close();
3111 }
3112
3113 /*public void testVersionedSubcollections() throws Exception {
3114 Session s = sessionsopenSession();
3115
3116 assertTrue( !s.iterate("from Fee fee").hasNext() );
3117
3118 GlarchProxy g = new Glarch();
3119 s.save(g);
3120 String gid = (String) s.getIdentifier(g);
3121 HashMap map = new HashMap();
3122 HashSet subSet = new HashSet();
3123 map.put("xxx", subSet);
3124 subSet.add("foo"); subSet.add("bar");
3125 g.setStringSets(map);
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()==1, "versioned collection before" );
3132 //System.out.println( g.getStringSets().get("xxx") );
3133 assertTrue( ( (Set) g.getStringSets().get("xxx") ).size()==2, "versioned collection before" );
3134 ( (Set) g.getStringSets().get("xxx") ).add("baz");
3135 s.flush();
3136 s.connection().commit();
3137 s.close();
3138 s = sessionsopenSession();
3139 g = (GlarchProxy) s.load(Glarch.class, gid);
3140 assertTrue( g.getVersion()==2, "versioned collection after" );
3141 //System.out.println( g.getStringSets().get("xxx") );
3142 assertTrue( ( (Set) g.getStringSets().get("xxx") ).size()==3, "versioned collection after" );
3143 s.delete(g);
3144 s.flush();
3145 s.connection().commit();
3146 s.close();
3147
3148 //with components! (note: this also tests some stuff testPersistCollections misses)
3149 s = sessionsopenSession();
3150 g = new Glarch(); //(GlarchProxy) s.create(Glarch.class);
3151 List list = new ArrayList();
3152 Date[] dates = new Date[] { null, null, new Date(), new Date(0) };
3153 list.add(null);
3154 list.add( new FooComponent("foo", 69, dates, new FooComponent("bar", 96, null, null, new Fee() ) ) );
3155 g.setFooComponents(list);
3156 String EIGHT_CHARS = "abcdefgh";
3157 s.save(g, EIGHT_CHARS + EIGHT_CHARS + EIGHT_CHARS + EIGHT_CHARS);
3158 gid = (String) s.getIdentifier(g);
3159 assertTrue( s.iterate("from Fee fee").hasNext() );
3160 g.getFooComponents().add( new FooComponent("bar", 96, null, null) );
3161 s.flush();
3162 s.connection().commit();
3163 s.close();
3164 s = sessionsopenSession();
3165 g = (GlarchProxy) s.load(Glarch.class, gid);
3166 assertTrue( g.getVersion()==2, "versioned collection before" );
3167 ( (FooComponent) g.getFooComponents().get(1) ).getImportantDates()[0] = new Date(123567890);
3168 s.flush();
3169 s.connection().commit();
3170 s.close();
3171 s = sessionsopenSession();
3172 g = (GlarchProxy) s.load(Glarch.class, gid);
3173 assertTrue( g.getVersion()==3, "versioned collection after" );
3174 ( (FooComponent) g.getFooComponents().get(1) ).getSubcomponent().setName("new name");
3175 assertTrue( ( (FooComponent) g.getFooComponents().get(1) ).getImportantDates()[0]!=null, "versioned collection after" );
3176 s.flush();
3177 s.connection().commit();
3178 s.close();
3179 s = sessionsopenSession();
3180 g = (GlarchProxy) s.load(Glarch.class, gid);
3181 assertTrue( ( (FooComponent) g.getFooComponents().get(1) ).getSubcomponent().getName().equals("new name"), "versioned collection after" );
3182 assertTrue( g.getVersion()==4, "versioned collection after" );
3183 s.delete(g);
3184 assertTrue( !s.iterate("from Fee fee").hasNext() );
3185 s.flush();
3186 s.connection().commit();
3187 s.close();
3188
3189
3190 }*/
3191
3192 public void testRecursiveLoad() throws Exception {
3193 //Non polymorphic class (there is an implementation optimization
3194 //being tested here)
3195 Session s = openSession();
3196 GlarchProxy last = new Glarch();
3197 s.save(last);
3198 last.setOrder( (short) 0 );
3199 for (int i=0; i<5; i++) {
3200 GlarchProxy next = new Glarch();
3201 s.save(next);
3202 last.setNext(next);
3203 last = next;
3204 last.setOrder( (short) (i+1) );
3205 }
3206 Iterator iter = s.iterate("from Glarch g");
3207 while ( iter.hasNext() ) {
3208 iter.next();
3209 }
3210 List list = s.find("from Glarch g");
3211 assertTrue( "recursive find", list.size()==6 );
3212 s.flush();
3213 s.connection().commit();
3214 s.close();
3215
3216 s = openSession();
3217 list = s.find("from Glarch g");
3218 assertTrue( "recursive iter", list.size()==6 );
3219 list = s.find("from Glarch g where g.next is not null");
3220 assertTrue( "recursive iter", list.size()==5 );
3221 s.flush();
3222 s.connection().commit();
3223 s.close();
3224
3225 s = openSession();
3226 iter = s.iterate("from Glarch g order by g.order asc");
3227 while ( iter.hasNext() ) {
3228 GlarchProxy g = (GlarchProxy) iter.next();
3229 assertTrue( "not null", g!=null );
3230 iter.remove();
3231 }
3232 s.flush();
3233 s.connection().commit();
3234 s.close();
3235
3236 //Same thing but using polymorphic class (no optimisation possible):
3237 s = openSession();
3238 FooProxy flast = new Bar();
3239 s.save(flast);
3240 flast.setString( "foo0" );
3241 for (int i=0; i<5; i++) {
3242 FooProxy foo = new Bar();
3243 s.save(foo);
3244 flast.setFoo(foo);
3245 flast = flast.getFoo();
3246 flast.setString( "foo" + (i+1) );
3247 }
3248 iter = s.iterate("from Foo foo");
3249 while ( iter.hasNext() ) {
3250 iter.next();
3251 }
3252 list = s.find("from Foo foo");
3253 assertTrue( "recursive find", list.size()==6 );
3254 s.flush();
3255 s.connection().commit();
3256 s.close();
3257
3258 s = openSession();
3259 list = s.find("from Foo foo");
3260 assertTrue( "recursive iter", list.size()==6 );
3261 iter = list.iterator();
3262 while ( iter.hasNext() ) {
3263 assertTrue( "polymorphic recursive load", iter.next() instanceof BarProxy );
3264 }
3265 s.flush();
3266 s.connection().commit();
3267 s.close();
3268
3269 s = openSession();
3270 iter = s.iterate("from Foo foo order by foo.string asc");
3271 while ( iter.hasNext() ) {
3272 BarProxy bar = (BarProxy) iter.next();
3273 assertTrue( "not null", bar!=null );
3274 iter.remove();
3275 }
3276 s.flush();
3277 s.connection().commit();
3278 s.close();
3279 }
3280
3281 public void testScrollableIterator() throws Exception {
3282 if ( getDialect() instanceof DB2Dialect || getDialect() instanceof OracleDialect || getDialect() instanceof SybaseDialect || getDialect() instanceof HSQLDialect ) {
3283 Session s = openSession();
3284 s.save( new Foo() );
3285 s.save( new Foo() );
3286 s.save( new Foo() );
3287 s.save( new Bar() );
3288 Query query = s.createQuery("select f, f.integer from Foo f");
3289 assertTrue( query.getReturnTypes().length==2 );
3290 ScrollableResults iter = query.scroll();
3291 assertTrue( iter.next() );
3292 assertTrue( iter.scroll(1) );
3293 FooProxy f2 = (FooProxy) iter.get()[0];
3294 assertTrue( f2!=null );
3295 assertTrue( iter.scroll(-1) );
3296 Object f1 = iter.get(0);
3297 iter.next();
3298 assertTrue( f1!=null && iter.get(0)==f2 );
3299 iter.getInteger(1);
3300
3301 assertTrue( !iter.scroll(100) );
3302 assertTrue( iter.first() );
3303 assertTrue( iter.scroll(3) );
3304 Object f4 = iter.get(0);
3305 assertTrue( f4!=null );
3306 assertTrue( !iter.next() );
3307 assertTrue( iter.first() );
3308 assertTrue( iter.get(0)==f1 );
3309 assertTrue( iter.last() );
3310 assertTrue( iter.get(0)==f4 );
3311 assertTrue( iter.previous() );
3312 s.connection().commit();
3313 s.close();
3314
3315 s = openSession();
3316 query = s.createQuery("select f, f.integer from Foo f");
3317 assertTrue( query.getReturnTypes().length==2 );
3318 iter = query.scroll();
3319 assertTrue( iter.next() );
3320 assertTrue( iter.scroll(1) );
3321 f2 = (FooProxy) iter.get()[0];
3322 assertTrue( f2!=null );
3323 assertTrue( f2.getString()!=null && f2.getComponent().getImportantDates().length > 0 );
3324 assertTrue( iter.scroll(-1) );
3325 f1 = iter.get(0);
3326 iter.next();
3327 assertTrue( f1!=null && iter.get(0)==f2 );
3328 iter.getInteger(1);
3329
3330 assertTrue( !iter.scroll(100) );
3331 assertTrue( iter.first() );
3332 assertTrue( iter.scroll(3) );
3333 f4 = iter.get(0);
3334 assertTrue( f4!=null );
3335 assertTrue( !iter.next() );
3336 assertTrue( iter.first() );
3337 assertTrue( iter.get(0)==f1 );
3338 assertTrue( iter.last() );
3339 assertTrue( iter.get(0)==f4 );
3340 assertTrue( iter.previous() );
3341 assertTrue( s.delete("from Foo")==4 );
3342 s.flush();
3343 assertTrue( s.find("from java.lang.Object").size()==0 );
3344 s.connection().commit();
3345 s.close();
3346 }
3347 }
3348
3349 public void testMultiColumnQueries() throws Exception {
3350 Session s = openSession();
3351 Foo foo = new Foo();
3352 s.save(foo);
3353 Foo foo1 = new Foo();
3354 s.save(foo1);
3355 foo.setFoo(foo1);
3356 List l = s.find("select parent, child from Foo parent, Foo child where parent.foo = child");
3357 assertTrue( "multi-column find", l.size()==1 );
3358
3359 Iterator rs = s.iterate("select count(distinct child.id), count(distinct parent.id) from Foo parent, Foo child where parent.foo = child");
3360 Object[] row = (Object[]) rs.next();
3361 assertTrue( "multi-column count", ( (Integer) row[0] ).intValue()==1 );
3362 assertTrue( "multi-column count", ( (Integer) row[1] ).intValue()==1 );
3363 assertTrue( !rs.hasNext() );
3364
3365 rs = s.iterate("select child.id, parent.id, child.long from Foo parent, Foo child where parent.foo = child");
3366 row = (Object[]) rs.next();
3367 assertTrue( "multi-column id", row[0].equals( foo.getFoo().getKey() ) );
3368 assertTrue( "multi-column id", row[1].equals( foo.getKey() ) );
3369 assertTrue( "multi-column property", row[2].equals( foo.getFoo().getLong() ) );
3370 assertTrue( !rs.hasNext() );
3371
3372 rs = s.iterate("select child.id, parent.id, child.long, child, parent.foo from Foo parent, Foo child where parent.foo = child");
3373 row = (Object[]) rs.next();
3374 assertTrue(
3375 foo.getFoo().getKey().equals( row[0] ) &&
3376 foo.getKey().equals( row[1] ) &&
3377 foo.getFoo().getLong().equals( row[2] ) &&
3378 row[3] == foo.getFoo() &&
3379 row[3]==row[4]
3380 );
3381 assertTrue( !rs.hasNext() );
3382
3383 row = (Object[]) l.get(0);
3384 assertTrue( "multi-column find", row[0]==foo && row[1]==foo.getFoo() );
3385 s.flush();
3386 s.connection().commit();
3387 s.close();
3388
3389 s = openSession();
3390 Iterator iter = s.iterate("select parent, child from Foo parent, Foo child where parent.foo = child and parent.string='a string'");
3391 int deletions=0;
3392 while ( iter.hasNext() ) {
3393 Object[] pnc = (Object[]) iter.next();
3394 s.delete( pnc[0] );
3395 s.delete( pnc[1] );
3396 deletions++;
3397 }
3398 assertTrue("multi-column iterate", deletions==1);
3399 s.flush();
3400 s.connection().commit();
3401 s.close();
3402 }
3403
3404 public void testDeleteTransient() throws Exception {
3405 Fee fee = new Fee();
3406 Fee fee2 = new Fee();
3407 fee2.setAnotherFee(fee);
3408 Session s = openSession();
3409 Transaction tx = s.beginTransaction();
3410 s.save(fee);
3411 s.save(fee2);
3412 s.flush();
3413 fee.setCount(123);
3414 tx.commit();
3415 s.close();
3416 s = openSession();
3417 tx = s.beginTransaction();
3418 s.delete(fee);
3419 s.delete(fee2);
3420 //foo.setAnotherFee(null);
3421 tx.commit();
3422 s.close();
3423 s = openSession();
3424 tx = s.beginTransaction();
3425 assertTrue( s.find("from Fee fee").size()==0 );
3426 tx.commit();
3427 s.close();
3428 }
3429
3430 public void testDeleteUpdatedTransient() throws Exception {
3431 Fee fee = new Fee();
3432 Fee fee2 = new Fee();
3433 fee2.setAnotherFee(fee);
3434 Session s = openSession();
3435 Transaction tx = s.beginTransaction();
3436 s.save(fee);
3437 s.save(fee2);
3438 s.flush();
3439 fee.setCount(123);
3440 tx.commit();
3441 s.close();
3442 s = openSession();
3443 tx = s.beginTransaction();
3444 s.update(fee);
3445 //fee2.setAnotherFee(null);
3446 s.update(fee2);
3447 s.delete(fee);
3448 s.delete(fee2);
3449 tx.commit();
3450 s.close();
3451 s = openSession();
3452 tx = s.beginTransaction();
3453 assertTrue( s.find("from Fee fee").size()==0 );
3454 tx.commit();
3455 s.close();
3456 }
3457
3458 public void testUpdateOrder() throws Exception {
3459 Session s = openSession();
3460 Fee fee1 = new Fee();
3461 s.save(fee1);
3462 Fee fee2 = new Fee();
3463 fee1.setFee(fee2);
3464 fee2.setFee(fee1);
3465 fee2.setFees( new HashSet() );
3466 Fee fee3 = new Fee();
3467 fee3.setFee(fee1);
3468 fee3.setAnotherFee(fee2);
3469 fee2.setAnotherFee(fee3);
3470 s.save(fee3);
3471 s.save(fee2);
3472 s.flush();
3473 s.connection().commit();
3474 s.close();
3475
3476 s = openSession();
3477 fee1.setCount(10);
3478 fee2.setCount(20);
3479 fee3.setCount(30);
3480 s.update(fee1);
3481 s.update(fee2);
3482 s.update(fee3);
3483 s.flush();
3484 s.delete(fee1);
3485 s.delete(fee2);
3486 s.delete(fee3);
3487 s.flush();
3488 s.connection().commit();
3489 s.close();
3490
3491 s = openSession();
3492 Transaction tx = s.beginTransaction();
3493 assertTrue( s.find("from Fee fee").size()==0 );
3494 tx.commit();
3495 s.close();
3496 }
3497
3498 public void testUpdateFromTransient() throws Exception {
3499 Session s = openSession();
3500 Fee fee1 = new Fee();
3501 s.save(fee1);
3502 Fee fee2 = new Fee();
3503 fee1.setFee(fee2);
3504 fee2.setFee(fee1);
3505 fee2.setFees( new HashSet() );
3506 Fee fee3 = new Fee();
3507 fee3.setFee(fee1);
3508 fee3.setAnotherFee(fee2);
3509 fee2.setAnotherFee(fee3);
3510 s.save(fee3);
3511 s.save(fee2);
3512 s.flush();
3513 s.connection().commit();
3514 s.close();
3515
3516 fee1.setFi("changed");
3517 s = openSession();
3518 s.saveOrUpdate(fee1);
3519 s.flush();
3520 s.connection().commit();
3521 s.close();
3522
3523 Qux q = new Qux("quxxy");
3524 q.setTheKey(0);
3525 fee1.setQux(q);
3526 s = openSession();
3527 s.saveOrUpdate(fee1);
3528 s.flush();
3529 s.connection().commit();
3530 s.close();
3531
3532
3533 s = openSession();
3534 fee1 = (Fee) s.load( Fee.class, fee1.getKey() );
3535 assertTrue( "updated from transient", fee1.getFi().equals("changed") );
3536 assertTrue( "unsaved value", fee1.getQux()!=null );
3537 s.delete( fee1.getQux() );
3538 fee1.setQux(null);
3539 s.flush();
3540 s.connection().commit();
3541 s.close();
3542
3543 fee2.setFi("CHANGED");
3544 fee2.getFees().add("an element");
3545 fee1.setFi("changed again");
3546 s = openSession();
3547 s.saveOrUpdate(fee2);
3548 s.update( fee1, fee1.getKey() );
3549 s.flush();
3550 s.connection().commit();
3551 s.close();
3552
3553 s = openSession();
3554 Fee fee = new Fee();
3555 s.load( fee, fee2.getKey() );
3556 fee1 = (Fee) s.load( Fee.class, fee1.getKey() );
3557 assertTrue( "updated from transient", fee1.getFi().equals("changed again") );
3558 assertTrue( "updated from transient", fee.getFi().equals("CHANGED") );
3559 assertTrue( "updated collection", fee.getFees().contains("an element") );
3560 s.flush();
3561 s.connection().commit();
3562 s.close();
3563
3564 fee.getFees().clear();
3565 fee.getFees().add("new element");
3566 fee1.setFee(null);
3567 s = openSession();
3568 s.saveOrUpdate(fee);
3569 s.saveOrUpdate(fee1);
3570 s.flush();
3571 s.connection().commit();
3572 s.close();
3573
3574 s = openSession();
3575 s.load( fee, fee.getKey() );
3576 assertTrue( "update", fee.getAnotherFee()!=null );
3577 assertTrue( "update", fee.getFee()!=null );
3578 assertTrue( "update", fee.getAnotherFee().getFee()==fee.getFee() );
3579 assertTrue( "updated collection", fee.getFees().contains("new element") );
3580 assertTrue( "updated collection", !fee.getFees().contains("an element") );
3581 s.flush();
3582 s.connection().commit();
3583 s.close();
3584
3585 fee.setQux( new Qux("quxy") );
3586 s = openSession();
3587 s.saveOrUpdate(fee);
3588 s.flush();
3589 s.connection().commit();
3590 s.close();
3591
3592 fee.getQux().setStuff("xxx");
3593 s = openSession();
3594 s.saveOrUpdate(fee);
3595 s.flush();
3596 s.connection().commit();
3597 s.close();
3598
3599 s = openSession();
3600 s.load( fee, fee.getKey() );
3601 assertTrue( "cascade update", fee.getQux()!=null );
3602 assertTrue( "cascade update", fee.getQux().getStuff().equals("xxx") );
3603 assertTrue( "update", fee.getAnotherFee()!=null );
3604 assertTrue( "update", fee.getFee()!=null );
3605 assertTrue( "update", fee.getAnotherFee().getFee()==fee.getFee() );
3606 fee.getAnotherFee().setAnotherFee(null);
3607 s.delete(fee);
3608 s.delete("from Fee fee");
3609 s.flush();
3610 s.connection().commit();
3611 s.close();
3612
3613 s = openSession();
3614 Transaction tx = s.beginTransaction();
3615 assertTrue( s.find("from Fee fee").size()==0 );
3616 tx.commit();
3617 s.close();
3618 }
3619
3620 public void testArraysOfTimes() throws Exception {
3621 Session s = openSession();
3622 Baz baz = new Baz() ;
3623 s.save(baz);
3624 baz.setDefaults();
3625 s.flush();
3626 s.connection().commit();
3627 s.close();
3628 s = openSession();
3629
3630 baz.getTimeArray()[2] = new Date(123);
3631 baz.getTimeArray()[3] = new java.sql.Time(1234);
3632 s.flush();
3633 s.connection().commit();
3634 s.close();
3635 s = openSession();
3636 baz = (Baz) s.load( Baz.class, baz.getCode() );
3637 s.delete(baz);
3638 s.flush();
3639 s.connection().commit();
3640 s.close();
3641 }
3642
3643 public void testComponents() throws Exception {
3644 Session s = openSession();
3645 Foo foo = new Foo();
3646 foo.setComponent( new FooComponent("foo", 69, null, new FooComponent("bar", 96, null, null) ) );
3647 s.save(foo);
3648 foo.getComponent().setName("IFA");
3649 s.flush();
3650 s.connection().commit();
3651 s.close();
3652 foo.setComponent(null);
3653 s = openSession();
3654 s.load( foo, foo.getKey() );
3655 assertTrue(
3656 "save components",
3657 foo.getComponent().getName().equals("IFA") &&
3658 foo.getComponent().getSubcomponent().getName().equals("bar")
3659 );
3660 assertTrue( "cascade save via component", foo.getComponent().getGlarch()!=null);
3661 foo.getComponent().getSubcomponent().setName("baz");
3662 s.flush();
3663 s.connection().commit();
3664 s.close();
3665 foo.setComponent(null);
3666 s = openSession();
3667 s.load( foo, foo.getKey() );
3668 assertTrue(
3669 "update components",
3670 foo.getComponent().getName().equals("IFA") &&
3671 foo.getComponent().getSubcomponent().getName().equals("baz")
3672 );
3673 s.delete(foo);
3674 s.flush();
3675 s.connection().commit();
3676 s.close();
3677
3678 s = openSession();
3679 foo = new Foo();
3680 s.save(foo);
3681 foo.setCustom( new String[] { "one", "two" } );
3682 assertTrue( s.find("from Foo foo where foo.custom.s1 = 'one'").get(0)==foo );
3683 s.delete(foo);
3684 s.flush();
3685 s.connection().commit();
3686 s.close();
3687
3688 }
3689
3690 public void testNoForeignKeyViolations() throws Exception {
3691 Session s = openSession();
3692 Glarch g1 = new Glarch();
3693 Glarch g2 = new Glarch();
3694 g1.setNext(g2);
3695 g2.setNext(g1);
3696 s.save(g1);
3697 s.save(g2);
3698 s.flush();
3699 s.connection().commit();
3700 s.close();
3701 s = openSession();
3702 List l = s.find("from Glarch g where g.next is not null");
3703 s.delete( l.get(0) );
3704 s.delete( l.get(1) );
3705 s.flush();
3706 s.connection().commit();
3707 s.close();
3708 }
3709
3710 public void testLazyCollections() throws Exception {
3711 Session s = openSession();
3712 Qux q = new Qux();
3713 s.save(q);
3714 s.flush();
3715 s.connection().commit();
3716 s.close();
3717
3718 s = openSession();
3719 q = (Qux) s.load( Qux.class, q.getKey() );
3720 s.flush();
3721 s.connection().commit();
3722 s.close();
3723
3724 System.out.println("Two exceptions are supposed to occur:");
3725 boolean ok = false;
3726 try {
3727 q.getMoreFums().isEmpty();
3728 }
3729 catch (LazyInitializationException e) {
3730 ok = true;
3731 }
3732 assertTrue( "lazy collection with one-to-many", ok );
3733
3734 ok = false;
3735 try {
3736 q.getFums().isEmpty();
3737 }
3738 catch (LazyInitializationException e) {
3739 ok = true;
3740 }
3741 assertTrue( "lazy collection with many-to-many", ok );
3742
3743 s = openSession();
3744 q = (Qux) s.load( Qux.class, q.getKey() );
3745 s.delete(q);
3746 s.flush();
3747 s.connection().commit();
3748 s.close();
3749 }
3750
3751 public void testNewSessionLifecycle() throws Exception {
3752 Session s = openSession();
3753 Serializable fid = null;
3754 try {
3755 Foo f = new Foo();
3756 s.save(f);
3757 fid = s.getIdentifier(f);
3758 s.flush();
3759 s.connection().commit();
3760 }
3761 catch (Exception e) {
3762 s.connection().rollback();
3763 throw e;
3764 }
3765 finally {
3766 s.close();
3767 }
3768 s = openSession();
3769 try {
3770 Foo f = new Foo();
3771 s.delete(f);
3772 s.flush();
3773 s.connection().commit();
3774 }
3775 catch (Exception e) {
3776 s.connection().rollback();
3777 }
3778 finally {
3779 s.close();
3780 }
3781 s = openSession();
3782 try {
3783 Foo f = (Foo) s.load(Foo.class, fid, LockMode.UPGRADE);
3784 s.delete(f);
3785 s.flush();
3786 s.connection().commit();
3787 }
3788 catch (Exception e) {
3789 s.connection().rollback();
3790 throw e;
3791 }
3792 finally {
3793 assertTrue( s.close()==null );
3794 }
3795 }
3796
3797 public void testDisconnect() throws Exception {
3798 Session s = openSession();
3799 Foo foo = new Foo();
3800 Foo foo2 = new Foo();
3801 s.save(foo);
3802 s.save(foo2);
3803 foo2.setFoo(foo);
3804 s.flush();
3805 s.connection().commit();
3806 s.disconnect();
3807 s.reconnect();
3808 s.delete(foo);
3809 foo2.setFoo(null);
3810 s.flush();
3811 s.connection().commit();
3812 s.disconnect();
3813 s.reconnect();
3814 s.delete(foo2);
3815 s.flush();
3816 s.connection().commit();
3817 s.close();
3818 }
3819
3820
3821
3822 public void testOrderBy() throws Exception {
3823
3824 Session s = openSession();
3825 Transaction tx = s.beginTransaction();
3826 Foo foo = new Foo();
3827 s.save(foo);
3828 List 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");
3829 assertTrue( "order by", list.size()==1 );
3830 Foo foo2 = new Foo();
3831 s.save(foo2);
3832 foo.setFoo(foo2);
3833 list = s.find("select foo.foo, foo.dependent from Foo foo order by foo.foo.string desc, foo.component.count asc, foo.dependent.id");
3834 assertTrue( "order by", list.size()==1 );
3835 list = s.find("select foo from Foo foo order by foo.dependent.id, foo.dependent.fi");
3836 assertTrue( "order by", list.size()==2 );
3837 s.delete(foo);
3838 s.delete(foo2);
3839 tx.commit();
3840 s.close();
3841
3842 s = openSession();
3843 Many manyB = new Many();
3844 s.save(manyB);
3845 One oneB = new One();
3846 s.save(oneB);
3847 oneB.setValue("b");
3848 manyB.setOne(oneB);
3849 Many manyA = new Many();
3850 s.save(manyA);
3851 One oneA = new One();
3852 s.save(oneA);
3853 oneA.setValue("a");
3854 manyA.setOne(oneA);
3855 s.flush();
3856 s.connection().commit();
3857 s.close();
3858
3859 s = openSession();
3860 Iterator it = s.iterate(
3861 "SELECT one FROM " +
3862 One.class.getName() +
3863 " one ORDER BY one.value ASC"
3864 );
3865 int count = 0;
3866 while ( it.hasNext() ) {
3867 One one = (One)it.next();
3868 switch (count) {
3869 case 0:
3870 assertTrue("ordering failed", "a".equals(one.getValue()));
3871 break;
3872 case 1:
3873 assertTrue("ordering failed", "b".equals(one.getValue()));
3874 break;
3875 default:
3876 assertTrue("more than two elements", false);
3877 break;
3878 }
3879 count ++;
3880 }
3881 s.flush();
3882 s.connection().commit();
3883 s.close();
3884
3885 s = openSession();
3886 it = s.iterate(
3887 "SELECT many.one FROM " +
3888 Many.class.getName() +
3889 " many ORDER BY many.one.value ASC, many.one.id"
3890 );
3891 count = 0;
3892 while ( it.hasNext() ) {
3893 One one = (One)it.next();
3894 switch (count) {
3895 case 0:
3896 assertTrue("'a' isn't first element", "a".equals(one.getValue()));
3897 break;
3898 case 1:
3899 assertTrue("'b' isn't second element", "b".equals(one.getValue()));
3900 break;
3901 default:
3902 assertTrue("more than two elements", false);
3903 break;
3904 }
3905 count ++;
3906 }
3907 s.flush();
3908 s.connection().commit();
3909 s.close();
3910
3911 s = openSession();
3912 oneA = (One)s.load(One.class, oneA.getKey());
3913 manyA = (Many)s.load(Many.class, manyA.getKey());
3914 oneB = (One)s.load(One.class, oneB.getKey());
3915 manyB = (Many)s.load(Many.class, manyB.getKey());
3916 s.delete(manyA);
3917 s.delete(oneA);
3918 s.delete(manyB);
3919 s.delete(oneB);
3920 s.flush();
3921 s.connection().commit();
3922 s.close();
3923 }
3924
3925 public void testManyToOne() throws Exception {
3926 Session s = openSession();
3927 One one = new One();
3928 s.save(one);
3929 one.setValue("yada");
3930 Many many = new Many();
3931 many.setOne(one);
3932 s.save(many);
3933 s.flush();
3934 s.connection().commit();
3935 s.close();
3936
3937 s = openSession();
3938 one = (One) s.load( One.class, one.getKey() );
3939 one.getManies().size();
3940 s.connection().commit();
3941 s.close();
3942
3943
3944 s = openSession();
3945 many = (Many) s.load( Many.class, many.getKey() );
3946 assertTrue( "many-to-one assoc", many.getOne()!=null );
3947 s.delete( many.getOne() );
3948 s.delete(many);
3949 s.flush();
3950 s.connection().commit();
3951 s.close();
3952 }
3953
3954 public void testSaveDelete() throws Exception {
3955 Session s = openSession();
3956 Foo f = new Foo();
3957 s.save(f);
3958 s.flush();
3959 s.connection().commit();
3960 s.close();
3961
3962 s = openSession();
3963 s.delete( s.load( Foo.class, f.getKey() ) );
3964 s.flush();
3965 s.connection().commit();
3966 s.close();
3967 }
3968
3969 /*public void testIdNotFound() throws Exception {
3970 // The following test is only enabled for MySQL which has no foreign key constraints.
3971 // I disabled this test cos it didn't clean up after itself so other tests failed
3972 if (db.equals("mysql")) {
3973 Session s = sessionsopenSession();
3974 Glarch g = (Glarch) s.create(Glarch.class);
3975 Glarch g2 = (Glarch) s.create(Glarch.class);
3976 g.setNext(g2);
3977 Serializable gid = s.getID(g);
3978 Serializable g2id = s.getID(g2);
3979 s.commit();
3980
3981 s = sessionsopenSession();
3982 g2 = (Glarch) s.load( Glarch.class, g2id );
3983 s.delete(g2);
3984 s.commit();
3985
3986 s = sessionsopenSession();
3987 boolean ok = false;
3988 try {
3989 g = (Glarch) s.load( Glarch.class, gid );
3990 }
3991 catch (HibernateException e) {
3992 ok = "id not found or provided object was wrong class".equals(e.getMessage());
3993 }
3994 catch (java.lang.StackOverflowError soe) {
3995 ok = false;
3996 }
3997 assertTrue( ok, "id not found");
3998 s.cancel();
3999 }
4000 }*/
4001
4002 public void testProxyArray() throws Exception {
4003 Session s = openSession();
4004 GlarchProxy g = new Glarch();
4005 Glarch g1 = new Glarch();
4006 Glarch g2 = new Glarch();
4007 g.setProxyArray( new GlarchProxy[] { g1, g2 } );
4008 Glarch g3 = new Glarch();
4009 s.save(g3);
4010 g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
4011 Set set = new HashSet();
4012 set.add(g1);
4013 set.add(g2);
4014 g.setProxySet(set);
4015 s.save(g);
4016 s.save(g1);
4017 s.save(g2);
4018 Serializable id = s.getIdentifier(g);
4019 s.flush();
4020 s.connection().commit();
4021 s.close();
4022
4023 s = openSession();
4024 g = (GlarchProxy) s.load(Glarch.class, id);
4025 assertTrue( "array of proxies", g.getProxyArray().length==2 );
4026 assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
4027 assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
4028 assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
4029 assertTrue( "set of proxies", g.getProxySet().size()==2 );
4030 Iterator iter = s.iterate("from Glarch g");
4031 while ( iter.hasNext() ) {
4032 iter.next();
4033 iter.remove();
4034 }
4035
4036 s.flush();
4037 s.connection().commit();
4038 s.disconnect();
4039 SerializationHelper.deserialize( SerializationHelper.serialize(s) );
4040 s.close();
4041 }
4042
4043 public void testCache() throws Exception {
4044 Session s = openSession();
4045 Immutable im = new Immutable();
4046 s.save(im);
4047 s.flush();
4048 s.connection().commit();
4049 s.close();
4050 s = openSession();
4051 s.load( im, im.getId() );
4052 s.connection().commit();
4053 s.close();
4054 s = openSession();
4055 s.load( im, im.getId() );
4056 assertTrue(
4057 "cached object identity",
4058 s.find(
4059 "from Immutable im where im = ?",
4060 im,
4061 Hibernate.entity(Immutable.class)
4062 ).get(0)==im &&
4063 im == s.load( Immutable.class, im.getId() )
4064 );
4065 s.connection().createStatement().executeUpdate("delete from immut");
4066 s.connection().commit();
4067 s.close();
4068 }
4069
4070 public void testFindLoad() throws Exception {
4071 Session s = openSession();
4072 FooProxy foo = new Foo();
4073 s.save(foo);
4074 s.flush();
4075 s.connection().commit();
4076 s.close();
4077 s = openSession();
4078 foo = (FooProxy) s.find("from Foo foo").get(0);
4079 FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
4080 assertTrue("find returns same object as load", foo==foo2);
4081 s.flush();
4082 s.connection().commit();
4083 s.close();
4084 s = openSession();
4085 foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
4086 foo = (FooProxy) s.find("from Foo foo").get(0);
4087 assertTrue("find returns same object as load", foo==foo2);
4088 s.delete("from Foo foo");
4089 s.flush();
4090 s.connection().commit();
4091 s.close();
4092 }
4093
4094 public void testRefresh() throws Exception {
4095 Session s = openSession();
4096 Foo foo = new Foo();
4097 s.save(foo);
4098 s.flush();
4099 s.connection().createStatement().executeUpdate("update "+getDialect().openQuote()+"foos"+getDialect().closeQuote()+" set long_ = -3");
4100 s.refresh(foo);
4101 assertTrue( foo.getLong().longValue()==-3l );
4102 assertTrue( s.getCurrentLockMode(foo)==LockMode.READ );
4103 s.refresh(foo, LockMode.UPGRADE);
4104 if ( getDialect().supportsOuterJoinForUpdate() ) {
4105 assertTrue( s.getCurrentLockMode(foo)==LockMode.UPGRADE );
4106 }
4107 s.delete(foo);
4108 s.flush();
4109 s.connection().commit();
4110 s.close();
4111 }
4112
4113 public void testAutoFlush() throws Exception {
4114 Session s = openSession();
4115 FooProxy foo = new Foo();
4116 s.save(foo);
4117 assertTrue( "autoflush create", s.find("from Foo foo").size()==1 );
4118 foo.setChar( new Character('X') );
4119 assertTrue( "autoflush update", s.find("from Foo foo where foo.char='X'").size()==1 );
4120 s.connection().commit();
4121 s.close();
4122 s = openSession();
4123 foo = (FooProxy) s.load( Foo.class, foo.getKey() );
4124 //s.update( new Foo(), foo.getKey() );
4125 //assertTrue( s.find("from Foo foo where not foo.char='X'").size()==1, "autoflush update" );
4126 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect) ) {
4127 foo.setBytes( "osama".getBytes() );
4128 assertTrue( "autoflush collection update", s.find("from Foo foo where 111 in elements(foo.bytes)").size()==1 );
4129 foo.getBytes()[0] = 69;
4130 assertTrue( "autoflush collection update", s.find("from Foo foo where 69 in elements(foo.bytes)").size()==1 );
4131 }
4132 s.delete(foo);
4133 assertTrue( "autoflush delete", s.find("from Foo foo").size()==0 );
4134 s.connection().commit();
4135 s.close();
4136 }
4137
4138 public void testVeto() throws Exception {
4139 Session s = openSession();
4140 Vetoer v = new Vetoer();
4141 s.save(v); Serializable id = s.save(v);
4142 s.flush();
4143 s.connection().commit();
4144 s.close();
4145 s = openSession();
4146 s.update(v, id); s.update(v, id);
4147 s.delete(v); s.delete(v);
4148 s.flush();
4149 s.connection().commit();
4150 s.close();
4151 }
4152
4153 public void testSerializableType() throws Exception {
4154 Session s = openSession();
4155 Vetoer v = new Vetoer();
4156 v.setStrings( new String[] { "foo", "bar", "baz" } );
4157 s.save(v); Serializable id = s.save(v);
4158 v.getStrings()[1] = "osama";
4159 s.flush();
4160 s.connection().commit();
4161 s.close();
4162 s = openSession();
4163 v = (Vetoer) s.load(Vetoer.class, id);
4164 assertTrue( "serializable type", v.getStrings()[1].equals("osama") );
4165 s.delete(v); s.delete(v);
4166 s.flush();
4167 s.connection().commit();
4168 s.close();
4169 }
4170
4171 public void testAutoFlushCollections() throws Exception {
4172 Session s = openSession();
4173 Transaction tx = s.beginTransaction();
4174 Baz baz = new Baz();
4175 baz.setDefaults();
4176 s.save(baz);
4177 tx.commit();
4178 s.close();
4179
4180 s = openSession();
4181 tx = s.beginTransaction();
4182 baz = (Baz) s.load(Baz.class, baz.getCode());
4183 baz.getStringArray()[0] = "bark";
4184 Iterator i = s.iterate("select elements(baz.stringArray) from Baz baz");
4185 boolean found = false;
4186 while ( i.hasNext() ) {
4187 if ( "bark".equals( i.next() ) ) found = true;
4188 }
4189 assertTrue(found);
4190 baz.setStringArray(null);
4191 i = s.iterate("select distinct elements(baz.stringArray) from Baz baz");
4192 assertTrue( !i.hasNext() );
4193 baz.setStringArray( new String[] { "foo", "bar" } );
4194 i = s.iterate("select elements(baz.stringArray) from Baz baz");
4195 assertTrue( i.hasNext() );
4196
4197 Foo foo = new Foo();
4198 s.save(foo);
4199 s.flush();
4200 baz.setFooArray( new Foo[] {foo} );
4201
4202 i = s.iterate("select foo from Baz baz join baz.fooArray foo");
4203 found = false;
4204 while ( i.hasNext() ) {
4205 if ( foo==i.next() ) found = true;
4206 }
4207 assertTrue(found);
4208
4209 baz.getFooArray()[0] = null;
4210 i = s.iterate("select foo from Baz baz join baz.fooArray foo");
4211 assertTrue( !i.hasNext() );
4212 baz.getFooArray()[0] = foo;
4213 i = s.iterate("select elements(baz.fooArray) from Baz baz");
4214 assertTrue( i.hasNext() );
4215
4216 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof InterbaseDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof SAPDBDialect) ) {
4217 baz.getFooArray()[0] = null;
4218 i = s.iterate(
4219 "from Baz baz where ? in elements(baz.fooArray)",
4220 foo, Hibernate.entity(Foo.class)
4221 );
4222 assertTrue( !i.hasNext() );
4223 baz.getFooArray()[0] = foo;
4224 i = s.iterate(
4225 "select foo from Foo foo where foo in "
4226 + "(select elt from Baz baz join baz.fooArray elt)"
4227 );
4228 assertTrue( i.hasNext() );
4229 }
4230 s.delete(foo);
4231 s.delete(baz);
4232 tx.commit();
4233 s.close();
4234
4235 }
4236
4237 public void testUserProvidedConnection() throws Exception {
4238 ConnectionProvider dcp = new DriverManagerConnectionProvider();
4239 dcp.configure( Environment.getProperties() );
4240 Session s = getSessions().openSession( dcp.getConnection() );
4241 Transaction tx = s.beginTransaction();
4242 s.find("from Fo");
4243 tx.commit();
4244 Connection c = s.disconnect();
4245 assertTrue( c!=null );
4246 s.reconnect(c);
4247 tx = s.beginTransaction();
4248 s.find("from Fo");
4249 tx.commit();
4250 assertTrue( s.close()==c );
4251 c.close();
4252 }
4253
4254 public void testCachedCollection() throws Exception {
4255 Session s = openSession();
4256 Baz baz = new Baz();
4257 baz.setDefaults();
4258 s.save(baz);
4259 s.flush();
4260 s.connection().commit();
4261 s.close();
4262 s = openSession();
4263 baz = (Baz) s.load( Baz.class, baz.getCode() );
4264 ( (FooComponent) baz.getTopComponents().get(0) ).setCount(99);
4265 s.flush();
4266 s.connection().commit();
4267 s.close();
4268 s = openSession();
4269 baz = (Baz) s.load( Baz.class, baz.getCode() );
4270 assertTrue( ( (FooComponent) baz.getTopComponents().get(0) ).getCount()==99 );
4271 s.delete(baz);
4272 s.flush();
4273 s.connection().commit();
4274 s.close();
4275 }
4276
4277 public void testComplicatedQuery() throws Exception {
4278 Session s = openSession();
4279 Foo foo = new Foo();
4280 Serializable id = s.save(foo);
4281 assertTrue(id!=null);
4282 Qux q = new Qux("q");
4283 foo.getDependent().setQux(q);
4284 s.save(q);
4285 q.getFoo().setString("foo2");
4286 //s.flush();
4287 //s.connection().commit();
4288 assertTrue(
4289 s.iterate("from Foo foo where foo.dependent.qux.foo.string = 'foo2'").hasNext()
4290 );
4291 s.delete(foo);
4292 s.flush();
4293 s.connection().commit();
4294 s.close();
4295 }
4296
4297 public void testLoadAfterDelete() throws Exception {
4298 Session s = openSession();
4299 Foo foo = new Foo();
4300 Serializable id = s.save(foo);
4301 s.flush();
4302 s.delete(foo);
4303 boolean err=false;
4304 try {
4305 s.load(Foo.class, id);
4306 }
4307 catch (ObjectDeletedException ode) {
4308 err=true;
4309 }
4310 assertTrue(err);
4311 s.flush();
4312 err=false;
4313 try {
4314 ( (FooProxy) s.load(Foo.class, id) ).getBool();
4315 }
4316 catch (ObjectNotFoundException onfe) {
4317 err=true;
4318 }
4319 assertTrue(err);
4320 Fo fo = Fo.newFo();
4321 id = new FumTest("").fumKey("abc"); //yuck!!
4322 s.save(fo, id);
4323 s.flush();
4324 s.delete(fo);
4325 err=false;
4326 try {
4327 s.load(Fo.class, id);
4328 }
4329 catch (ObjectDeletedException ode) {
4330 err=true;
4331 }
4332 assertTrue(err);
4333 s.flush();
4334 err=false;
4335 try {
4336 s.load(Fo.class, id);
4337 }
4338 catch (ObjectNotFoundException onfe) {
4339 err=true;
4340 }
4341 assertTrue(err);
4342 s.connection().commit();
4343 s.close();
4344 }
4345
4346 public void testObjectType() throws Exception {
4347 Session s = openSession();
4348 GlarchProxy g = new Glarch();
4349 Foo foo = new Foo();
4350 g.setAny(foo);
4351 Serializable gid = s.save(g);
4352 s.save(foo);
4353 s.flush();
4354 s.connection().commit();
4355 s.close();
4356 s = openSession();
4357 g = (GlarchProxy) s.load(Glarch.class, gid);
4358 assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy );
4359 s.delete( g.getAny() );
4360 s.delete(g);
4361 //s.delete( g.getAny() );
4362 s.flush();
4363 s.connection().commit();
4364 s.close();
4365 }
4366
4367
4368 public void testAny() throws Exception {
4369 Session s = openSession();
4370 One one = new One();
4371 BarProxy foo = new Bar();
4372 foo.setObject(one);
4373 //Serializable oid = s.save(one);
4374 Serializable fid = s.save(foo);
4375 Serializable oid = one.getKey();
4376 s.flush();
4377 s.connection().commit();
4378 s.close();
4379 s = openSession();
4380 assertTrue( s.find(
4381 "from Bar bar where bar.object.id = ? and bar.object.class = ?",
4382 new Object[] { oid, new Character('O') },
4383 new Type[] { Hibernate.LONG, Hibernate.CHARACTER }
4384 ).size()==1 );
4385 assertTrue( s.find(
4386 "select one from One one, Bar bar where bar.object.id = one.id and bar.object.class = 'O'"
4387 ).size()==1 );
4388 s.flush();
4389 s.connection().commit();
4390 s.close();
4391 s = openSession();
4392 foo = (BarProxy) s.load(Foo.class, fid);
4393 assertTrue( foo.getObject()!=null && foo.getObject() instanceof One && s.getIdentifier( foo.getObject() ).equals(oid) );
4394 //s.delete( foo.getObject() );
4395 s.delete(foo);
4396 s.flush();
4397 s.connection().commit();
4398 s.close();
4399 }
4400
4401 public void testEmbeddedCompositeID() throws Exception {
4402 Session s = openSession();
4403 Location l = new Location();
4404 l.setCountryCode("AU");
4405 l.setDescription("foo bar");
4406 l.setLocale( Locale.getDefault() );
4407 l.setStreetName("Brunswick Rd");
4408 l.setStreetNumber(300);
4409 l.setCity("Melbourne");
4410 s.save(l);
4411 s.flush();
4412 s.connection().commit();
4413 s.close();
4414 s = openSession();
4415 s.setFlushMode(FlushMode.NEVER);
4416 l = (Location) s.find("from Location l where l.countryCode = 'AU' and l.description='foo bar'").get(0);
4417 assertTrue( l.getCountryCode().equals("AU") );
4418 assertTrue( l.getCity().equals("Melbourne") );
4419 assertTrue( l.getLocale().equals( Locale.getDefault() ) );
4420 assertTrue( s.createCriteria(Location.class).add( Expression.eq( "streetNumber", new Integer(300) ) ).list().size()==1 );
4421 s.connection().commit();
4422 s.close();
4423
4424 s = openSession();
4425 l.setDescription("sick're");
4426 s.update(l);
4427 s.flush();
4428 s.connection().commit();
4429 s.close();
4430 s = openSession();
4431 l = new Location();
4432 l.setCountryCode("AU");
4433 l.setDescription("foo bar");
4434 l.setLocale(Locale.ENGLISH);
4435 l.setStreetName("Brunswick Rd");
4436 l.setStreetNumber(300);
4437 l.setCity("Melbourne");
4438 assertTrue( l==s.load(Location.class, l) );
4439 assertTrue( l.getLocale().equals( Locale.getDefault() ) );
4440 s.delete(l);
4441 s.flush();
4442 s.connection().commit();
4443 s.close();
4444 }
4445
4446 public void testAutosaveChildren() throws Exception {
4447 Session s = openSession();
4448 Transaction t = s.beginTransaction();
4449 Baz baz = new Baz();
4450 Set bars = new HashSet();
4451 baz.setCascadingBars(bars);
4452 s.save(baz);
4453 t.commit();
4454 s.close();
4455
4456 s = openSession();
4457 t = s.beginTransaction();
4458 baz = (Baz) s.load( Baz.class, baz.getCode() );
4459 baz.getCascadingBars().add( new Bar() );
4460 baz.getCascadingBars().add( new Bar() );
4461 t.commit();
4462 s.close();
4463
4464 s = openSession();
4465 t = s.beginTransaction();
4466 baz = (Baz) s.load( Baz.class, baz.getCode() );
4467 assertTrue( baz.getCascadingBars().size()==2 );
4468 assertTrue( baz.getCascadingBars().iterator().next()!=null );
4469 baz.getCascadingBars().clear(); //test all-delete-orphan;
4470 s.flush();
4471 assertTrue( s.find("from Bar bar").size()==0 );
4472 s.delete(baz);
4473 t.commit();
4474 s.close();
4475 }
4476
4477 public void testOrphanDelete() throws Exception {
4478 Session s = openSession();
4479 Transaction t = s.beginTransaction();
4480 Baz baz = new Baz();
4481 Set bars = new HashSet();
4482 baz.setCascadingBars(bars);
4483 bars.add( new Bar() );
4484 bars.add( new Bar() );
4485 bars.add( new Bar() );
4486 bars.add( new Bar() );
4487 s.save(baz);
4488 t.commit();
4489 s.close();
4490
4491 s = openSession();
4492 t = s.beginTransaction();
4493 baz = (Baz) s.load( Baz.class, baz.getCode() );
4494 bars = baz.getCascadingBars();
4495 assertEquals( 4, bars.size() );
4496 bars.remove( bars.iterator().next() );
4497 assertEquals( 3, s.find("From Bar bar").size() );
4498 t.commit();
4499 s.close();
4500
4501 s = openSession();
4502 t = s.beginTransaction();
4503 baz = (Baz) s.load( Baz.class, baz.getCode() );
4504 bars = baz.getCascadingBars();
4505 assertEquals( 3, bars.size() );
4506 bars.remove( bars.iterator().next() );
4507 s.delete(baz);
4508 bars.remove( bars.iterator().next() );
4509 assertEquals( 0, s.find("From Bar bar").size() );
4510 t.commit();
4511 s.close();
4512
4513 }
4514
4515 public void testTransientOrphanDelete() throws Exception {
4516 Session s = openSession();
4517 Transaction t = s.beginTransaction();
4518 Baz baz = new Baz();
4519 Set bars = new HashSet();
4520 baz.setCascadingBars(bars);
4521 bars.add( new Bar() );
4522 bars.add( new Bar() );
4523 bars.add( new Bar() );
4524 List foos = new ArrayList();
4525 foos.add( new Foo() );
4526 foos.add( new Foo() );
4527 baz.setFooBag(foos);
4528 s.save(baz);
4529 Iterator i = new JoinedIterator( new Iterator[] {foos.iterator(), bars.iterator()} );
4530 while ( i.hasNext() ) {
4531 FooComponent cmp = ( (Foo) i.next() ).getComponent();
4532 s.delete( cmp.getGlarch() );
4533 cmp.setGlarch(null);
4534 }
4535 t.commit();
4536 s.close();
4537
4538 bars.remove( bars.iterator().next() );
4539 foos.remove(1);
4540 s = openSession();
4541 t = s.beginTransaction();
4542 s.update(baz);
4543 assertEquals( 2, s.find("From Bar bar").size() );
4544 assertEquals( 3, s.find("From Foo foo").size() );
4545 t.commit();
4546 s.close();
4547
4548 foos.remove(0);
4549 s = openSession();
4550 t = s.beginTransaction();
4551 s.update(baz);
4552 bars.remove( bars.iterator().next() );
4553 assertEquals( 1, s.find("From Foo foo").size() );
4554 s.delete(baz);
4555 //s.flush();
4556 assertEquals( 0, s.find("From Foo foo").size() );
4557 t.commit();
4558 s.close();
4559
4560 }
4561
4562 public void testProxiesInCollections() throws Exception {
4563 Session s = openSession();
4564 Baz baz = new Baz();
4565 Bar bar = new Bar();
4566 Bar bar2 = new Bar();
4567 s.save(bar);
4568 Serializable bar2id = s.save(bar2);
4569 baz.setFooArray( new Foo[] { bar, bar2 } );
4570 HashSet set = new HashSet();
4571 bar = new Bar();
4572 s.save(bar);
4573 set.add(bar);
4574 baz.setFooSet(set);
4575 set = new HashSet();
4576 set.add( new Bar() );
4577 set.add( new Bar() );
4578 baz.setCascadingBars(set);
4579 ArrayList list = new ArrayList();
4580 list.add( new Foo() );
4581 baz.setFooBag(list);
4582 Serializable id = s.save(baz);
4583 Serializable bid = ( (Bar) baz.getCascadingBars().iterator().next() ).getKey();
4584 s.flush();
4585 s.connection().commit();
4586 s.close();
4587
4588 s = openSession();
4589 BarProxy barprox = (BarProxy) s.load(Bar.class, bid);
4590 BarProxy bar2prox = (BarProxy) s.load(Bar.class, bar2id);
4591 assertTrue(bar2prox instanceof HibernateProxy);
4592 assertTrue(barprox instanceof HibernateProxy);
4593 baz = (Baz) s.load(Baz.class, id);
4594 Iterator i = baz.getCascadingBars().iterator();
4595 BarProxy b1 = (BarProxy) i.next();
4596 BarProxy b2 = (BarProxy) i.next();
4597 assertTrue( ( b1==barprox && !(b2 instanceof HibernateProxy) ) || ( b2==barprox && !(b1 instanceof HibernateProxy) ) ); //one-to-many
4598 assertTrue( baz.getFooArray()[0] instanceof HibernateProxy ); //many-to-many
4599 assertTrue( baz.getFooArray()[1]==bar2prox );
4600 if ( !isOuterJoinFetchingDisabled() ) assertTrue( !(baz.getFooBag().iterator().next() instanceof HibernateProxy) ); //many-to-many outer-join="true"
4601 assertTrue( !(baz.getFooSet().iterator().next() instanceof HibernateProxy) ); //one-to-many
4602 s.delete("from Baz");
4603 s.delete("from Foo");
4604 s.flush();
4605 s.connection().commit();
4606 s.close();
4607 }
4608
4609 public void testService() throws Exception {
4610 HibernateService hs = new HibernateService();
4611 hs.setJndiName("SessionFactory");
4612 hs.setMapResources("net/sf/hibernate/test/Simple.hbm.xml, net/sf/hibernate/test/Blobber.hbm.xml");
4613 hs.setShowSqlEnabled("true");
4614 hs.start();
4615 hs.stop();
4616 hs.setProperty("foo", "bar");
4617 hs.start();
4618 hs.stop();
4619 }
4620
4621 public void testPSCache() throws Exception {
4622 Session s = openSession();
4623 for ( int i=0; i<10; i++ ) s.save( new Foo() );
4624 Query q = s.createQuery("from Foo");
4625 q.setMaxResults(2);
4626 q.setFirstResult(5);
4627 assertTrue( q.list().size()==2 );
4628 q = s.createQuery("from Foo");
4629 assertTrue( q.list().size()==10 );
4630 assertTrue( q.list().size()==10 );
4631 q.setMaxResults(3);
4632 q.setFirstResult(3);
4633 assertTrue( q.list().size()==3 );
4634 q = s.createQuery("from Foo");
4635 assertTrue( q.list().size()==10 );
4636 s.connection().commit();
4637 s.close();
4638 s = openSession();
4639 q = s.createQuery("from Foo");
4640 assertTrue( q.list().size()==10 );
4641 q.setMaxResults(5);
4642 assertTrue( q.list().size()==5 );
4643 s.delete("from Foo");
4644 s.connection().commit();
4645 s.close();
4646
4647 }
4648
4649 public void testForCertain() throws Exception {
4650 Glarch g = new Glarch();
4651 Glarch g2 = new Glarch();
4652 List set = new ArrayList();
4653 set.add("foo");
4654 g2.setStrings(set);
4655 Session s = openSession();
4656 Transaction t = s.beginTransaction();
4657 Serializable gid = (Serializable) s.save(g);
4658 Serializable g2id = (Serializable) s.save(g2);
4659 t.commit();
4660 assertTrue( g.getVersion()==0 );
4661 assertTrue( g2.getVersion()==0 );
4662 s.close();
4663
4664 s = openSession();
4665 t = s.beginTransaction();
4666 g = (Glarch) s.get(Glarch.class, gid);
4667 g2 = (Glarch) s.get(Glarch.class, g2id);
4668 assertTrue( g2.getStrings().size()==1 );
4669 s.delete(g);
4670 s.delete(g2);
4671 t.commit();
4672 s.close();
4673
4674 }
4675
4676 public void testBagMultipleElements() throws Exception {
4677 Session s = openSession();
4678 Transaction t = s.beginTransaction();
4679 Baz baz = new Baz();
4680 baz.setBag( new ArrayList() );
4681 baz.setByteBag( new ArrayList() );
4682 s.save(baz);
4683 baz.getBag().add("foo");
4684 baz.getBag().add("bar");
4685 baz.getByteBag().add( "foo".getBytes() );
4686 baz.getByteBag().add( "bar".getBytes() );
4687 t.commit();
4688 s.close();
4689
4690 s = openSession();
4691 t = s.beginTransaction();
4692 //put in cache
4693 baz = (Baz) s.get( Baz.class, baz.getCode() );
4694 assertTrue( baz.getBag().size()==2 );
4695 assertTrue( baz.getByteBag().size()==2 );
4696 t.commit();
4697 s.close();
4698
4699 s = openSession();
4700 t = s.beginTransaction();
4701 baz = (Baz) s.get( Baz.class, baz.getCode() );
4702 assertTrue( baz.getBag().size()==2 );
4703 assertTrue( baz.getByteBag().size()==2 );
4704 baz.getBag().remove("bar");
4705 baz.getBag().add("foo");
4706 baz.getByteBag().add( "bar".getBytes() );
4707 t.commit();
4708 s.close();
4709
4710 s = openSession();
4711 t = s.beginTransaction();
4712 baz = (Baz) s.get( Baz.class, baz.getCode() );
4713 assertTrue( baz.getBag().size()==2 );
4714 assertTrue( baz.getByteBag().size()==3 );
4715 s.delete(baz);
4716 t.commit();
4717 s.close();
4718 }
4719
4720 public void testWierdSession() throws Exception {
4721 Session s = openSession();
4722 Transaction t = s.beginTransaction();
4723 Serializable id = s.save( new Foo() );
4724 t.commit();
4725 s.close();
4726
4727 s = openSession();
4728 s.setFlushMode(FlushMode.NEVER);
4729 t = s.beginTransaction();
4730 Foo foo = (Foo) s.get(Foo.class, id);
4731 t.commit();
4732 s.disconnect();
4733
4734 s.reconnect();
4735 t = s.beginTransaction();
4736 s.flush();
4737 t.commit();
4738 s.close();
4739
4740 s = openSession();
4741 t = s.beginTransaction();
4742 foo = (Foo) s.get(Foo.class, id);
4743 s.delete(foo);
4744 t.commit();
4745 s.close();
4746 }
4747
4748 public static Test suite() {
4749 return new TestSuite(FooBarTest.class);
4750 }
4751
4752 public static void main(String[] args) throws Exception {
4753 TestRunner.run( suite() );
4754 }
4755
4756 protected void configure(Configuration cfg) {
4757 if ( getDialect() instanceof OracleDialect ) {
4758 ( (RootClass) cfg.getClassMapping("org.hibernate.test.legacy.Foo") ).setForceDiscriminator(false);
4759 }
4760 }
4761
4762 protected String[] getMappings() {
4763 return new String[] {
4764 "legacy/FooBar.hbm.xml",
4765 "legacy/Baz.hbm.xml",
4766 "legacy/Qux.hbm.xml",
4767 "legacy/Glarch.hbm.xml",
4768 "legacy/Fum.hbm.xml",
4769 "legacy/Fumm.hbm.xml",
4770 "legacy/Fo.hbm.xml",
4771 "legacy/One.hbm.xml",
4772 "legacy/Many.hbm.xml",
4773 "legacy/Immutable.hbm.xml",
4774 "legacy/Fee.hbm.xml",
4775 "legacy/Vetoer.hbm.xml",
4776 "legacy/Holder.hbm.xml",
4777 "legacy/Location.hbm.xml",
4778 "legacy/Stuff.hbm.xml",
4779 "legacy/Container.hbm.xml",
4780 "legacy/Simple.hbm.xml",
4781 "legacy/XY.hbm.xml"
4782 };
4783 }
4784
4785}