1 package org.hibernate.test.ternary; 3 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 13 import org.hibernate.Hibernate; 14 import org.hibernate.Session; 15 import org.hibernate.Transaction; 16 import org.hibernate.test.TestCase; 17 18 21 public class TernaryTest extends TestCase { 22 23 public TernaryTest(String str) { 24 super(str); 25 } 26 27 public void testTernary() { 28 Session s = openSession(); 29 Transaction t = s.beginTransaction(); 30 Employee bob = new Employee("Bob"); 31 Employee tom = new Employee("Tom"); 32 Employee jim = new Employee("Jim"); 33 Employee tim = new Employee("Tim"); 34 Site melb = new Site("Melbourne"); 35 Site geel = new Site("Geelong"); 36 s.persist(bob); 37 s.persist(tom); 38 s.persist(jim); 39 s.persist(tim); 40 s.persist(melb); 41 s.persist(geel); 42 bob.getManagerBySite().put(melb, tom); 43 bob.getManagerBySite().put(geel, jim); 44 tim.getManagerBySite().put(melb, tom); 45 t.commit(); 46 s.close(); 47 48 s = openSession(); 49 t = s.beginTransaction(); 50 tom = (Employee) s.get(Employee.class, "Tom"); 51 assertFalse( Hibernate.isInitialized(tom.getUnderlings()) ); 52 assertEquals( tom.getUnderlings().size(), 2 ); 53 bob = (Employee) s.get(Employee.class, "Bob"); 54 assertFalse( Hibernate.isInitialized(bob.getManagerBySite()) ); 55 assertTrue( tom.getUnderlings().contains(bob) ); 56 melb = (Site) s.get(Site.class, "Melbourne"); 57 assertSame( bob.getManagerBySite().get(melb), tom ); 58 assertTrue( melb.getEmployees().contains(bob) ); 59 assertTrue( melb.getManagers().contains(tom) ); 60 t.commit(); 61 s.close(); 62 63 s = openSession(); 64 t = s.beginTransaction(); 65 List l = s.createQuery("from Employee e join e.managerBySite m where m.name='Bob'").list(); 66 assertEquals( l.size(), 0 ); 67 l = s.createQuery("from Employee e join e.managerBySite m where m.name='Tom'").list(); 68 assertEquals( l.size(), 2 ); 69 t.commit(); 70 s.close(); 71 72 s = openSession(); 73 t = s.beginTransaction(); 74 l = s.createQuery("from Employee e left join fetch e.managerBySite").list(); 75 assertEquals( l.size(), 5 ); 76 Set set = new HashSet (l); 77 assertEquals( set.size(), 4 ); 78 Iterator iter = set.iterator(); 79 int total=0; 80 while ( iter.hasNext() ) { 81 Map map = ( (Employee) iter.next() ).getManagerBySite(); 82 assertTrue( Hibernate.isInitialized(map) ); 83 total += map.size(); 84 } 85 assertTrue(total==3); 86 l = s.createQuery("from Employee e left join e.managerBySite m left join m.managerBySite m2").list(); 87 t.commit(); 88 s.close(); 89 } 90 91 protected String [] getMappings() { 92 return new String [] { "ternary/Ternary.hbm.xml" }; 93 } 94 95 public static Test suite() { 96 return new TestSuite(TernaryTest.class); 97 } 98 99 public String getCacheConcurrencyStrategy() { 100 return null; 101 } 102 103 } 104 105 | Popular Tags |