1 package org.hibernate.test.annotations.cid; 3 4 import org.hibernate.Query; 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import org.hibernate.test.annotations.TestCase; 8 9 import java.util.List ; 10 11 16 public class CompositeIdTest extends TestCase { 17 public CompositeIdTest(String x) { 18 super(x); 19 } 20 21 25 public void testManyToOneInCompositePk() throws Exception { 26 Session s; 27 Transaction tx; 28 s = openSession(); 29 tx = s.beginTransaction(); 30 ParentPk ppk = new ParentPk(); 31 ppk.setFirstName("Emmanuel"); 32 ppk.setLastName("Bernard"); 33 Parent p = new Parent(); 34 p.id = ppk; 35 s.persist(p); 36 ChildPk cpk = new ChildPk(); 37 cpk.parent = p; 38 cpk.nthChild = 1; 39 Child c = new Child(); 40 c.id = cpk; 41 s.persist(c); 42 tx.commit(); 43 s.close(); 44 45 s = openSession(); 46 tx = s.beginTransaction(); 47 Query q = s.createQuery("select c from Child c where c.id.nthChild = :nth"); 48 q.setInteger("nth", 1); 49 List results = q.list(); 50 assertEquals( 1, results.size() ); 51 c = (Child) results.get(0); 52 assertNotNull(c); 53 assertNotNull(c.id.parent); 54 tx.commit(); 58 s.close(); 59 } 60 61 protected Class [] getMappings() { 62 return new Class [] { 63 Parent.class, 64 Child.class 65 }; 66 } 67 } 68 | Popular Tags |