KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > query > QueryTest


1 //$Id: QueryTest.java,v 1.6 2005/06/21 10:20:51 epbernard Exp $
2
package org.hibernate.test.annotations.query;
3
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.GregorianCalendar JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.hibernate.Query;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.annotations.A320;
13 import org.hibernate.test.annotations.A320b;
14 import org.hibernate.test.annotations.Plane;
15 import org.hibernate.test.annotations.TestCase;
16
17 /**
18  * Test named queries
19  *
20  * @author Emmanuel Bernard
21  */

22 public class QueryTest extends TestCase {
23     public QueryTest(String JavaDoc x) {
24         super(x);
25     }
26
27     public void testPackageQueries() throws Exception JavaDoc {
28         Session s = openSession();
29         Transaction tx = s.beginTransaction();
30         Plane p = new Plane();
31         s.persist(p);
32         Query q = s.getNamedQuery("plane.getAll");
33         assertEquals( 1, q.list().size() );
34         tx.commit();
35         s.close();
36     }
37
38     public void testClassQueries() throws Exception JavaDoc {
39         Session s = openSession();
40         Transaction tx = s.beginTransaction();
41         Night n = new Night();
42         Calendar JavaDoc c = new GregorianCalendar JavaDoc();
43         c.set(2000, 2, 2);
44         Date JavaDoc now = c.getTime();
45         c.add(Calendar.MONTH, -1);
46         Date JavaDoc aMonthAgo = c.getTime();
47         c.add(Calendar.MONTH, 2);
48         Date JavaDoc inAMonth = c.getTime();
49         n.setDate( now );
50         n.setDuration(14);
51         s.persist(n);
52         Query q = s.getNamedQuery("night.moreRecentThan");
53         q.setDate( "date", aMonthAgo );
54         assertEquals( 1, q.list().size() );
55         q = s.getNamedQuery("night.moreRecentThan");
56         q.setDate( "date", inAMonth );
57         assertEquals( 0, q.list().size() );
58         tx.commit();
59         s.close();
60     }
61
62     public void testSQLQuery() {
63         Night n = new Night();
64         Calendar JavaDoc c = new GregorianCalendar JavaDoc();
65         c.set(2000, 2, 2);
66         Date JavaDoc now = c.getTime();
67         c.add(Calendar.MONTH, -1);
68         Date JavaDoc aMonthAgo = c.getTime();
69         c.add(Calendar.MONTH, 2);
70         Date JavaDoc inAMonth = c.getTime();
71         n.setDate( now );
72         n.setDuration(9999);
73         Area area = new Area();
74         area.setName("Monceau");
75
76         Session s = openSession();
77         Transaction tx = s.beginTransaction();
78         s.persist(n);
79         s.persist(area);
80         tx.commit();
81         s.clear();
82         tx = s.beginTransaction();
83         Query q = s.getNamedQuery("night.getAll.bySQL");
84         q.setParameter(0, 9990);
85         List JavaDoc result = q.list();
86         assertEquals( 1, result.size() );
87         Night n2 = (Night) result.get(0);
88         assertEquals( n2.getDuration(), n.getDuration() );
89         List JavaDoc areas = s.getNamedQuery("getAreaByNative").list();
90         assertTrue( 1 == areas.size() );
91         assertEquals( area.getName(), ( (Area) areas.get(0) ).getName() );
92         tx.commit();
93         s.close();
94     }
95
96     public void testSQLQueryWithManyToOne() {
97         Night n = new Night();
98         Calendar JavaDoc c = new GregorianCalendar JavaDoc();
99         c.set(2000, 2, 2);
100         Date JavaDoc now = c.getTime();
101         c.add(Calendar.MONTH, -1);
102         Date JavaDoc aMonthAgo = c.getTime();
103         c.add(Calendar.MONTH, 2);
104         Date JavaDoc inAMonth = c.getTime();
105         n.setDate( now );
106         n.setDuration(9999);
107         Area a = new Area();
108         a.setName("Paris");
109         n.setArea(a);
110         Session s = openSession();
111         Transaction tx = s.beginTransaction();
112         s.persist(a);
113         s.persist(n);
114         tx.commit();
115         s.clear();
116         tx = s.beginTransaction();
117         Query q = s.getNamedQuery("night&area");
118         List JavaDoc result = q.list();
119         assertEquals( 1, result.size() );
120         Night n2 = (Night) ( (Object JavaDoc[]) result.get(0) )[0];
121         assertEquals( n2.getDuration(), n.getDuration() );
122         tx.commit();
123         s.close();
124     }
125
126     public void testImplicitNativeQuery() throws Exception JavaDoc {
127         Session s;
128         Transaction tx;
129         s = openSession();
130         tx = s.beginTransaction();
131         SpaceShip ship = new SpaceShip();
132         ship.setModel("X-Wing");
133         ship.setName("YuBlue");
134         ship.setSpeed(2000);
135         s.persist(ship);
136         tx.commit();
137         s.clear();
138         tx = s.beginTransaction();
139         Query q = s.getNamedQuery("implicitSample");
140         List JavaDoc result = q.list();
141         assertEquals( 1, result.size() );
142         assertEquals( ship.getModel(), ( (SpaceShip) result.get(0) ).getModel() );
143         tx.commit();
144         s.close();
145     }
146
147     public void testDiscriminator() throws Exception JavaDoc {
148         Session s;
149         Transaction tx;
150         s = openSession();
151         tx = s.beginTransaction();
152         Dictionary dic = new Dictionary();
153         dic.setName("Anglais-Francais");
154         dic.setEditor("Harrap's");
155         SynonymousDictionary syn = new SynonymousDictionary();
156         syn.setName("Synonymes de tous les temps");
157         syn.setEditor("Imagination edition");
158         s.persist(dic);
159         s.persist(syn);
160         tx.commit();
161         s.clear();
162         tx = s.beginTransaction();
163         List JavaDoc results = s.getNamedQuery("all.dictionaries").list();
164         assertEquals( 2, results.size() );
165         assertTrue( results.get(0) instanceof SynonymousDictionary
166                 || results.get(1) instanceof SynonymousDictionary);
167         tx.commit();
168         s.close();
169     }
170
171 // public void testScalarQuery() throws Exception {
172
// Session s = openSession();
173
// Transaction tx;
174
// tx = s.beginTransaction();
175
// Mark bad = new Mark();
176
// bad.value = 5;
177
// Mark good = new Mark();
178
// good.value = 15;
179
// s.persist(bad);
180
// s.persist(good);
181
// tx.commit();
182
// s.clear();
183
// tx = s.beginTransaction();
184
// List result = s.getNamedQuery("average").list();
185
// assertEquals( 1, result.size() );
186
// tx.commit();
187
// s.close();
188
//
189
// }
190

191     protected Class JavaDoc[] getMappings() {
192         return new Class JavaDoc[] {
193             Plane.class,
194             A320.class,
195             A320b.class,
196             Night.class,
197             Area.class,
198             SpaceShip.class,
199             Dictionary.class,
200             SynonymousDictionary.class
201         };
202     }
203
204     protected String JavaDoc[] getAnnotatedPackages() {
205         return new String JavaDoc[] {
206             "org.hibernate.test.annotations.query"
207         };
208     }
209 }
210
Popular Tags