KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > IJ2Test


1 //$Id: IJ2Test.java,v 1.3 2005/06/19 02:01:05 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5
6
7
8 import junit.framework.Test;
9
10 import junit.framework.TestSuite;
11
12
13
14 import org.hibernate.LockMode;
15
16 import org.hibernate.classic.Session;
17 import org.hibernate.dialect.MySQLDialect;
18 import org.hibernate.test.TestCase;
19
20 /**
21  * @author Gavin King
22  */

23 public class IJ2Test extends TestCase {
24
25     public void testUnionSubclass() throws Exception JavaDoc {
26
27         //if ( getDialect() instanceof MySQLDialect ) return;
28

29         Session s = getSessions().openSession();
30         I i = new I();
31         i.setName( "i" );
32         i.setType( 'a' );
33         J j = new J();
34         j.setName( "j" );
35         j.setType( 'x' );
36         j.setAmount( 1.0f );
37         Serializable JavaDoc iid = s.save(i);
38         Serializable JavaDoc jid = s.save(j);
39         s.flush();
40         s.connection().commit();
41         s.close();
42
43         getSessions().evict(I.class);
44
45         s = getSessions().openSession();
46         j = (J) s.get(I.class, jid);
47         j = (J) s.get(J.class, jid);
48         i = (I) s.get(I.class, iid);
49         assertTrue( i.getClass()==I.class );
50         j.setAmount( 0.5f );
51         s.lock(i, LockMode.UPGRADE);
52         s.flush();
53         s.connection().commit();
54         s.close();
55
56         getSessions().evict(I.class);
57
58         s = getSessions().openSession();
59         j = (J) s.get(J.class, jid);
60         j = (J) s.get(I.class, jid);
61         i = (I) s.get(I.class, iid);
62         assertTrue( i.getClass()==I.class );
63         j.setAmount( 0.5f );
64         s.lock(i, LockMode.UPGRADE);
65         s.flush();
66         s.connection().commit();
67         s.close();
68
69         getSessions().evict(I.class);
70
71         s = getSessions().openSession();
72         assertTrue( s.find("from I").size()==2 );
73         assertTrue( s.find("from J").size()==1 );
74         assertTrue( s.find("from J j where j.amount > 0 and j.name is not null").size()==1 );
75         assertTrue( s.find("from I i where i.class = org.hibernate.test.legacy.I").size()==1 );
76         assertTrue( s.find("from I i where i.class = J").size()==1 );
77         s.connection().commit();
78         s.close();
79
80         getSessions().evict(I.class);
81
82         s = getSessions().openSession();
83         j = (J) s.get(J.class, jid);
84         i = (I) s.get(I.class, iid);
85         K k = new K();
86         Serializable JavaDoc kid = s.save(k);
87         i.setParent(k);
88         j.setParent(k);
89         s.flush();
90         s.connection().commit();
91         s.close();
92
93         getSessions().evict(I.class);
94
95         s = getSessions().openSession();
96         j = (J) s.get(J.class, jid);
97         i = (I) s.get(I.class, iid);
98         k = (K) s.get(K.class, kid);
99         System.out.println(k + "=" + i.getParent());
100         assertTrue( i.getParent()==k );
101         assertTrue( j.getParent()==k );
102         assertTrue( k.getIs().size()==2 );
103         s.flush();
104         s.connection().commit();
105         s.close();
106
107         getSessions().evict(I.class);
108
109         s = getSessions().openSession();
110         assertTrue( s.find("from K k inner join k.is i where i.name = 'j'").size()==1 );
111         assertTrue( s.find("from K k inner join k.is i where i.name = 'i'").size()==1 );
112         assertTrue( s.find("from K k left join fetch k.is").size()==2 );
113         s.connection().commit();
114         s.close();
115
116         s = getSessions().openSession();
117         j = (J) s.get(J.class, jid);
118         i = (I) s.get(I.class, iid);
119         k = (K) s.get(K.class, kid);
120         s.delete(k);
121         s.delete(j);
122         s.delete(i);
123         s.flush();
124         s.connection().commit();
125         s.close();
126
127     }
128
129     protected String JavaDoc[] getMappings() {
130         return new String JavaDoc[] { "legacy/IJ2.hbm.xml" };
131     }
132
133     public IJ2Test(String JavaDoc x) {
134         super(x);
135     }
136
137     public static Test suite() {
138         return new TestSuite(IJ2Test.class);
139     }
140 }
141
Popular Tags