KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > unionsubclass > UnionSubclassTest


1 //$Id: UnionSubclassTest.java,v 1.11 2005/05/03 22:59:24 epbernard Exp $
2
package org.hibernate.test.unionsubclass;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.FetchMode;
11 import org.hibernate.Hibernate;
12 import org.hibernate.Session;
13 import org.hibernate.Transaction;
14 import org.hibernate.Query;
15 import org.hibernate.criterion.Order;
16 import org.hibernate.test.TestCase;
17
18 /**
19  * @author Gavin King
20  */

21 public class UnionSubclassTest extends TestCase {
22     
23     public UnionSubclassTest(String JavaDoc str) {
24         super(str);
25     }
26     
27     public void testUnionSubclassCollection() {
28         Session s = openSession();
29         Transaction t = s.beginTransaction();
30         Location mel = new Location("Earth");
31         s.save(mel);
32         
33         Human gavin = new Human();
34         gavin.setIdentity("gavin");
35         gavin.setSex('M');
36         gavin.setLocation(mel);
37         mel.addBeing(gavin);
38         
39         gavin.getInfo().put("foo", "bar");
40         gavin.getInfo().put("x", "y");
41         
42         t.commit();
43         s.close();
44
45         s = openSession();
46         t = s.beginTransaction();
47         gavin = (Human) s.createCriteria(Human.class).uniqueResult();
48         assertEquals( gavin.getInfo().size(), 2 );
49         s.delete(gavin);
50         s.delete( gavin.getLocation() );
51         t.commit();
52         s.close();
53     }
54
55     public void testUnionSubclassFetchMode() {
56         Session s = openSession();
57         Transaction t = s.beginTransaction();
58         Location mel = new Location("Earth");
59         s.save(mel);
60         
61         Human gavin = new Human();
62         gavin.setIdentity("gavin");
63         gavin.setSex('M');
64         gavin.setLocation(mel);
65         mel.addBeing(gavin);
66         Human max = new Human();
67         max.setIdentity("max");
68         max.setSex('M');
69         max.setLocation(mel);
70         mel.addBeing(gavin);
71         
72         s.flush();
73         s.clear();
74         
75         List JavaDoc list = s.createCriteria(Human.class)
76             .setFetchMode("location", FetchMode.JOIN)
77             .setFetchMode("location.beings", FetchMode.JOIN)
78             .list();
79         
80         for (int i=0; i<list.size(); i++ ) {
81             Human h = (Human) list.get(i);
82             assertTrue( Hibernate.isInitialized( h.getLocation() ) );
83             assertTrue( Hibernate.isInitialized( h.getLocation().getBeings() ) );
84             s.delete(h);
85         }
86         s.delete( s.get( Location.class, new Long JavaDoc(mel.getId()) ) );
87         t.commit();
88         s.close();
89         
90         
91     }
92     
93     public void testUnionSubclassOneToMany() {
94         Session s = openSession();
95         Transaction t = s.beginTransaction();
96         Location mel = new Location("Melbourne, Australia");
97         Location mars = new Location("Mars");
98         s.save(mel);
99         s.save(mars);
100         
101         Human gavin = new Human();
102         gavin.setIdentity("gavin");
103         gavin.setSex('M');
104         gavin.setLocation(mel);
105         mel.addBeing(gavin);
106         
107         Alien x23y4 = new Alien();
108         x23y4.setIdentity("x23y4$$hu%3");
109         x23y4.setLocation(mars);
110         x23y4.setSpecies("martian");
111         mars.addBeing(x23y4);
112         
113         Alien yy3dk = new Alien();
114         yy3dk.setIdentity("yy3dk&*!!!");
115         yy3dk.setLocation(mars);
116         yy3dk.setSpecies("martian");
117         mars.addBeing(yy3dk);
118         
119         Hive hive = new Hive();
120         hive.setLocation(mars);
121         hive.getMembers().add(x23y4);
122         x23y4.setHive(hive);
123         hive.getMembers().add(yy3dk);
124         yy3dk.setHive(hive);
125         s.persist(hive);
126         
127         yy3dk.getHivemates().add(x23y4);
128         x23y4.getHivemates().add(yy3dk);
129         
130         s.flush();
131         s.clear();
132         
133         hive = (Hive) s.createQuery("from Hive h").uniqueResult();
134         assertFalse( Hibernate.isInitialized( hive.getMembers() ) );
135         assertEquals( hive.getMembers().size(), 2 );
136         
137         s.clear();
138         
139         hive = (Hive) s.createQuery("from Hive h left join fetch h.location left join fetch h.members").uniqueResult();
140         assertTrue( Hibernate.isInitialized( hive.getMembers() ) );
141         assertEquals( hive.getMembers().size(), 2 );
142         
143         s.clear();
144         
145         x23y4 = (Alien) s.createQuery("from Alien a left join fetch a.hivemates where a.identity like 'x%'").uniqueResult();
146         assertTrue( Hibernate.isInitialized( x23y4.getHivemates() ) );
147         assertEquals( x23y4.getHivemates().size(), 1 );
148         
149         s.clear();
150         
151         x23y4 = (Alien) s.createQuery("from Alien a where a.identity like 'x%'").uniqueResult();
152         assertFalse( Hibernate.isInitialized( x23y4.getHivemates() ) );
153         assertEquals( x23y4.getHivemates().size(), 1 );
154         
155         s.clear();
156         
157         x23y4 = (Alien) s.createCriteria(Alien.class).addOrder( Order.asc("identity") ).list().get(0);
158         s.delete( x23y4.getHive() );
159         s.delete( s.get(Location.class, new Long JavaDoc( mel.getId() ) ) );
160         s.delete( s.get(Location.class, new Long JavaDoc( mars.getId() ) ) );
161         assertTrue( s.createQuery("from Being").list().isEmpty() );
162         t.commit();
163         s.close();
164     }
165     
166     public void testUnionSubclassManyToOne() {
167         Session s = openSession();
168         Transaction t = s.beginTransaction();
169         Location mel = new Location("Melbourne, Australia");
170         Location mars = new Location("Mars");
171         s.save(mel);
172         s.save(mars);
173         
174         Human gavin = new Human();
175         gavin.setIdentity("gavin");
176         gavin.setSex('M');
177         gavin.setLocation(mel);
178         mel.addBeing(gavin);
179         
180         Alien x23y4 = new Alien();
181         x23y4.setIdentity("x23y4$$hu%3");
182         x23y4.setLocation(mars);
183         x23y4.setSpecies("martian");
184         mars.addBeing(x23y4);
185         
186         Hive hive = new Hive();
187         hive.setLocation(mars);
188         hive.getMembers().add(x23y4);
189         x23y4.setHive(hive);
190         s.persist(hive);
191         
192         Thing thing = new Thing();
193         thing.setDescription("some thing");
194         thing.setOwner(gavin);
195         gavin.getThings().add(thing);
196         s.save(thing);
197         s.flush();
198         
199         s.clear();
200         
201         thing = (Thing) s.createQuery("from Thing t left join fetch t.owner").uniqueResult();
202         assertTrue( Hibernate.isInitialized( thing.getOwner() ) );
203         assertEquals( thing.getOwner().getIdentity(), "gavin" );
204         s.clear();
205         
206         thing = (Thing) s.createQuery("select t from Thing t left join t.owner where t.owner.identity='gavin'").uniqueResult();
207         assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
208         assertEquals( thing.getOwner().getIdentity(), "gavin" );
209         s.clear();
210         
211         gavin = (Human) s.createQuery("from Human h left join fetch h.things").uniqueResult();
212         assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
213         assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
214         s.clear();
215         
216         assertTrue( s.createQuery("from Being b left join fetch b.things").list().size()==2 );
217         s.clear();
218         
219         gavin = (Human) s.createQuery("from Being b join fetch b.things").uniqueResult();
220         assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
221         assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
222         s.clear();
223         
224         gavin = (Human) s.createQuery("select h from Human h join h.things t where t.description='some thing'").uniqueResult();
225         assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
226         assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
227         s.clear();
228         
229         gavin = (Human) s.createQuery("select b from Being b join b.things t where t.description='some thing'").uniqueResult();
230         assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
231         assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
232         s.clear();
233         
234         thing = (Thing) s.get( Thing.class, new Long JavaDoc( thing.getId() ) );
235         assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
236         assertEquals( thing.getOwner().getIdentity(), "gavin" );
237         
238         thing.getOwner().getThings().remove(thing);
239         thing.setOwner(x23y4);
240         x23y4.getThings().add(thing);
241         
242         s.flush();
243         
244         s.clear();
245
246         thing = (Thing) s.get( Thing.class, new Long JavaDoc( thing.getId() ) );
247         assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
248         assertEquals( thing.getOwner().getIdentity(), "x23y4$$hu%3" );
249         
250         s.delete(thing);
251         x23y4 = (Alien) s.createCriteria(Alien.class).uniqueResult();
252         s.delete( x23y4.getHive() );
253         s.delete( s.get(Location.class, new Long JavaDoc( mel.getId() ) ) );
254         s.delete( s.get(Location.class, new Long JavaDoc( mars.getId() ) ) );
255         assertTrue( s.createQuery("from Being").list().isEmpty() );
256         t.commit();
257         s.close();
258     }
259     
260     public void testUnionSubclass() {
261         Session s = openSession();
262         Transaction t = s.beginTransaction();
263         Location mel = new Location("Melbourne, Australia");
264         Location atl = new Location("Atlanta, GA");
265         Location mars = new Location("Mars");
266         s.save(mel);
267         s.save(atl);
268         s.save(mars);
269         
270         Human gavin = new Human();
271         gavin.setIdentity("gavin");
272         gavin.setSex('M');
273         gavin.setLocation(mel);
274         mel.addBeing(gavin);
275         
276         Alien x23y4 = new Alien();
277         x23y4.setIdentity("x23y4$$hu%3");
278         x23y4.setLocation(mars);
279         x23y4.setSpecies("martian");
280         mars.addBeing(x23y4);
281         
282         Hive hive = new Hive();
283         hive.setLocation(mars);
284         hive.getMembers().add(x23y4);
285         x23y4.setHive(hive);
286         s.persist(hive);
287         
288         assertEquals( s.createQuery("from Being").list().size(), 2 );
289         assertEquals( s.createQuery("from Being b where b.class = Alien").list().size(), 1 );
290         assertEquals( s.createQuery("from Alien").list().size(), 1 );
291         s.clear();
292
293         List JavaDoc beings = s.createQuery("from Being b left join fetch b.location").list();
294         for ( Iterator JavaDoc iter = beings.iterator(); iter.hasNext(); ) {
295             Being b = (Being) iter.next();
296             assertTrue( Hibernate.isInitialized( b.getLocation() ) );
297             assertNotNull( b.getLocation().getName() );
298             assertNotNull( b.getIdentity() );
299             assertNotNull( b.getSpecies() );
300         }
301         assertEquals( beings.size(), 2 );
302         s.clear();
303         
304         beings = s.createQuery("from Being").list();
305         for ( Iterator JavaDoc iter = beings.iterator(); iter.hasNext(); ) {
306             Being b = (Being) iter.next();
307             assertFalse( Hibernate.isInitialized( b.getLocation() ) );
308             assertNotNull( b.getLocation().getName() );
309             assertNotNull( b.getIdentity() );
310             assertNotNull( b.getSpecies() );
311         }
312         assertEquals( beings.size(), 2 );
313         s.clear();
314         
315         List JavaDoc locations = s.createQuery("from Location").list();
316         int count = 0;
317         for ( Iterator JavaDoc iter = locations.iterator(); iter.hasNext(); ) {
318             Location l = (Location) iter.next();
319             assertNotNull( l.getName() );
320             Iterator JavaDoc iter2 = l.getBeings().iterator();
321             while ( iter2.hasNext() ) {
322                 count++;
323                 assertSame( ( (Being) iter2.next() ).getLocation(), l );
324             }
325         }
326         assertEquals(count, 2);
327         assertEquals( locations.size(), 3 );
328         s.clear();
329
330         locations = s.createQuery("from Location loc left join fetch loc.beings").list();
331         count = 0;
332         for ( Iterator JavaDoc iter = locations.iterator(); iter.hasNext(); ) {
333             Location l = (Location) iter.next();
334             assertNotNull( l.getName() );
335             Iterator JavaDoc iter2 = l.getBeings().iterator();
336             while ( iter2.hasNext() ) {
337                 count++;
338                 assertSame( ( (Being) iter2.next() ).getLocation(), l );
339             }
340         }
341         assertEquals(count, 2);
342         assertEquals( locations.size(), 3 );
343         s.clear();
344
345         gavin = (Human) s.get( Human.class, new Long JavaDoc( gavin.getId() ) );
346         atl = (Location) s.get( Location.class, new Long JavaDoc( atl.getId() ) );
347         
348         atl.addBeing(gavin);
349         assertEquals( s.createQuery("from Human h where h.location.name like '%GA'").list().size(), 1 );
350         s.delete(gavin);
351         x23y4 = (Alien) s.createCriteria(Alien.class).uniqueResult();
352         s.delete( x23y4.getHive() );
353         assertTrue( s.createQuery("from Being").list().isEmpty() );
354         t.commit();
355         s.close();
356     }
357
358     public void testNestedUnionedSubclasses() throws Exception JavaDoc {
359         Session s;
360         Transaction tx;
361         s = openSession();
362         tx = s.beginTransaction();
363         Location mel = new Location("Earth");
364         Human marcf = new Human();
365         marcf.setIdentity("marc");
366         marcf.setSex('M');
367         mel.addBeing(marcf);
368         Employee steve = new Employee();
369         steve.setIdentity("steve");
370         steve.setSex('M');
371         steve.setSalary( new Double JavaDoc(0) );
372         mel.addBeing(steve);
373         s.persist(mel);
374         tx.commit();
375         s.close();
376         s = openSession();
377         tx = s.beginTransaction();
378         Query q = s.createQuery( "from Being h where h.identity = :name1 or h.identity = :name2" );
379         q.setString("name1", "marc");
380         q.setString("name2", "steve");
381         final List JavaDoc result = q.list();
382         assertEquals( 2, result.size() );
383         s.delete( result.get(0) );
384         s.delete( result.get(1) );
385         s.delete( ( (Human) result.get(0) ).getLocation() );
386         tx.commit();
387         s.close();
388     }
389
390
391     protected String JavaDoc[] getMappings() {
392         return new String JavaDoc[] { "unionsubclass/Beings.hbm.xml" };
393     }
394
395     public static Test suite() {
396         return new TestSuite(UnionSubclassTest.class);
397     }
398
399 }
400
401
Popular Tags