KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > subselect > SubselectTest


1 //$Id: SubselectTest.java,v 1.3 2005/06/19 02:01:05 oneovthafew Exp $
2
package org.hibernate.test.subselect;
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.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * @author Gavin King
16  */

17 public class SubselectTest extends TestCase {
18     
19     public SubselectTest(String JavaDoc str) {
20         super(str);
21     }
22     
23     public void testEntitySubselect() {
24         Session s = openSession();
25         Transaction t = s.beginTransaction();
26         Human gavin = new Human();
27         gavin.setName( "gavin" );
28         gavin.setSex( 'M' );
29         gavin.setAddress( "Melbourne, Australia" );
30         Alien x23y4 = new Alien();
31         x23y4.setIdentity( "x23y4$$hu%3" );
32         x23y4.setPlanet( "Mars" );
33         x23y4.setSpecies( "martian" );
34         s.save(gavin);
35         s.save(x23y4);
36         s.flush();
37         List JavaDoc beings = s.createQuery("from Being").list();
38         for ( Iterator JavaDoc iter = beings.iterator(); iter.hasNext(); ) {
39             Being b = (Being) iter.next();
40             assertNotNull( b.getLocation() );
41             assertNotNull( b.getIdentity() );
42             assertNotNull( b.getSpecies() );
43         }
44         s.clear();
45         getSessions().evict(Being.class);
46         Being gav = (Being) s.get(Being.class, gavin.getId());
47         assertEquals( gav.getLocation(), gavin.getAddress() );
48         assertEquals( gav.getSpecies(), "human" );
49         assertEquals( gav.getIdentity(), gavin.getName() );
50         s.clear();
51         //test the <synchronized> tag:
52
gavin = (Human) s.get(Human.class, gavin.getId());
53         gavin.setAddress( "Atlanta, GA" );
54         gav = (Being) s.createQuery("from Being b where b.location like '%GA%'").uniqueResult();
55         assertEquals( gav.getLocation(), gavin.getAddress() );
56         s.delete(gavin);
57         s.delete(x23y4);
58         assertTrue( s.createQuery("from Being").list().isEmpty() );
59         t.commit();
60         s.close();
61     }
62
63     
64     protected String JavaDoc[] getMappings() {
65         return new String JavaDoc[] { "subselect/Beings.hbm.xml" };
66     }
67
68     public static Test suite() {
69         return new TestSuite(SubselectTest.class);
70     }
71
72 }
73
74
Popular Tags