KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > ecid > EmbeddedCompositeIdTest


1 //$Id: EmbeddedCompositeIdTest.java,v 1.2 2005/06/08 05:42:20 oneovthafew Exp $
2
package org.hibernate.test.ecid;
3
4 import java.util.List JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.proxy.HibernateProxy;
13 import org.hibernate.test.TestCase;
14
15 /**
16  * @author Gavin King
17  */

18 public class EmbeddedCompositeIdTest extends TestCase {
19     
20     public EmbeddedCompositeIdTest(String JavaDoc str) {
21         super(str);
22     }
23     
24     public void testPolymorphism() {
25         Session s = openSession();
26         Transaction t = s.beginTransaction();
27         Course uc = new UniversityCourse("mat2000", "Monash", "second year maths", 0);
28         Course c = new Course("eng5000", "BHS", "grade 5 english");
29         s.persist(uc);
30         s.persist(c);
31         t.commit();
32         s.close();
33         
34         s = openSession();
35         t = s.beginTransaction();
36         Course ucid = new Course("mat2000", "Monash", null);
37         Course cid = new Course("eng5000", "BHS", null);
38         Course luc = (Course) s.load(Course.class, ucid);
39         Course lc = (Course) s.load(Course.class, cid);
40         assertFalse( Hibernate.isInitialized(luc) );
41         assertFalse( Hibernate.isInitialized(lc) );
42         assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
43         assertEquals( Course.class, Hibernate.getClass(lc) );
44         assertSame( ( (HibernateProxy) lc ).getHibernateLazyInitializer().getImplementation(), cid );
45         assertEquals( c.getCourseCode(), "eng5000" );
46         assertEquals( uc.getCourseCode(), "mat2000" );
47         t.commit();
48         s.close();
49
50         s = openSession();
51         t = s.beginTransaction();
52         ucid = new Course("mat2000", "Monash", null);
53         cid = new Course("eng5000", "BHS", null);
54         luc = (Course) s.get(Course.class, ucid);
55         lc = (Course) s.get(Course.class, cid);
56         assertTrue( Hibernate.isInitialized(luc) );
57         assertTrue( Hibernate.isInitialized(lc) );
58         assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
59         assertEquals( Course.class, Hibernate.getClass(lc) );
60         assertSame( lc, cid );
61         assertEquals( c.getCourseCode(), "eng5000" );
62         assertEquals( uc.getCourseCode(), "mat2000" );
63         t.commit();
64         s.close();
65
66         s = openSession();
67         t = s.beginTransaction();
68         List JavaDoc list = s.createQuery("from Course order by courseCode").list();
69         assertTrue( list.get(0) instanceof Course );
70         assertTrue( list.get(1) instanceof UniversityCourse );
71         c = (Course) list.get(0);
72         uc = (UniversityCourse) list.get(1);
73         assertEquals( c.getCourseCode(), "eng5000" );
74         assertEquals( uc.getCourseCode(), "mat2000" );
75         t.commit();
76         s.close();
77         
78         c.setDescription("Grade 5 English");
79         uc.setDescription("Second year mathematics");
80         
81         s = openSession();
82         t = s.beginTransaction();
83         s.saveOrUpdate(c);
84         s.saveOrUpdate(uc);
85         t.commit();
86         s.close();
87         
88         s = openSession();
89         t = s.beginTransaction();
90         s.delete(c);
91         s.delete(uc);
92         t.commit();
93         s.close();
94         
95     }
96
97     protected String JavaDoc[] getMappings() {
98         return new String JavaDoc[] { "ecid/Course.hbm.xml" };
99     }
100
101     public static Test suite() {
102         return new TestSuite(EmbeddedCompositeIdTest.class);
103     }
104
105 }
106
107
Popular Tags