1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Date ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Set ; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 16 import org.hibernate.Criteria; 17 import org.hibernate.FetchMode; 18 import org.hibernate.LockMode; 19 import org.hibernate.classic.Session; 20 import org.hibernate.criterion.Expression; 21 import org.hibernate.Transaction; 22 import org.hibernate.dialect.HSQLDialect; 23 import org.hibernate.dialect.MySQLDialect; 24 import org.hibernate.dialect.SybaseDialect; 25 import org.hibernate.test.TestCase; 26 27 public class MultiTableTest extends TestCase { 28 29 public MultiTableTest(String arg0) { 30 super(arg0); 31 } 32 33 public void testCriteria() throws Exception { 34 Session s = openSession(); 35 Lower l = new Lower(); 36 s.save(l); 37 assertTrue( l==s.createCriteria(Top.class).uniqueResult() ); 38 s.delete(l); 39 s.flush(); 40 Criteria c = s.createCriteria(Lower.class); 41 c.createCriteria("yetanother") 42 .add( Expression.isNotNull("id") ) 43 .createCriteria("another"); 44 c.createCriteria("another").add( Expression.isNotNull("id") ); 45 c.list(); 46 s.connection().commit(); 47 s.close(); 48 } 49 50 public void testFetchOneToMany() throws Exception { 51 Session s = openSession(); 52 s.createCriteria(Po.class).setFetchMode("set", FetchMode.EAGER).list(); 53 s.createCriteria(Po.class).setFetchMode("list", FetchMode.EAGER).list(); 54 s.connection().commit(); 55 s.close(); 56 } 57 58 public void testNarrow() throws Exception { 59 Session s = openSession(); 60 s.find("from Po po, Lower low where low.mypo = po"); 61 s.find("from Po po join po.set as sm where sm.amount > 0"); 62 s.find("from Po po join po.top as low where low.foo = 'po'"); 63 s.connection().commit(); 64 s.close(); 65 } 66 67 public void testJoins() throws Exception { 68 Session s = openSession(); 69 s.find("from Lower l join l.yetanother l2 where lower(l2.name) > 'a'"); 70 s.find("from Lower l where lower(l.yetanother.top.name) > 'a'"); 71 s.find("from SubMulti sm join sm.children smc where smc.name > 'a'"); 72 s.find("select s, ya from Lower s join s.yetanother ya"); 73 s.find("from Lower s1 join s1.bag s2"); 74 s.find("from Lower s1 left join s1.bag s2"); 75 s.find("select s, a from Lower s join s.another a"); 76 s.find("select s, a from Lower s left join s.another a"); 77 s.find("from Top s, Lower ls"); 78 s.find("from Lower ls join ls.set s where s.name > 'a'"); 79 s.find("from Po po join po.list sm where sm.name > 'a'"); 80 s.find("from Lower ls inner join ls.another s where s.name is not null"); 81 s.find("from Lower ls where ls.other.another.name is not null"); 82 s.find("from Multi m where m.derived like 'F%'"); 83 s.find("from SubMulti m where m.derived like 'F%'"); 84 s.connection().commit(); 85 s.close(); 86 } 87 88 public void testSubclassCollection() throws Exception { 89 Session s = openSession(); 91 SubMulti sm = new SubMulti(); 92 SubMulti sm1 = new SubMulti(); 93 SubMulti sm2 = new SubMulti(); 94 ArrayList list = new ArrayList (); 95 ArrayList anotherList = new ArrayList (); 96 sm.setChildren(list); 97 sm.setMoreChildren(anotherList); 98 sm.setExtraProp("foo"); 99 list.add(sm1); 100 list.add(sm2); 101 anotherList.add(sm1); 102 anotherList.add(sm2); 103 sm1.setParent(sm); 104 sm2.setParent(sm); 105 Serializable id = s.save(sm); 106 s.save(sm1); 107 s.save(sm2); 108 s.flush(); 109 s.connection().commit(); 110 s.close(); 111 112 getSessions().evict(SubMulti.class); 113 114 s = openSession(); 115 s.connection().createStatement().executeQuery( 116 "select * from leafsubsubclass sm, nonleafsubclass m, rootclass s where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1" 117 ).next(); 118 assertTrue( s.find("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==2 ); 119 s.find("select c from SubMulti sm join sm.children c"); 120 assertTrue( s.find("select elements(sm.children) from SubMulti as sm").size()==2 ); 121 assertTrue( s.find("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==1 ); 122 sm = (SubMulti) s.load(SubMulti.class, id); 123 assertTrue( sm.getChildren().size()==2 ); 124 assertEquals( 125 s.filter( sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null" ).iterator().next(), 126 new Integer (2) 127 ); 128 assertEquals( "FOO", sm.getDerived() ); 129 assertSame( 130 s.iterate("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").next(), 131 sm 132 ); 133 assertTrue( sm.getMoreChildren().size()==2 ); 134 s.delete(sm); 135 Iterator iter = sm.getChildren().iterator(); 136 while ( iter.hasNext() ) s.delete( iter.next() ); 137 s.flush(); 138 s.connection().commit(); 139 s.close(); 140 141 } 142 143 public void testCollectionOnly() throws Exception { 144 Session s = openSession(); 145 Transaction t = s.beginTransaction(); 146 Mono m = new Mono(); 147 Long id = (Long ) s.save(m); 148 t.commit(); 149 s.close(); 150 s = openSession(); 151 t = s.beginTransaction(); 152 s.update(m, id); 153 s.flush(); 154 m.setAddress("foo bar"); 155 s.flush(); 156 s.delete(m); 157 t.commit(); 158 s.close(); 159 } 160 161 public void testQueries() throws Exception { 162 163 Session s = openSession(); 164 Long id = new Long (1); 165 if (getDialect() instanceof SybaseDialect) { 166 id = (Long ) s.save( new TrivialClass() ); 167 } 168 else{ 169 s.save( new TrivialClass(), id ); 170 } 171 172 s.flush(); 173 s.connection().commit(); 174 s.close(); 175 s = openSession(); 176 TrivialClass tc = (TrivialClass) s.load(TrivialClass.class, id); 177 s.find("from TrivialClass s where s.id = 2"); 178 s.find("select t.count from Top t"); 179 s.find("from Lower s where s.another.name='name'"); 180 s.find("from Lower s where s.yetanother.name='name'"); 181 s.find("from Lower s where s.yetanother.name='name' and s.yetanother.foo is null"); 182 s.find("from Top s where s.count=1"); 183 s.find("select s.count from Top s, Lower ls where ls.another=s"); 184 s.find("select elements(ls.bag), elements(ls.set) from Lower ls"); 185 s.iterate("from Lower"); 186 s.iterate("from Top"); 187 s.delete(tc); 188 s.flush(); 189 s.connection().commit(); 190 s.close(); 191 } 192 193 public void testConstraints() throws Exception { 194 195 Session s = openSession(); 196 Transaction t = s.beginTransaction(); 197 SubMulti sm = new SubMulti(); 198 sm.setAmount(66.5f); 199 if ( getDialect() instanceof SybaseDialect ) { 200 s.save(sm); 201 } 202 else { 203 s.save( sm, new Long (2) ); 204 } 205 t.commit(); 206 s.close(); 207 s = openSession(); 208 s.delete( "from SubMulti" ); 209 t = s.beginTransaction(); 210 t.commit(); 211 s.close(); 212 } 213 214 public void testMultiTable() throws Exception { 215 216 Session s = openSession(); 217 Transaction t = s.beginTransaction(); 218 Multi multi = new Multi(); 219 multi.setExtraProp("extra"); 220 multi.setName("name"); 222 Top simp = new Top(); 223 simp.setDate( new Date () ); 224 simp.setName("simp"); 225 Serializable mid; 227 Serializable sid; 228 if ( getDialect() instanceof SybaseDialect ) { 229 mid = s.save(multi); 230 sid = s.save(simp); 231 } 232 else { 233 mid = new Long (123); 234 s.save(multi, mid); 235 sid = new Long (1234); 236 s.save(simp, sid); 237 } 238 SubMulti sm = new SubMulti(); 239 sm.setAmount(66.5f); 240 Serializable smid; 241 if (getDialect() instanceof SybaseDialect) { 242 smid = s.save(sm); 243 } 244 else { 245 smid = new Long (2); 246 s.save(sm, smid); 247 } 248 t.commit(); 249 s.close(); 250 251 s = openSession(); 252 t = s.beginTransaction(); 253 multi.setExtraProp( multi.getExtraProp() + "2" ); 254 multi.setName("new name"); 256 s.update(multi, mid); 257 simp.setName("new name"); 258 s.update(simp, sid); 259 sm.setAmount(456.7f); 260 s.update(sm, smid); 261 t.commit(); 262 s.close(); 263 264 s = openSession(); 265 t = s.beginTransaction(); 266 multi = (Multi) s.load(Multi.class, mid); 267 assertTrue( multi.getExtraProp().equals("extra2") ); 268 multi.setExtraProp( multi.getExtraProp() + "3" ); 269 assertTrue( multi.getName().equals("new name") ); 271 multi.setName("newer name"); 272 sm = (SubMulti) s.load(SubMulti.class, smid); 273 assertTrue( sm.getAmount()==456.7f ); 274 sm.setAmount(23423f); 275 t.commit(); 276 s.close(); 277 278 s = openSession(); 279 t = s.beginTransaction(); 280 multi = (Multi) s.load(Top.class, mid); 281 simp = (Top) s.load(Top.class, sid); 282 assertTrue( ! (simp instanceof Multi) ); 283 assertTrue( multi.getExtraProp().equals("extra23") ); 284 assertTrue( multi.getName().equals("newer name") ); 286 t.commit(); 287 s.close(); 288 289 s = openSession(); 290 t = s.beginTransaction(); 291 Iterator iter = s.iterate("select\n\nt from Top t where t.count>0"); 292 boolean foundSimp = false; 293 boolean foundMulti = false; 294 boolean foundSubMulti = false; 295 while ( iter.hasNext() ) { 296 Object o = iter.next(); 297 if ( ( o instanceof Top ) && !( o instanceof Multi) ) foundSimp = true; 298 if ( o instanceof Multi && !(o instanceof SubMulti) ) foundMulti = true; 299 if ( o instanceof SubMulti ) foundSubMulti = true; 300 } 301 assertTrue( foundSimp&&foundMulti&&foundSubMulti ); 302 s.find("from Multi m where m.count>0 and m.extraProp is not null"); 303 s.find("from Top m where m.count>0 and m.name is not null"); 304 s.find("from Lower m where m.other is not null"); 305 s.find("from Multi m where m.other.id = 1"); 306 s.find("from SubMulti m where m.amount > 0.0"); 307 308 assertTrue( 309 s.find("from Multi").size()==2 310 ); 311 assertTrue( 312 s.find("from Multi m where m.class = SubMulti").size()==1 313 ); 314 assertTrue( 315 s.find("from Top m where m.class = Multi").size()==1 316 ); 317 assertTrue( 318 s.find("from Top").size()==3 319 ); 320 assertTrue( 321 s.find("from Lower").size()==0 322 ); 323 assertTrue( 324 s.find("from SubMulti").size()==1 325 ); 326 327 s.find("from Lower ls join ls.bag s where s.id is not null"); 328 s.find("from Lower ls join ls.set s where s.id is not null"); 329 if ( !(getDialect() instanceof MySQLDialect) ) s.find("from SubMulti sm where exists elements(sm.children)"); 330 331 List l = s.createCriteria(Top.class).list(); 332 assertTrue( l.size()==3 ); 333 assertTrue( s.createCriteria(SubMulti.class).list().size()==1 ); 334 assertTrue( 335 s.createCriteria(SubMulti.class) 336 .add( Expression.lt("amount", new Float (0)) ) 337 .list() 338 .size()==0 339 ); 340 assertTrue( 341 s.createCriteria(SubMulti.class) 342 .add( Expression.ge("amount", new Float (0)) ) 343 .list() 344 .size()==1 345 ); 346 347 t.commit(); 348 s.close(); 349 350 s = openSession(); 351 t = s.beginTransaction(); 352 multi = (Multi) s.load(Top.class, mid, LockMode.UPGRADE); 353 simp = (Top) s.load(Top.class, sid); 354 s.lock(simp, LockMode.UPGRADE_NOWAIT); 355 t.commit(); 356 s.close(); 357 358 s = openSession(); 359 t = s.beginTransaction(); 360 s.update(multi, mid); 361 s.delete(multi); 362 assertTrue( s.delete("from Top")==2); 363 t.commit(); 364 s.close(); 365 366 } 367 368 public void testMultiTableGeneratedId() throws Exception { 369 370 Session s = openSession(); 371 Transaction t = s.beginTransaction(); 372 Multi multi = new Multi(); 373 multi.setExtraProp("extra"); 374 multi.setName("name"); 376 Top simp = new Top(); 377 simp.setDate( new Date () ); 378 simp.setName("simp"); 379 Serializable multiId = s.save( multi ); 381 Serializable simpId = s.save( simp ); 382 SubMulti sm = new SubMulti(); 383 sm.setAmount(66.5f); 384 Serializable smId = s.save( sm ); 385 t.commit(); 386 s.close(); 387 388 s = openSession(); 389 t = s.beginTransaction(); 390 multi.setExtraProp( multi.getExtraProp() + "2" ); 391 multi.setName("new name"); 393 s.update( multi, multiId ); 394 simp.setName("new name"); 395 s.update( simp, simpId ); 396 sm.setAmount(456.7f); 397 s.update( sm, smId ); 398 t.commit(); 399 s.close(); 400 401 s = openSession(); 402 t = s.beginTransaction(); 403 multi = (Multi) s.load( Multi.class, multiId ); 404 assertTrue( multi.getExtraProp().equals("extra2") ); 405 multi.setExtraProp( multi.getExtraProp() + "3" ); 406 assertTrue( multi.getName().equals("new name") ); 408 multi.setName("newer name"); 409 sm = (SubMulti) s.load( SubMulti.class, smId ); 410 assertTrue( sm.getAmount()==456.7f ); 411 sm.setAmount(23423f); 412 t.commit(); 413 s.close(); 414 415 s = openSession(); 416 t = s.beginTransaction(); 417 multi = (Multi) s.load( Top.class, multiId ); 418 simp = (Top) s.load( Top.class, simpId ); 419 assertTrue( ! (simp instanceof Multi) ); 420 assertTrue( multi.getExtraProp().equals("extra23") ); 421 assertTrue( multi.getName().equals("newer name") ); 423 t.commit(); 424 s.close(); 425 426 s = openSession(); 427 t = s.beginTransaction(); 428 Iterator iter = s.iterate("select\n\nt from Top t where t.count>0"); 429 boolean foundSimp = false; 430 boolean foundMulti = false; 431 boolean foundSubMulti = false; 432 while ( iter.hasNext() ) { 433 Object o = iter.next(); 434 if ( ( o instanceof Top ) && !( o instanceof Multi) ) foundSimp = true; 435 if ( o instanceof Multi && !(o instanceof SubMulti) ) foundMulti = true; 436 if ( o instanceof SubMulti ) foundSubMulti = true; 437 } 438 assertTrue( foundSimp&&foundMulti&&foundSubMulti ); 439 s.find("from Multi m where m.count>0 and m.extraProp is not null"); 440 s.find("from Top m where m.count>0 and m.name is not null"); 441 s.find("from Lower m where m.other is not null"); 442 s.find("from Multi m where m.other.id = 1"); 443 s.find("from SubMulti m where m.amount > 0.0"); 444 445 assertTrue( 446 s.find("from Multi").size()==2 447 ); 448 451 assertTrue( 452 s.find("from Top").size()==3 453 ); 454 assertTrue( 455 s.find("from Lower").size()==0 456 ); 457 assertTrue( 458 s.find("from SubMulti").size()==1 459 ); 460 461 s.find("from Lower ls join ls.bag s where s.id is not null"); 462 if ( !(getDialect() instanceof MySQLDialect) ) s.find("from SubMulti sm where exists elements(sm.children)"); 463 464 t.commit(); 465 s.close(); 466 467 s = openSession(); 468 t = s.beginTransaction(); 469 multi = (Multi) s.load( Top.class, multiId, LockMode.UPGRADE ); 470 simp = (Top) s.load( Top.class, simpId ); 471 s.lock(simp, LockMode.UPGRADE_NOWAIT); 472 t.commit(); 473 s.close(); 474 475 s = openSession(); 476 t = s.beginTransaction(); 477 s.update( multi, multiId ); 478 s.delete(multi); 479 assertTrue( s.delete("from Top")==2); 480 t.commit(); 481 s.close(); 482 483 } 484 485 public void testMultiTableCollections() throws Exception { 486 487 if ( getDialect() instanceof HSQLDialect ) return; 488 489 Session s = openSession(); 490 Transaction t = s.beginTransaction(); 491 assertTrue( s.find("from Top").size()==0 ); 492 Multi multi = new Multi(); 493 multi.setExtraProp("extra"); 494 multi.setName("name"); 496 Top simp = new Top(); 497 simp.setDate( new Date () ); 498 simp.setName("simp"); 499 Serializable mid; 501 Serializable sid; 502 if ( getDialect() instanceof SybaseDialect ) { 503 mid = s.save(multi); 504 sid = s.save(simp); 505 } 506 else { 507 mid = new Long (123); 508 sid = new Long (1234); 509 s.save(multi, mid); 510 s.save(simp, sid); 511 } 512 Lower ls = new Lower(); 513 ls.setOther(ls); 514 ls.setAnother(ls); 515 ls.setYetanother(ls); 516 ls.setName("Less Simple"); 517 Set set = new HashSet (); 518 ls.setSet(set); 519 set.add(multi); 520 set.add(simp); 521 Serializable id; 522 if ( getDialect() instanceof SybaseDialect ) { 523 id = s.save(ls); 524 } 525 else { 526 id = new Long (2); 527 s.save( ls, new Long (2) ); 528 } 529 t.commit(); 530 s.close(); 531 assertTrue( ls.getOther()==ls && ls.getAnother()==ls && ls.getYetanother()==ls ); 532 533 s = openSession(); 534 t = s.beginTransaction(); 535 ls = (Lower) s.load(Lower.class, id); 536 assertTrue( ls.getOther()==ls && ls.getAnother()==ls && ls.getYetanother()==ls ); 537 assertTrue( ls.getSet().size()==2 ); 538 Iterator iter = ls.getSet().iterator(); 539 int foundMulti = 0; 540 int foundSimple = 0; 541 while ( iter.hasNext() ) { 542 Object o = iter.next(); 543 if ( o instanceof Top ) foundSimple++; 544 if ( o instanceof Multi ) foundMulti++; 545 } 546 assertTrue( foundSimple==2 && foundMulti==1 ); 547 assertTrue( s.delete("from Top")==3 ); 548 t.commit(); 549 s.close(); 550 } 551 552 public void testMultiTableManyToOne() throws Exception { 553 554 if ( getDialect() instanceof HSQLDialect ) return; 555 556 Session s = openSession(); 557 Transaction t = s.beginTransaction(); 558 assertTrue( s.find("from Top").size()==0 ); 559 Multi multi = new Multi(); 560 multi.setExtraProp("extra"); 561 multi.setName("name"); 563 Top simp = new Top(); 564 simp.setDate( new Date () ); 565 simp.setName("simp"); 566 Serializable mid; 568 if ( getDialect() instanceof SybaseDialect ) { 569 mid = s.save(multi); 570 } 571 else { 572 mid = new Long (123); 573 s.save(multi, mid); 574 } 575 Lower ls = new Lower(); 576 ls.setOther(ls); 577 ls.setAnother(multi); 578 ls.setYetanother(ls); 579 ls.setName("Less Simple"); 580 Serializable id; 581 if ( getDialect() instanceof SybaseDialect ) { 582 id = s.save(ls); 583 } 584 else { 585 id = new Long (2); 586 s.save( ls, new Long (2) ); 587 } 588 t.commit(); 589 s.close(); 590 assertTrue( ls.getOther()==ls && ls.getAnother()==multi && ls.getYetanother()==ls ); 591 592 s = openSession(); 593 t = s.beginTransaction(); 594 ls = (Lower) s.load(Lower.class, id); 595 assertTrue( ls.getOther()==ls && ls.getYetanother()==ls ); 596 assertTrue( ls.getAnother().getName().equals("name") && ls.getAnother() instanceof Multi ); 597 s.delete(ls); 598 s.delete( ls.getAnother() ); 599 t.commit(); 600 s.close(); 601 } 602 603 public void testMultiTableNativeId() throws Exception { 604 Session s = openSession(); 605 Transaction t = s.beginTransaction(); 606 Multi multi = new Multi(); 607 multi.setExtraProp("extra"); 608 Long id = (Long ) s.save(multi); 609 assertTrue( id!=null ); 610 s.delete(multi); 611 t.commit(); 612 s.close(); 613 } 614 615 public void testCollection() throws Exception { 616 Session s = openSession(); 617 Transaction t = s.beginTransaction(); 618 Multi multi1 = new Multi(); 619 multi1.setExtraProp("extra1"); 620 Multi multi2 = new Multi(); 621 multi2.setExtraProp("extra2"); 622 Po po = new Po(); 623 multi1.setPo(po); multi2.setPo(po); 624 po.setSet( new HashSet () ); 625 po.getSet().add(multi1); 626 po.getSet().add(multi2); 627 po.setList( new ArrayList () ); 628 po.getList().add( new SubMulti() ); 630 Serializable id = s.save(po); 631 assertTrue( id!=null ); 632 t.commit(); 633 s.close(); 634 s = openSession(); 635 t = s.beginTransaction(); 636 po = (Po) s.load(Po.class, id); 637 assertTrue( po.getSet().size()==2 ); 638 assertTrue( po.getList().size()==1 ); 639 s.delete(po); 640 assertTrue( s.find("from Top").size()==0 ); 641 t.commit(); 642 s.close(); 643 } 644 645 public void testOneToOne() throws Exception { 646 Session s = openSession(); 647 Lower ls = new Lower(); 648 Serializable id = s.save(ls); 649 s.flush(); 650 s.connection().commit(); 651 s.close(); 652 s = openSession(); 653 s.load(Lower.class, id); 654 s.connection().commit(); 655 s.close(); 656 s = openSession(); 657 s.delete( s.load(Lower.class, id) ); 658 s.flush(); 659 s.connection().commit(); 660 s.close(); 661 } 662 663 public void testCollectionPointer() throws Exception { 664 Session sess = openSession(); 665 Lower ls = new Lower(); 666 List list = new ArrayList (); 667 ls.setBag(list); 668 Top s = new Top(); 669 Serializable id = sess.save(ls); 670 sess.save(s); 671 sess.flush(); 672 list.add(s); 673 sess.flush(); 674 sess.connection().commit(); 675 sess.close(); 676 677 sess = openSession(); 678 ls = (Lower) sess.load(Lower.class, id); 679 assertTrue( ls.getBag().size()==1 ); 680 sess.delete("from java.lang.Object"); 681 sess.flush(); 682 sess.connection().commit(); 683 sess.close(); 684 } 685 686 public String [] getMappings() { 687 return new String [] { "legacy/Multi.hbm.xml", "legacy/MultiExtends.hbm.xml" }; 688 } 689 690 public static Test suite() { 691 return new TestSuite(MultiTableTest.class); 692 } 693 694 public static void main(String [] args) throws Exception { 695 TestRunner.run( suite() ); 696 } 697 } 698 | Popular Tags |