1 package org.hibernate.test.onetomany; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 import org.hibernate.test.TestCase; 10 11 14 public class OneToManyTest extends TestCase { 15 16 public OneToManyTest(String str) { 17 super(str); 18 } 19 20 public void testOneToManyLinkTable() { 21 Session s = openSession(); 22 Transaction t = s.beginTransaction(); 23 Child c = new Child(); 24 c.setName("Child One"); 25 Parent p = new Parent(); 26 p.setName("Parent"); 27 p.getChildren().add(c); 28 c.setParent(p); 29 s.save(p); 30 s.flush(); 31 32 p.getChildren().remove(c); 33 c.setParent(null); 34 s.flush(); 35 36 p.getChildren().add(c); 37 c.setParent(p); 38 t.commit(); 39 s.close(); 40 41 s = openSession(); 42 t = s.beginTransaction(); 43 c.setParent(null); 44 s.update(c); 45 t.commit(); 46 s.close(); 47 48 s = openSession(); 49 t = s.beginTransaction(); 50 c.setParent(p); 51 s.update(c); 52 t.commit(); 53 s.close(); 54 55 56 s = openSession(); 57 t = s.beginTransaction(); 58 c = (Child) s.createQuery("from Child").uniqueResult(); 59 s.createQuery("from Child c left join fetch c.parent").list(); 60 s.createQuery("from Child c inner join fetch c.parent").list(); 61 s.clear(); 62 p = (Parent) s.createQuery("from Parent p left join fetch p.children").uniqueResult(); 63 t.commit(); 64 s.close(); 65 } 66 67 public void testManyToManySize() { 68 Session s = openSession(); 69 Transaction t = s.beginTransaction(); 70 assertEquals( 0, s.createQuery("from Parent p where size(p.children) = 0").list().size() ); 72 assertEquals( 0, s.createQuery("from Parent p where p.children.size = 0").list().size() ); 73 t.commit(); 74 s.close(); 75 } 76 77 78 protected String [] getMappings() { 79 return new String [] { "onetomany/Parent.hbm.xml" }; 80 } 81 82 public static Test suite() { 83 return new TestSuite(OneToManyTest.class); 84 } 85 86 } 87 88 | Popular Tags |