1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.sql.Connection ; 6 import java.sql.SQLException ; 7 import java.util.ArrayList ; 8 import java.util.Date ; 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import junit.framework.Test; 16 import junit.framework.TestSuite; 17 import junit.textui.TestRunner; 18 19 import org.hibernate.Criteria; 20 import org.hibernate.FetchMode; 21 import org.hibernate.Hibernate; 22 import org.hibernate.HibernateException; 23 import org.hibernate.LockMode; 24 import org.hibernate.ObjectNotFoundException; 25 import org.hibernate.ReplicationMode; 26 import org.hibernate.Transaction; 27 import org.hibernate.classic.Session; 28 import org.hibernate.criterion.Expression; 29 import org.hibernate.dialect.DB2Dialect; 30 import org.hibernate.dialect.HSQLDialect; 31 import org.hibernate.dialect.MySQLDialect; 32 import org.hibernate.test.TestCase; 33 34 public class ParentChildTest extends TestCase { 35 36 public ParentChildTest(String x) { 37 super(x); 38 } 39 40 public void testReplicate() throws Exception { 41 if ( getDialect() instanceof HSQLDialect ) return; 42 Session s = openSession(); 43 Container baz = new Container(); 44 Contained f = new Contained(); 45 List list = new ArrayList (); 46 list.add(baz); 47 f.setBag(list); 48 List list2 = new ArrayList (); 49 list2.add(f); 50 baz.setBag(list2); 51 s.save(f); 52 s.save(baz); 53 s.flush(); 54 s.connection().commit(); 55 s.close(); 56 s = openSession(); 57 s.replicate(baz, ReplicationMode.OVERWRITE); 58 s.flush(); 59 s.connection().commit(); 60 s.close(); 61 s = openSession(); 62 s.replicate(baz, ReplicationMode.IGNORE); 63 s.flush(); 64 s.connection().commit(); 65 s.close(); 66 s = openSession(); 67 s.delete(baz); 68 s.delete(f); 69 s.flush(); 70 s.connection().commit(); 71 s.close(); 72 } 73 74 public void testQueryOneToOne() throws Exception { 75 Session s = openSession(); 76 Serializable id = s.save( new Parent() ); 77 assertTrue( s.find("from Parent p left join fetch p.child").size()==1 ); 78 s.flush(); 79 s.connection().commit(); 80 s.close(); 81 82 s = openSession(); 83 Parent p = (Parent) s.createQuery("from Parent p left join fetch p.child").uniqueResult(); 84 assertTrue( p.getChild()==null ); 85 s.find("from Parent p join p.child c where c.x > 0"); 86 s.find("from Child c join c.parent p where p.x > 0"); 87 s.flush(); 88 s.connection().commit(); 89 s.close(); 90 91 s = openSession(); 92 s.delete( s.get(Parent.class, id) ); 93 s.flush(); 94 s.connection().commit(); 95 s.close(); 96 } 97 98 public void testProxyReuse() throws Exception { 99 Session s = openSession(); 100 Transaction t = s.beginTransaction(); 101 FooProxy foo = new Foo(); 102 FooProxy foo2 = new Foo(); 103 Serializable id = s.save(foo); 104 Serializable id2 = s.save(foo2); 105 foo2.setInt(1234567); 106 foo.setInt(1234); 107 t.commit(); 108 s.close(); 109 110 s = openSession(); 111 t = s.beginTransaction(); 112 foo = (FooProxy) s.load(Foo.class, id); 113 foo2 = (FooProxy) s.load(Foo.class, id2); 114 assertFalse( Hibernate.isInitialized(foo) ); 115 Hibernate.initialize(foo2); 116 Hibernate.initialize(foo); 117 assertTrue( foo.getComponent().getImportantDates().length==4 ); 118 assertTrue( foo2.getComponent().getImportantDates().length==4 ); 119 t.commit(); 120 s.close(); 121 122 s = openSession(); 123 t = s.beginTransaction(); 124 foo.setFloat( new Float (1.2f) ); 125 foo2.setFloat( new Float (1.3f) ); 126 foo2.getDependent().setKey(null); 127 foo2.getComponent().getSubcomponent().getFee().setKey(null); 128 assertFalse( foo2.getKey().equals(id) ); 129 s.save(foo, "xyzid"); 130 s.update(foo2, id); assertEquals( foo2.getKey(), id ); 132 assertTrue( foo2.getInt()==1234567 ); 133 assertEquals( foo.getKey(), "xyzid" ); 134 t.commit(); 135 s.close(); 136 137 s = openSession(); 138 t = s.beginTransaction(); 139 foo = (FooProxy) s.load(Foo.class, id); 140 assertTrue( foo.getInt()==1234567 ); 141 assertTrue( foo.getComponent().getImportantDates().length==4 ); 142 String feekey = foo.getDependent().getKey(); 143 String fookey = foo.getKey(); 144 s.delete(foo); 145 s.delete( s.get(Foo.class, id2) ); 146 s.delete( s.get(Foo.class, "xyzid") ); 147 assertTrue( s.delete("from java.lang.Object")==3 ); 148 t.commit(); 149 s.close(); 150 151 foo.setKey(fookey); 153 foo.getDependent().setKey(feekey); 154 foo.getComponent().setGlarch(null); 155 foo.getComponent().setSubcomponent(null); 156 157 s = openSession(); 158 t = s.beginTransaction(); 159 s.replicate(foo, ReplicationMode.OVERWRITE); 161 t.commit(); 162 s.close(); 163 164 s = openSession(); 165 t = s.beginTransaction(); 166 Foo refoo = (Foo) s.get(Foo.class, id); 167 assertEquals( feekey, refoo.getDependent().getKey() ); 168 s.delete(refoo); 169 t.commit(); 170 s.close(); 171 } 172 173 public void testComplexCriteria() throws Exception { 174 Session s = openSession(); 175 Transaction t = s.beginTransaction(); 176 Baz baz = new Baz(); 177 s.save(baz); 178 baz.setDefaults(); 179 Map topGlarchez = new HashMap (); 180 baz.setTopGlarchez(topGlarchez); 181 Glarch g1 = new Glarch(); 182 g1.setName("g1"); 183 s.save(g1); 184 Glarch g2 = new Glarch(); 185 g2.setName("g2"); 186 s.save(g2); 187 g1.setProxyArray( new GlarchProxy[] {g2} ); 188 topGlarchez.put( new Character ('1'),g1 ); 189 topGlarchez.put( new Character ('2'), g2); 190 Foo foo1 = new Foo(); 191 Foo foo2 = new Foo(); 192 s.save(foo1); 193 s.save(foo2); 194 baz.getFooSet().add(foo1); 195 baz.getFooSet().add(foo2); 196 baz.setFooArray( new FooProxy[] { foo1 } ); 197 198 LockMode lockMode = (getDialect() instanceof DB2Dialect) ? LockMode.READ : LockMode.UPGRADE; 199 200 Criteria crit = s.createCriteria(Baz.class); 201 crit.createCriteria("topGlarchez") 202 .add( Expression.isNotNull("name") ) 203 .createCriteria("proxyArray") 204 .add( Expression.eqProperty("name", "name") ) 205 .add( Expression.eq("name", "g2") ) 206 .add( Expression.gt("x", new Integer (-666) ) ); 207 crit.createCriteria("fooSet") 208 .add( Expression.isNull("null") ) 209 .add( Expression.eq("string", "a string") ) 210 .add( Expression.lt("integer", new Integer (-665) ) ); 211 crit.createCriteria("fooArray") 212 .add( Expression.eq("string", "a string") ) 213 .setLockMode(lockMode); 214 215 List list = crit.list(); 216 assertTrue( list.size()==2 ); 217 218 s.createCriteria(Glarch.class).setLockMode(LockMode.UPGRADE).list(); 219 s.createCriteria(Glarch.class).setLockMode(Criteria.ROOT_ALIAS, LockMode.UPGRADE).list(); 220 221 g2.setName(null); 222 t.commit(); 223 s.close(); 224 225 s = openSession(); 226 t = s.beginTransaction(); 227 228 list = s.createCriteria(Baz.class).add( Expression.isEmpty("fooSet") ).list(); 229 assertEquals( list.size(), 0 ); 230 231 list = s.createCriteria(Baz.class).add( Expression.isNotEmpty("fooSet") ).list(); 232 assertEquals( new HashSet (list).size(), 1 ); 233 234 list = s.createCriteria(Baz.class).add( Expression.sizeEq("fooSet", 2) ).list(); 235 assertEquals( new HashSet (list).size(), 1 ); 236 237 t.commit(); 238 s.close(); 239 240 s = openSession(); 241 t = s.beginTransaction(); 242 243 crit = s.createCriteria(Baz.class) 244 .setLockMode(lockMode); 245 crit.createCriteria("topGlarchez") 246 .add( Expression.gt( "x", new Integer (-666) ) ); 247 crit.createCriteria("fooSet") 248 .add( Expression.isNull("null") ); 249 list = crit.list(); 250 251 assertTrue( list.size()==4 ); 252 baz = (Baz) crit.uniqueResult(); 253 assertTrue( Hibernate.isInitialized(baz.getTopGlarchez()) ); assertTrue( !Hibernate.isInitialized(baz.getFooSet()) ); 255 256 262 list = s.createCriteria(Baz.class) 263 .createCriteria("fooSet") 264 .createCriteria("foo") 265 .createCriteria("component.glarch") 266 .add( Expression.eq("name", "xxx") ) 267 .list(); 268 assertTrue( list.size()==0 ); 269 270 list = s.createCriteria(Baz.class) 271 .createAlias("fooSet", "foo") 272 .createAlias("foo.foo", "foo2") 273 .setLockMode("foo2", lockMode) 274 .add( Expression.isNull("foo2.component.glarch") ) 275 .createCriteria("foo2.component.glarch") 276 .add( Expression.eq("name", "xxx") ) 277 .list(); 278 assertTrue( list.size()==0 ); 279 280 t.commit(); 281 s.close(); 282 283 s = openSession(); 284 t = s.beginTransaction(); 285 286 crit = s.createCriteria(Baz.class); 287 crit.createCriteria("topGlarchez") 288 .add( Expression.isNotNull("name") ); 289 crit.createCriteria("fooSet") 290 .add( Expression.isNull("null") ); 291 292 list = crit.list(); 293 assertTrue( list.size()==2 ); 294 baz = (Baz) crit.uniqueResult(); 295 assertTrue( Hibernate.isInitialized(baz.getTopGlarchez()) ); assertTrue( !Hibernate.isInitialized(baz.getFooSet()) ); 297 298 299 list = s.createCriteria(Child.class).setFetchMode("parent", FetchMode.JOIN).list(); 300 301 s.delete("from Glarch g"); 302 s.delete( s.get(Foo.class, foo1.getKey() ) ); 303 s.delete( s.get(Foo.class, foo2.getKey() ) ); 304 s.delete(baz); 305 t.commit(); 306 s.close(); 307 } 308 309 public void testClassWhere() throws Exception { 310 Session s = openSession(); 311 Transaction t = s.beginTransaction(); 312 Baz baz = new Baz(); 313 baz.setParts( new ArrayList () ); 314 Part p1 = new Part(); 315 p1.setDescription("xyz"); 316 Part p2 = new Part(); 317 p2.setDescription("abc"); 318 baz.getParts().add(p1); 319 baz.getParts().add(p2); 320 s.save(baz); 321 t.commit(); 322 s.close(); 323 324 s = openSession(); 325 t = s.beginTransaction(); 326 assertTrue( s.createCriteria(Part.class).list().size()==1 ); assertTrue( s.createCriteria(Part.class).add( Expression.eq( "id", p1.getId() ) ).list().size()==1 ); 328 assertTrue( s.createQuery("from Part").list().size()==1 ); 329 assertTrue( s.createQuery("from Baz baz join baz.parts").list().size()==2 ); 330 baz = (Baz) s.createCriteria(Baz.class).uniqueResult(); 331 assertTrue( s.createFilter( baz.getParts(), "" ).list().size()==2 ); 332 s.delete( s.get( Part.class, p1.getId() )); 334 s.delete( s.get( Part.class, p2.getId() )); 335 s.delete(baz); 336 t.commit(); 337 s.close(); 338 } 339 340 public void testClassWhereManyToMany() throws Exception { 341 Session s = openSession(); 342 Transaction t = s.beginTransaction(); 343 Baz baz = new Baz(); 344 baz.setMoreParts( new ArrayList () ); 345 Part p1 = new Part(); 346 p1.setDescription("xyz"); 347 Part p2 = new Part(); 348 p2.setDescription("abc"); 349 baz.getMoreParts().add(p1); 350 baz.getMoreParts().add(p2); 351 s.save(baz); 352 t.commit(); 353 s.close(); 354 355 s = openSession(); 356 t = s.beginTransaction(); 357 assertTrue( s.createCriteria(Part.class).list().size()==1 ); assertTrue( s.createCriteria(Part.class).add( Expression.eq( "id", p1.getId() ) ).list().size()==1 ); 359 assertTrue( s.createQuery("from Part").list().size()==1 ); 360 assertTrue( s.createQuery("from Baz baz join baz.moreParts").list().size()==2 ); 361 baz = (Baz) s.createCriteria(Baz.class).uniqueResult(); 362 assertTrue( s.createFilter( baz.getMoreParts(), "" ).list().size()==2 ); 363 s.delete( s.get( Part.class, p1.getId() )); 365 s.delete( s.get( Part.class, p2.getId() )); 366 s.delete(baz); 367 t.commit(); 368 s.close(); 369 } 370 371 public void testCollectionQuery() throws Exception { 372 Session s = openSession(); 373 Transaction t = s.beginTransaction(); 374 375 Simple s1 = new Simple(); 376 s1.setName("s"); 377 s1.setCount(0); 378 Simple s2 = new Simple(); 379 s2.setCount(2); 380 Simple s3 = new Simple(); 381 s3.setCount(3); 382 s.save( s1, new Long (1) ); s.save( s2, new Long (2) ); s.save( s3, new Long (3) ); 383 Container c = new Container(); 384 Contained cd = new Contained(); 385 List bag = new ArrayList (); 386 bag.add(cd); 387 c.setBag(bag); 388 List l = new ArrayList (); 389 l.add(s1); 390 l.add(s3); 391 l.add(s2); 392 c.setOneToMany(l); 393 l = new ArrayList (); 394 l.add(s1); 395 l.add(null); 396 l.add(s2); 397 c.setManyToMany(l); 398 s.save(c); 399 Container cx = new Container(); 400 s.save(cx); 401 Simple sx = new Simple(); 402 sx.setCount(5); 403 sx.setName("s"); 404 s.save( sx, new Long (5) ); 405 assertTrue( 406 s.find("select c from ContainerX c, Simple s where c.oneToMany[2] = s") 407 .size() == 1 408 ); 409 assertTrue( 410 s.find("select c from ContainerX c, Simple s where c.manyToMany[2] = s") 411 .size() == 1 412 ); 413 assertTrue( 414 s.find("select c from ContainerX c, Simple s where s = c.oneToMany[2]") 415 .size() == 1 416 ); 417 assertTrue( 418 s.find("select c from ContainerX c, Simple s where s = c.manyToMany[2]") 419 .size() == 1 420 ); 421 assertTrue( 422 s.find("select c from ContainerX c where c.oneToMany[0].name = 's'") 423 .size() == 1 424 ); 425 assertTrue( 426 s.find("select c from ContainerX c where c.manyToMany[0].name = 's'") 427 .size() == 1 428 ); 429 assertTrue( 430 s.find("select c from ContainerX c where 's' = c.oneToMany[2 - 2].name") 431 .size() == 1 432 ); 433 assertTrue( 434 s.find("select c from ContainerX c where 's' = c.manyToMany[(3+1)/4-1].name") 435 .size() == 1 436 ); 437 assertTrue( 438 s.find("select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'") 439 .size() == 1 440 ); 441 assertTrue( 442 s.find("select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'") 443 .size() == 1 444 ); 445 if ( ! ( getDialect() instanceof MySQLDialect ) && !(getDialect() instanceof org.hibernate.dialect.TimesTenDialect) ) { 446 assertTrue( 447 s.find("select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2") 448 .size() == 1 449 ); 450 } 451 assertTrue( s.contains(cd) ); 452 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) ) { 453 s.filter( c.getBag(), "where 0 in elements(this.bag)" ); 454 s.filter( c.getBag(), "where 0 in elements(this.lazyBag)" ); 455 } 456 s.find("select count(comp.name) from ContainerX c join c.components comp"); 457 s.delete(cd); 458 s.delete(c); 459 s.delete(s1); 460 s.delete(s2); 461 s.delete(s3); 462 s.delete(cx); 463 s.delete(sx); 464 t.commit(); 465 s.close(); 466 } 467 468 public void testParentChild() throws Exception { 469 Session s = openSession(); 470 Transaction t = s.beginTransaction(); 471 Parent p = new Parent(); 472 Child c = new Child(); 473 c.setParent(p); 474 p.setChild(c); 475 s.save(p); 476 s.save(c); 477 t.commit(); 478 s.close(); 479 480 s = openSession(); 481 t = s.beginTransaction(); 482 c = (Child) s.load( Child.class, new Long ( c.getId() ) ); 483 p = c.getParent(); 484 assertTrue( "1-1 parent", p!=null ); 485 c.setCount(32); 486 p.setCount(66); 487 t.commit(); 488 s.close(); 489 490 s = openSession(); 491 t = s.beginTransaction(); 492 c = (Child) s.load( Child.class, new Long ( c.getId() ) ); 493 p = c.getParent(); 494 assertTrue( "1-1 update", p.getCount()==66 ); 495 assertTrue( "1-1 update", c.getCount()==32 ); 496 assertTrue( 497 "1-1 query", 498 s.find("from Child c where c.parent.count=66").size()==1 499 ); 500 assertTrue( 501 "1-1 query", 502 ( (Object []) s.find("from Parent p join p.child c where p.count=66").get(0) ).length==2 503 ); 504 s.find("select c, c.parent from Child c order by c.parent.count"); 505 s.find("select c, c.parent from Child c where c.parent.count=66 order by c.parent.count"); 506 s.iterate("select c, c.parent, c.parent.count from Child c order by c.parent.count"); 507 assertTrue( 508 "1-1 query", 509 s.find("FROM Parent AS p WHERE p.count = ?", new Integer (66), Hibernate.INTEGER).size()==1 510 ); 511 s.delete(c); s.delete(p); 512 t.commit(); 513 s.close(); 514 } 515 516 public void testParentNullChild() throws Exception { 517 Session s = openSession(); 518 Transaction t = s.beginTransaction(); 519 Parent p = new Parent(); 520 s.save(p); 521 t.commit(); 522 s.close(); 523 524 s = openSession(); 525 t = s.beginTransaction(); 526 p = (Parent) s.load( Parent.class, new Long ( p.getId() ) ); 527 assertTrue( p.getChild()==null ); 528 p.setCount(66); 529 t.commit(); 530 s.close(); 531 532 s = openSession(); 533 t = s.beginTransaction(); 534 p = (Parent) s.load( Parent.class, new Long ( p.getId() ) ); 535 assertTrue( "null 1-1 update", p.getCount()==66 ); 536 assertTrue( p.getChild()==null ); 537 s.delete(p); 538 t.commit(); 539 s.close(); 540 } 541 542 public void testManyToMany() throws Exception { 543 544 if (getDialect() instanceof HSQLDialect) return; 545 546 Session s = openSession(); 547 Transaction t = s.beginTransaction(); 548 Container c = new Container(); 549 c.setManyToMany( new ArrayList () ); 550 c.setBag( new ArrayList () ); 551 Simple s1 = new Simple(); 552 Simple s2 = new Simple(); 553 s1.setCount(123); s2.setCount(654); 554 Contained c1 = new Contained(); 555 c1.setBag( new ArrayList () ); 556 c1.getBag().add(c); 557 c.getBag().add(c1); 558 c.getManyToMany().add(s1); 559 c.getManyToMany().add(s2); 560 Serializable cid = s.save(c); s.save(s1, new Long (12) ); s.save(s2, new Long (-1) ); 562 t.commit(); 563 s.close(); 564 565 s = openSession(); 566 t = s.beginTransaction(); 567 c = (Container) s.load(Container.class, cid); 568 assertTrue( c.getBag().size()==1 ); 569 assertTrue( c.getManyToMany().size()==2 ); 570 c1 = (Contained) c.getBag().iterator().next(); 571 assertTrue( c.getBag().size()==1 ); 572 c.getBag().remove(c1); 573 c1.getBag().remove(c); 574 assertTrue( c.getManyToMany().remove(0)!=null ); 575 t.commit(); 576 s.close(); 577 578 s = openSession(); 579 t = s.beginTransaction(); 580 c = (Container) s.load(Container.class, cid); 581 assertTrue( c.getBag().size()==0 ); 582 assertTrue( c.getManyToMany().size()==1 ); 583 c1 = (Contained) s.load( Contained.class, new Long (c1.getId()) ); 584 assertTrue( c1.getBag().size()==0 ); 585 assertTrue( s.delete("from ContainerX c")==1 ); 586 assertTrue( s.delete("from Contained")==1 ); 587 assertTrue( s.delete("from Simple")==2 ); 588 t.commit(); 589 s.close(); 590 } 591 592 public void testContainer() throws Exception { 593 Session s = openSession(); 594 Transaction t = s.beginTransaction(); 595 Container c = new Container(); 596 Simple x = new Simple(); x.setCount(123); 597 Simple y = new Simple(); y.setCount(456); 598 s.save( x, new Long (1) ); s.save( y, new Long (0) ); 599 List o2m = new ArrayList (); 600 o2m.add(x); o2m.add(null); o2m.add(y); 601 List m2m = new ArrayList (); 602 m2m.add(x); m2m.add(null); m2m.add(y); 603 c.setOneToMany(o2m); c.setManyToMany(m2m); 604 List comps = new ArrayList (); 605 Container.ContainerInnerClass ccic = new Container.ContainerInnerClass(); 606 ccic.setName("foo"); 607 ccic.setSimple(x); 608 comps.add(ccic); 609 comps.add(null); 610 ccic = new Container.ContainerInnerClass(); 611 ccic.setName("bar"); 612 ccic.setSimple(y); 613 comps.add(ccic); 614 HashSet compos = new HashSet (); 615 compos.add(ccic); 616 c.setComposites(compos); 617 c.setComponents(comps); 618 One one = new One(); 619 Many many = new Many(); 620 HashSet manies = new HashSet (); 621 manies.add(many); 622 one.setManies(manies); 623 many.setOne(one); 624 ccic.setMany(many); 625 ccic.setOne(one); 626 s.save(one); 627 s.save(many); 628 s.save(c); 629 t.commit(); 630 s.close(); 631 632 s = openSession(); 633 t = s.beginTransaction(); 634 Integer count = (Integer ) s.createQuery("select count(*) from ContainerX as c join c.components as ce join ce.simple as s where ce.name='foo'").uniqueResult(); 635 assertTrue( count.intValue()==1 ); 636 List res = s.find("select c, s from ContainerX as c join c.components as ce join ce.simple as s where ce.name='foo'"); 637 assertTrue(res.size()==1); 638 t.commit(); 639 s.close(); 640 641 s = openSession(); 642 t = s.beginTransaction(); 643 c = (Container) s.load( Container.class, new Long ( c.getId() ) ); 644 System.out.println( c.getOneToMany() ); 645 System.out.println( c.getManyToMany() ); 646 System.out.println( c.getComponents() ); 647 System.out.println( c.getComposites() ); 648 ccic = (Container.ContainerInnerClass) c.getComponents().get(2); 649 assertTrue( ccic.getMany().getOne()==ccic.getOne() ); 650 assertTrue( c.getComponents().size()==3 ); 651 assertTrue( c.getComposites().size()==1 ); 652 assertTrue( c.getOneToMany().size()==3 ); 653 assertTrue( c.getManyToMany().size()==3 ); 654 assertTrue( c.getOneToMany().get(0)!=null ); 655 assertTrue( c.getOneToMany().get(2)!=null ); 656 for ( int i=0; i<3; i++ ) { 657 assertTrue( c.getManyToMany().get(i) == c.getOneToMany().get(i) ); 658 } 659 Object o1 = c.getOneToMany().get(0); 660 Object o2 = c.getOneToMany().remove(2); 661 c.getOneToMany().set(0, o2); 662 c.getOneToMany().set(1, o1); 663 o1 = c.getComponents().remove(2); 664 c.getComponents().set(0, o1); 665 c.getManyToMany().set( 0, c.getManyToMany().get(2) ); 666 Container.ContainerInnerClass ccic2 = new Container.ContainerInnerClass(); 667 ccic2.setName("foo"); 668 ccic2.setOne(one); 669 ccic2.setMany(many); 670 ccic2.setSimple( (Simple) s.load(Simple.class, new Long (0) ) ); 671 c.getComposites().add(ccic2); 672 t.commit(); 673 s.close(); 674 675 s = openSession(); 676 t = s.beginTransaction(); 677 c = (Container) s.load( Container.class, new Long ( c.getId() ) ); 678 System.out.println( c.getOneToMany() ); 679 System.out.println( c.getManyToMany() ); 680 System.out.println( c.getComponents() ); 681 System.out.println( c.getComposites() ); 682 assertTrue( c.getComponents().size()==1 ); assertTrue( c.getComposites().size()==2 ); 684 assertTrue( c.getOneToMany().size()==2 ); 685 assertTrue( c.getManyToMany().size()==3 ); 686 assertTrue( c.getOneToMany().get(0)!=null ); 687 assertTrue( c.getOneToMany().get(1)!=null ); 688 ( (Container.ContainerInnerClass) c.getComponents().get(0) ).setName("a different name"); 689 ( (Container.ContainerInnerClass) c.getComposites().iterator().next() ).setName("once again"); 690 t.commit(); 691 s.close(); 692 693 s = openSession(); 694 t = s.beginTransaction(); 695 c = (Container) s.load( Container.class, new Long ( c.getId() ) ); 696 System.out.println( c.getOneToMany() ); 697 System.out.println( c.getManyToMany() ); 698 System.out.println( c.getComponents() ); 699 System.out.println( c.getComposites() ); 700 assertTrue( c.getComponents().size()==1 ); assertTrue( c.getComposites().size()==2 ); 702 assertTrue( ( (Container.ContainerInnerClass) c.getComponents().get(0) ).getName().equals("a different name") ); 703 Iterator iter = c.getComposites().iterator(); 704 boolean found = false; 705 while ( iter.hasNext() ) { 706 if ( ( (Container.ContainerInnerClass) iter.next() ).getName().equals("once again") ) found = true; 707 } 708 assertTrue(found); 709 c.getOneToMany().clear(); 710 c.getManyToMany().clear(); 711 c.getComposites().clear(); 712 c.getComponents().clear(); 713 s.delete("from Simple"); 714 s.delete("from Many"); 715 s.delete("from One"); 716 t.commit(); 717 s.close(); 718 719 s = openSession(); 720 t = s.beginTransaction(); 721 c = (Container) s.load( Container.class, new Long ( c.getId() ) ); 722 assertTrue( c.getComponents().size()==0 ); 723 assertTrue( c.getComposites().size()==0 ); 724 assertTrue( c.getOneToMany().size()==0 ); 725 assertTrue( c.getManyToMany().size()==0 ); 726 s.delete(c); 727 t.commit(); 728 s.close(); 729 } 730 731 public void testCascadeCompositeElements() throws Exception { 732 Container c = new Container(); 733 List list = new ArrayList (); 734 c.setCascades(list); 735 Container.ContainerInnerClass cic = new Container.ContainerInnerClass(); 736 cic.setMany( new Many() ); 737 cic.setOne( new One() ); 738 list.add(cic); 739 Session s = openSession(); 740 s.save(c); 741 s.flush(); 742 s.connection().commit(); 743 s.close(); 744 745 s=openSession(); 746 c = (Container) s.iterate("from ContainerX c").next(); 747 cic = (Container.ContainerInnerClass) c.getCascades().iterator().next(); 748 assertTrue( cic.getMany()!=null && cic.getOne()!=null ); 749 assertTrue( c.getCascades().size()==1 ); 750 s.delete(c); 751 s.flush(); 752 s.connection().commit(); 753 s.close(); 754 755 c = new Container(); 756 s = openSession(); 757 s.save(c); 758 list = new ArrayList (); 759 c.setCascades(list); 760 cic = new Container.ContainerInnerClass(); 761 cic.setMany( new Many() ); 762 cic.setOne( new One() ); 763 list.add(cic); 764 s.flush(); 765 s.connection().commit(); 766 s.close(); 767 768 s=openSession(); 769 c = (Container) s.iterate("from ContainerX c").next(); 770 cic = (Container.ContainerInnerClass) c.getCascades().iterator().next(); 771 assertTrue( cic.getMany()!=null && cic.getOne()!=null ); 772 assertTrue( c.getCascades().size()==1 ); 773 s.delete(c); 774 s.flush(); 775 s.connection().commit(); 776 s.close(); 777 } 778 779 public void testBag() throws Exception { 780 781 if (getDialect() instanceof HSQLDialect) return; 782 783 Session s = openSession(); 784 Transaction t = s.beginTransaction(); 785 Container c = new Container(); 786 Contained c1 = new Contained(); 787 Contained c2 = new Contained(); 788 c.setBag( new ArrayList () ); 789 c.getBag().add(c1); 790 c.getBag().add(c2); 791 c1.getBag().add(c); 792 c2.getBag().add(c); 793 s.save(c); 794 c.getBag().add(c2); 795 c2.getBag().add(c); 796 c.getLazyBag().add(c1); 797 c1.getLazyBag().add(c); 798 t.commit(); 799 s.close(); 800 801 s = openSession(); 802 t = s.beginTransaction(); 803 c = (Container) s.find("from ContainerX c").get(0); 804 c.getLazyBag().size(); 805 t.commit(); 806 s.close(); 807 808 s = openSession(); 809 t = s.beginTransaction(); 810 c = (Container) s.find("from ContainerX c").get(0); 811 Contained c3 = new Contained(); 812 c.getLazyBag().add(c3); 815 c3.getLazyBag().add(c); 816 t.commit(); 817 s.close(); 818 819 s = openSession(); 820 t = s.beginTransaction(); 821 c = (Container) s.find("from ContainerX c").get(0); 822 Contained c4 = new Contained(); 823 c.getLazyBag().add(c4); 824 c4.getLazyBag().add(c); 825 assertTrue( c.getLazyBag().size()==3 ); t.commit(); 828 s.close(); 829 830 s = openSession(); 831 t = s.beginTransaction(); 832 c = (Container) s.find("from ContainerX c").get(0); 833 Iterator i = c.getBag().iterator(); 834 int j=0; 835 while ( i.hasNext() ) { 836 assertTrue( i.next()!=null ); 837 j++; 838 } 839 assertTrue(j==3); 840 assertTrue( c.getLazyBag().size()==3 ); 841 s.delete(c); 842 c.getBag().remove(c2); 843 Iterator iter = c.getBag().iterator(); 844 j=0; 845 while ( iter.hasNext() ) { 846 j++; 847 s.delete( iter.next() ); 848 } 849 assertTrue(j==2); 850 s.delete( s.load(Contained.class, new Long ( c4.getId() ) ) ); 851 s.delete( s.load(Contained.class, new Long ( c3.getId() ) ) ); 852 t.commit(); 853 s.close(); 854 855 } 856 857 public void testCircularCascade() throws Exception { 858 Session s = openSession(); 859 Transaction tx = s.beginTransaction(); 860 Circular c = new Circular(); 861 c.setClazz(Circular.class); 862 c.setOther( new Circular() ); 863 c.getOther().setOther( new Circular() ); 864 c.getOther().getOther().setOther(c); 865 c.setAnyEntity( c.getOther() ); 866 String id = (String ) s.save(c); 867 tx.commit(); 868 s.close(); 869 s = openSession(); 870 tx = s.beginTransaction(); 871 c = (Circular) s.load(Circular.class, id); 872 c.getOther().getOther().setClazz(Foo.class); 873 tx.commit(); 874 s.close(); 875 c.getOther().setClazz(Qux.class); 876 s = openSession(); 877 tx = s.beginTransaction(); 878 s.saveOrUpdate(c); 879 tx.commit(); 880 s.close(); 881 c.getOther().getOther().setClazz(Bar.class); 882 s = openSession(); 883 tx = s.beginTransaction(); 884 s.saveOrUpdate(c); 885 tx.commit(); 886 s.close(); 887 s = openSession(); 888 tx = s.beginTransaction(); 889 c = (Circular) s.load(Circular.class, id); 890 assertTrue( c.getOther().getOther().getClazz()==Bar.class); 891 assertTrue( c.getOther().getClazz()==Qux.class); 892 assertTrue( c.getOther().getOther().getOther()==c); 893 assertTrue( c.getAnyEntity()==c.getOther() ); 894 assertTrue( s.delete("from Universe")==3 ); 895 tx.commit(); 896 s.close(); 897 } 898 899 public void testDeleteEmpty() throws Exception { 900 Session s = openSession(); 901 assertTrue( s.delete("from Simple")==0 ); 902 assertTrue( s.delete("from Universe")==0 ); 903 s.close(); 904 } 905 906 public void testLocking() throws Exception { 907 Session s = openSession(); 908 Transaction tx = s.beginTransaction(); 909 Simple s1 = new Simple(); s1.setCount(1); 910 Simple s2 = new Simple(); s2.setCount(2); 911 Simple s3 = new Simple(); s3.setCount(3); 912 Simple s4 = new Simple(); s4.setCount(4); 913 s.save(s1, new Long (1) ); 914 s.save(s2, new Long (2) ); 915 s.save(s3, new Long (3) ); 916 s.save(s4, new Long (4) ); 917 assertTrue( s.getCurrentLockMode(s1)==LockMode.WRITE ); 918 tx.commit(); 919 s.close(); 920 921 s = openSession(); 922 tx = s.beginTransaction(); 923 s1 = (Simple) s.load(Simple.class, new Long (1), LockMode.NONE); 924 assertTrue( s.getCurrentLockMode(s1)==LockMode.READ || s.getCurrentLockMode(s1)==LockMode.NONE ); s2 = (Simple) s.load(Simple.class, new Long (2), LockMode.READ); 926 assertTrue( s.getCurrentLockMode(s2)==LockMode.READ ); 927 s3 = (Simple) s.load(Simple.class, new Long (3), LockMode.UPGRADE); 928 assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE ); 929 s4 = (Simple) s.get(Simple.class, new Long (4), LockMode.UPGRADE_NOWAIT); 930 assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT ); 931 932 s1 = (Simple) s.load(Simple.class, new Long (1), LockMode.UPGRADE); assertTrue( s.getCurrentLockMode(s1)==LockMode.UPGRADE ); 934 s2 = (Simple) s.load(Simple.class, new Long (2), LockMode.NONE); 935 assertTrue( s.getCurrentLockMode(s2)==LockMode.READ ); 936 s3 = (Simple) s.load(Simple.class, new Long (3), LockMode.READ); 937 assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE ); 938 s4 = (Simple) s.load(Simple.class, new Long (4), LockMode.UPGRADE); 939 assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT ); 940 941 s.lock(s2, LockMode.UPGRADE); assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE ); 943 s.lock(s3, LockMode.UPGRADE); 944 assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE ); 945 s.lock(s1, LockMode.UPGRADE_NOWAIT); 946 s.lock(s4, LockMode.NONE); 947 assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT ); 948 949 tx.commit(); 950 tx = s.beginTransaction(); 951 952 assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE ); 953 assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE ); 954 assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE ); 955 assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE ); 956 957 s.lock(s1, LockMode.READ); assertTrue( s.getCurrentLockMode(s1)==LockMode.READ ); 959 s.lock(s2, LockMode.UPGRADE); assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE ); 961 s.lock(s3, LockMode.UPGRADE_NOWAIT); assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE_NOWAIT ); 963 s.lock(s4, LockMode.NONE); 964 assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE ); 965 966 s4.setName("s4"); 967 s.flush(); 968 assertTrue( s.getCurrentLockMode(s4)==LockMode.WRITE ); 969 tx.commit(); 970 971 tx = s.beginTransaction(); 972 973 assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE ); 974 assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE ); 975 assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE ); 976 assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE ); 977 978 s.delete(s1); s.delete(s2); s.delete(s3); s.delete(s4); 979 tx.commit(); 980 s.close(); 981 } 982 983 public void testObjectType() throws Exception { 984 Session s = openSession(); 985 Parent g = new Parent(); 986 Foo foo = new Foo(); 987 g.setAny(foo); 988 s.save(g); 989 s.save(foo); 990 s.flush(); 991 s.connection().commit(); 992 s.close(); 993 s = openSession(); 994 g = (Parent) s.load( Parent.class, new Long ( g.getId() ) ); 995 assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy ); 996 s.delete( g.getAny() ); 997 s.delete(g); 998 s.flush(); 999 s.connection().commit(); 1000 s.close(); 1001 } 1002 1003 public void testLoadAfterNonExists() throws HibernateException, SQLException { 1004 1005 Session session = openSession(); 1006 if ( (getDialect() instanceof MySQLDialect) ) { 1007 session.connection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 1008 } 1009 1010 try { 1012 session.load( Simple.class, new Long (-1) ); 1013 fail(); 1014 } 1015 catch(ObjectNotFoundException onfe) { 1016 } 1018 1019 Session anotherSession = getSessions().openSession(); 1021 Simple myNewSimple = new Simple(); 1022 myNewSimple.setName("My under the radar Simple entity"); 1023 myNewSimple.setAddress("SessionCacheTest.testLoadAfterNonExists"); 1024 myNewSimple.setCount(1); 1025 myNewSimple.setDate( new Date () ); 1026 myNewSimple.setPay( new Float (100000000) ); 1027 anotherSession.save( myNewSimple, new Long (-1) ); 1028 anotherSession.flush(); 1029 anotherSession.connection().commit(); 1030 anotherSession.close(); 1031 1032 session.load( Simple.class, new Long (-1) ); 1035 1039 1040 session.clear(); 1043 try { 1044 Simple dummy = (Simple) session.load( Simple.class, new Long (-1) ); 1045 assertNotNull("Unable to locate entity Simple with id = -1", dummy); 1046 } 1047 catch(ObjectNotFoundException onfe) { 1048 fail("Unable to locate entity Simple with id = -1"); 1049 } 1050 session.connection().commit(); 1051 session.close(); 1052 } 1053 1054 public static Test suite() { 1055 return new TestSuite(ParentChildTest.class); 1056 } 1057 1058 public String [] getMappings() { 1059 return new String [] { 1060 "legacy/ParentChild.hbm.xml", 1061 "legacy/FooBar.hbm.xml", 1062 "legacy/Baz.hbm.xml", 1063 "legacy/Qux.hbm.xml", 1064 "legacy/Glarch.hbm.xml", 1065 "legacy/Fum.hbm.xml", 1066 "legacy/Fumm.hbm.xml", 1067 "legacy/Fo.hbm.xml", 1068 "legacy/One.hbm.xml", 1069 "legacy/Many.hbm.xml", 1070 "legacy/Immutable.hbm.xml", 1071 "legacy/Fee.hbm.xml", 1072 "legacy/Vetoer.hbm.xml", 1073 "legacy/Holder.hbm.xml", 1074 "legacy/Simple.hbm.xml", 1075 "legacy/Container.hbm.xml", 1076 "legacy/Circular.hbm.xml", 1077 "legacy/Stuff.hbm.xml" 1078 }; 1079 } 1080 1081 public static void main(String [] args) throws Exception { 1082 TestRunner.run( suite() ); 1083 } 1084 1085} 1086 | Popular Tags |