KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > cid > CompositeIdTest


1 //$Id: CompositeIdTest.java,v 1.1 2005/05/12 13:33:26 epbernard Exp $
2
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 JavaDoc;
10
11 /**
12  * test some composite id functionalities
13  *
14  * @author Emmanuel Bernard
15  */

16 public class CompositeIdTest extends TestCase {
17     public CompositeIdTest(String JavaDoc x) {
18         super(x);
19     }
20
21     /**
22      * This feature is not supported by the EJB3
23      * this is an hibernate extension
24      */

25     public void testManyToOneInCompositePk() throws Exception JavaDoc {
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 JavaDoc results = q.list();
50         assertEquals( 1, results.size() );
51         c = (Child) results.get(0);
52         assertNotNull(c);
53         assertNotNull(c.id.parent);
54         //FIXME mke it work in unambigious cases
55
// assertNotNull(c.id.parent.id);
56
// assertEquals(p.id.getFirstName(), c.id.parent.id.getFirstName());
57
tx.commit();
58         s.close();
59     }
60
61     protected Class JavaDoc[] getMappings() {
62         return new Class JavaDoc[] {
63             Parent.class,
64             Child.class
65         };
66     }
67 }
68
Popular Tags