Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/test/ecid/EmbeddedCompositeIdTest.java


1   //$Id: EmbeddedCompositeIdTest.java 7695 2005-07-30 00:53:37Z oneovthafew $
2   package org.hibernate.test.ecid;
3   
4   import java.util.List;
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 str) {
21      super(str);
22    }
23    
24    public void testMerge() {
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      c.setDescription("Grade 5 English");
35      uc.setDescription("Second year mathematics");
36      
37      s = openSession();
38      t = s.beginTransaction();
39      s.merge(c);
40      s.merge(uc);
41      t.commit();
42      s.close();
43      
44      s = openSession();
45      t = s.beginTransaction();
46      s.delete(c);
47      s.delete(uc);
48      t.commit();
49      s.close();
50    }
51  
52    public void testMerging() {
53      // Test HHH-799
54      Session s = openSession();
55      Transaction t = s.beginTransaction();
56      Course course = new Course( "EN-101", "BA", "preparatory english" );
57      s.persist( course );
58      t.commit();
59      s.close();
60  
61      String newDesc = "basic preparatory english";
62      course.setDescription( newDesc );
63  
64      s = openSession();
65      t = s.beginTransaction();
66      Course c = (Course) s.merge( course );
67      assertEquals( "description not merged", newDesc, c.getDescription() );
68      t.commit();
69      s.close();
70  
71      s = openSession();
72      t = s.beginTransaction();
73      Course cid = new Course( "EN-101", "BA", null );
74      course = ( Course ) s.get( Course.class, cid );
75      assertEquals( "description not merged", newDesc, course.getDescription() );
76      s.delete( course );
77      t.commit();
78      s.close();
79    }
80  
81    public void testPolymorphism() {
82      Session s = openSession();
83      Transaction t = s.beginTransaction();
84      Course uc =  new UniversityCourse("mat2000", "Monash", "second year maths", 0);
85      Course c =  new Course("eng5000", "BHS", "grade 5 english");
86      s.persist(uc);
87      s.persist(c);
88      t.commit();
89      s.close();
90      
91      s = openSession();
92      t = s.beginTransaction();
93      Course ucid = new Course("mat2000", "Monash", null);
94      Course cid =  new Course("eng5000", "BHS", null);
95      Course luc = (Course) s.load(Course.class, ucid);
96      Course lc = (Course) s.load(Course.class, cid);
97      assertFalse( Hibernate.isInitialized(luc) );
98      assertFalse( Hibernate.isInitialized(lc) );
99      assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
100     assertEquals( Course.class, Hibernate.getClass(lc) );
101     assertSame( ( (HibernateProxy) lc ).getHibernateLazyInitializer().getImplementation(), cid );
102     assertEquals( c.getCourseCode(), "eng5000" );
103     assertEquals( uc.getCourseCode(), "mat2000" );
104     t.commit();
105     s.close();
106 
107     s = openSession();
108     t = s.beginTransaction();
109     ucid = new Course("mat2000", "Monash", null);
110     cid =  new Course("eng5000", "BHS", null);
111     luc = (Course) s.get(Course.class, ucid);
112     lc = (Course) s.get(Course.class, cid);
113     assertTrue( Hibernate.isInitialized(luc) );
114     assertTrue( Hibernate.isInitialized(lc) );
115     assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
116     assertEquals( Course.class, Hibernate.getClass(lc) );
117     assertSame( lc, cid );
118     assertEquals( c.getCourseCode(), "eng5000" );
119     assertEquals( uc.getCourseCode(), "mat2000" );
120     t.commit();
121     s.close();
122 
123     s = openSession();
124     t = s.beginTransaction();
125     List list = s.createQuery("from Course order by courseCode").list();
126     assertTrue( list.get(0) instanceof Course );
127     assertTrue( list.get(1) instanceof UniversityCourse );
128     c = (Course) list.get(0);
129     uc = (UniversityCourse) list.get(1);
130     assertEquals( c.getCourseCode(), "eng5000" );
131     assertEquals( uc.getCourseCode(), "mat2000" );
132     t.commit();
133     s.close();
134     
135     c.setDescription("Grade 5 English");
136     uc.setDescription("Second year mathematics");
137     
138     s = openSession();
139     t = s.beginTransaction();
140     s.saveOrUpdate(c);
141     s.saveOrUpdate(uc);
142     t.commit();
143     s.close();
144     
145     s = openSession();
146     t = s.beginTransaction();
147     s.delete(c);
148     s.delete(uc);
149     t.commit();
150     s.close();
151     
152   }
153 
154   protected String[] getMappings() {
155     return new String[] { "ecid/Course.hbm.xml" };
156   }
157 
158   public static Test suite() {
159     return new TestSuite(EmbeddedCompositeIdTest.class);
160   }
161 
162 }
163