KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > joinedsubclass > JoinedSubclassTest


1 //$Id: JoinedSubclassTest.java,v 1.7 2005/07/19 23:25:36 oneovthafew Exp $
2
package org.hibernate.test.joinedsubclass;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import junit.framework.Test;
9 import junit.framework.TestSuite;
10
11 import org.hibernate.Hibernate;
12 import org.hibernate.Session;
13 import org.hibernate.Transaction;
14 import org.hibernate.criterion.Expression;
15 import org.hibernate.criterion.Property;
16 import org.hibernate.dialect.HSQLDialect;
17 import org.hibernate.test.TestCase;
18
19 /**
20  * @author Gavin King
21  */

22 public class JoinedSubclassTest extends TestCase {
23     
24     public JoinedSubclassTest(String JavaDoc str) {
25         super(str);
26     }
27     
28     public void testJoinedSubclass() {
29         Session s = openSession();
30         Transaction t = s.beginTransaction();
31         
32         Employee mark = new Employee();
33         mark.setName("Mark");
34         mark.setTitle("internal sales");
35         mark.setSex('M');
36         mark.setAddress("buckhead");
37         mark.setZip("30305");
38         mark.setCountry("USA");
39         
40         Customer joe = new Customer();
41         joe.setName("Joe");
42         joe.setAddress("San Francisco");
43         joe.setZip("XXXXX");
44         joe.setCountry("USA");
45         joe.setComments("Very demanding");
46         joe.setSex('M');
47         joe.setSalesperson(mark);
48         
49         Person yomomma = new Person();
50         yomomma.setName("mum");
51         yomomma.setSex('F');
52         
53         s.save(yomomma);
54         s.save(mark);
55         s.save(joe);
56         
57         assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 );
58         
59         assertEquals( s.createQuery("from Person").list().size(), 3 );
60         assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 );
61         assertEquals( s.createQuery("from Person p where p.class = Person").list().size(), 1 );
62         s.clear();
63
64         List JavaDoc customers = s.createQuery("from Customer c left join fetch c.salesperson").list();
65         for ( Iterator JavaDoc iter = customers.iterator(); iter.hasNext(); ) {
66             Customer c = (Customer) iter.next();
67             assertTrue( Hibernate.isInitialized( c.getSalesperson() ) );
68             assertEquals( c.getSalesperson().getName(), "Mark" );
69         }
70         assertEquals( customers.size(), 1 );
71         s.clear();
72         
73         customers = s.createQuery("from Customer").list();
74         for ( Iterator JavaDoc iter = customers.iterator(); iter.hasNext(); ) {
75             Customer c = (Customer) iter.next();
76             assertFalse( Hibernate.isInitialized( c.getSalesperson() ) );
77             assertEquals( c.getSalesperson().getName(), "Mark" );
78         }
79         assertEquals( customers.size(), 1 );
80         s.clear();
81         
82
83         mark = (Employee) s.get( Employee.class, new Long JavaDoc( mark.getId() ) );
84         joe = (Customer) s.get( Customer.class, new Long JavaDoc( joe.getId() ) );
85         
86         mark.setZip("30306");
87         assertEquals( s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1 );
88         
89         if ( !(getDialect() instanceof HSQLDialect) ) {
90             s.createCriteria(Person.class).add(
91                     Expression.in("address", new Address[] { mark.getAddress(), joe.getAddress() } )
92             ).list();
93         }
94         
95         s.delete(mark);
96         s.delete(joe);
97         s.delete(yomomma);
98         assertTrue( s.createQuery("from Person").list().isEmpty() );
99         t.commit();
100         s.close();
101     }
102     
103     public void testQuerySubclassAttribute() {
104         Session s = openSession();
105         Transaction t = s.beginTransaction();
106         Person p = new Person();
107         p.setName("Emmanuel");
108         p.setSex('M');
109         s.persist(p);
110         Employee q = new Employee();
111         q.setName("Steve");
112         q.setSex('M');
113         q.setTitle("Mr");
114         q.setSalary( new BigDecimal JavaDoc(1000) );
115         s.persist(q);
116
117         List JavaDoc result = s.createQuery("from Person where salary > 100").list();
118         assertEquals( result.size(), 1 );
119         assertSame( result.get(0), q );
120         
121         result = s.createQuery("from Person where salary > 100 or name like 'E%'").list();
122         assertEquals( result.size(), 2 );
123
124         result = s.createCriteria(Person.class)
125             .add( Property.forName("salary").gt( new BigDecimal JavaDoc(100) ) )
126             .list();
127         assertEquals( result.size(), 1 );
128         assertSame( result.get(0), q );
129
130         //TODO: make this work:
131
/*result = s.createQuery("select salary from Person where salary > 100").list();
132         assertEquals( result.size(), 1 );
133         assertEquals( result.get(0), new BigDecimal(1000) );*/

134         
135         s.delete(p);
136         s.delete(q);
137         t.commit();
138         s.close();
139     }
140
141     
142     protected String JavaDoc[] getMappings() {
143         return new String JavaDoc[] { "joinedsubclass/Person.hbm.xml" };
144     }
145
146     public static Test suite() {
147         return new TestSuite(JoinedSubclassTest.class);
148     }
149
150 }
151
152
Popular Tags