1 package org.hibernate.test.formulajoin; 3 4 import java.util.List ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 9 import org.hibernate.Session; 10 import org.hibernate.Transaction; 11 import org.hibernate.dialect.PostgreSQLDialect; 12 import org.hibernate.test.TestCase; 13 14 17 public class FormulaJoinTest extends TestCase { 18 19 public FormulaJoinTest(String str) { 20 super(str); 21 } 22 23 public void testFormulaJoin() { 24 Session s = openSession(); 25 Transaction tx = s.beginTransaction(); 26 Master master = new Master(); 27 master.setName("master 1"); 28 Detail current = new Detail(); 29 current.setCurrentVersion(true); 30 current.setVersion(2); 31 current.setDetails("details of master 1 blah blah"); 32 current.setMaster(master); 33 master.setDetail(current); 34 Detail past = new Detail(); 35 past.setCurrentVersion(false); 36 past.setVersion(1); 37 past.setDetails("old details of master 1 yada yada"); 38 past.setMaster(master); 39 s.persist(master); 40 s.persist(past); 41 s.persist(current); 42 tx.commit(); 43 s.close(); 44 45 if ( getDialect() instanceof PostgreSQLDialect ) return; 46 47 s = openSession(); 48 tx = s.beginTransaction(); 49 List l = s.createQuery("from Master m left join m.detail d").list(); 50 assertEquals( l.size(), 1 ); 51 tx.commit(); 52 s.close(); 53 54 s = openSession(); 55 tx = s.beginTransaction(); 56 l = s.createQuery("from Master m left join fetch m.detail").list(); 57 assertEquals( l.size(), 1 ); 58 Master m = (Master) l.get(0); 59 assertEquals( "master 1", m.getDetail().getMaster().getName() ); 60 assertTrue( m==m.getDetail().getMaster() ); 61 tx.commit(); 62 s.close(); 63 64 s = openSession(); 65 tx = s.beginTransaction(); 66 l = s.createQuery("from Master m join fetch m.detail").list(); 67 assertEquals( l.size(), 1 ); 68 tx.commit(); 69 s.close(); 70 71 s = openSession(); 72 tx = s.beginTransaction(); 73 l = s.createQuery("from Detail d join fetch d.currentMaster.master").list(); 74 assertEquals( l.size(), 2 ); 75 tx.commit(); 76 s.close(); 77 78 s = openSession(); 79 tx = s.beginTransaction(); 80 l = s.createQuery("from Detail d join fetch d.currentMaster.master m join fetch m.detail").list(); 81 assertEquals( l.size(), 2 ); 82 tx.commit(); 83 s.close(); 84 85 } 86 87 88 protected String [] getMappings() { 89 return new String [] { "formulajoin/Master.hbm.xml" }; 90 } 91 92 public static Test suite() { 93 return new TestSuite(FormulaJoinTest.class); 94 } 95 96 } 97 98 | Popular Tags |