KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > subselectfetch > SubselectFetchTest


1 //$Id: SubselectFetchTest.java,v 1.3 2005/05/22 18:11:03 oneovthafew Exp $
2
package org.hibernate.test.subselectfetch;
3
4 import java.util.List JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.FetchMode;
10 import org.hibernate.Hibernate;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13 import org.hibernate.cfg.Configuration;
14 import org.hibernate.cfg.Environment;
15 import org.hibernate.criterion.Order;
16 import org.hibernate.criterion.Property;
17 import org.hibernate.test.TestCase;
18
19 /**
20  * @author Gavin King
21  */

22 public class SubselectFetchTest extends TestCase {
23
24     protected void configure(Configuration cfg) {
25         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
26     }
27
28     public SubselectFetchTest(String JavaDoc str) {
29         super(str);
30     }
31     
32     public void testSubselectFetchHql() {
33         Session s = openSession();
34         Transaction t = s.beginTransaction();
35         Parent p = new Parent("foo");
36         p.getChildren().add( new Child("foo1") );
37         p.getChildren().add( new Child("foo2") );
38         Parent q = new Parent("bar");
39         q.getChildren().add( new Child("bar1") );
40         q.getChildren().add( new Child("bar2") );
41         q.getMoreChildren().addAll( p.getChildren() );
42         s.persist(p);
43         s.persist(q);
44         t.commit();
45         s.close();
46         
47         s = openSession();
48         t = s.beginTransaction();
49         
50         getSessions().getStatistics().clear();
51         
52         List JavaDoc parents = s.createQuery("from Parent where name between 'bar' and 'foo' order by name desc")
53             .list();
54         p = (Parent) parents.get(0);
55         q = (Parent) parents.get(1);
56
57         assertFalse( Hibernate.isInitialized( p.getChildren() ) );
58         assertFalse( Hibernate.isInitialized( q.getChildren() ) );
59
60         assertEquals( p.getChildren().size(), 2 );
61         
62         assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
63                 
64         assertTrue( Hibernate.isInitialized( q.getChildren() ) );
65         
66         assertEquals( q.getChildren().size(), 2 );
67         
68         assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
69         
70         assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
71         assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
72
73         assertEquals( p.getMoreChildren().size(), 0 );
74         
75         assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
76     
77         assertEquals( q.getMoreChildren().size(), 2 );
78         
79         assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
80         
81         assertEquals( 3, getSessions().getStatistics().getPrepareStatementCount() );
82
83         Child c = (Child) p.getChildren().get(0);
84         c.getFriends().size();
85
86         s.delete(p);
87         s.delete(q);
88
89         t.commit();
90         s.close();
91     }
92     
93     public void testSubselectFetchWithLimit() {
94         Session s = openSession();
95         Transaction t = s.beginTransaction();
96         Parent p = new Parent("foo");
97         p.getChildren().add( new Child("foo1") );
98         p.getChildren().add( new Child("foo2") );
99         Parent q = new Parent("bar");
100         q.getChildren().add( new Child("bar1") );
101         q.getChildren().add( new Child("bar2") );
102         Parent r = new Parent("aaa");
103         r.getChildren().add( new Child("aaa1") );
104         s.persist(p);
105         s.persist(q);
106         s.persist(r);
107         t.commit();
108         s.close();
109         
110         s = openSession();
111         t = s.beginTransaction();
112         
113         getSessions().getStatistics().clear();
114         
115         List JavaDoc parents = s.createQuery("from Parent order by name desc")
116             .setMaxResults(2)
117             .list();
118         p = (Parent) parents.get(0);
119         q = (Parent) parents.get(1);
120         assertFalse( Hibernate.isInitialized( p.getChildren() ) );
121         assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
122         assertFalse( Hibernate.isInitialized( q.getChildren() ) );
123         assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
124         assertEquals( p.getMoreChildren().size(), 0 );
125         assertEquals( p.getChildren().size(), 2 );
126         assertTrue( Hibernate.isInitialized( q.getChildren() ) );
127         assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
128         
129         assertEquals( 3, getSessions().getStatistics().getPrepareStatementCount() );
130         
131         r = (Parent) s.get( Parent.class, r.getName() );
132         assertTrue( Hibernate.isInitialized( r.getChildren() ) );
133         assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) );
134         assertEquals( r.getChildren().size(), 1 );
135         assertEquals( r.getMoreChildren().size(), 0 );
136
137         s.delete(p);
138         s.delete(q);
139         s.delete(r);
140         
141         t.commit();
142         s.close();
143     }
144     
145     public void testManyToManyCriteriaJoin() {
146         Session s = openSession();
147         Transaction t = s.beginTransaction();
148         Parent p = new Parent("foo");
149         p.getChildren().add( new Child("foo1") );
150         p.getChildren().add( new Child("foo2") );
151         Parent q = new Parent("bar");
152         q.getChildren().add( new Child("bar1") );
153         q.getChildren().add( new Child("bar2") );
154         q.getMoreChildren().addAll( p.getChildren() );
155         s.persist(p);
156         s.persist(q);
157         t.commit();
158         s.close();
159         
160         s = openSession();
161         t = s.beginTransaction();
162         
163         List JavaDoc parents = s.createCriteria(Parent.class)
164             .createCriteria("moreChildren")
165             .createCriteria("friends")
166             .addOrder( Order.desc("name") )
167             .list();
168
169         parents = s.createCriteria(Parent.class)
170             .setFetchMode("moreChildren", FetchMode.JOIN)
171             .setFetchMode("moreChildren.friends", FetchMode.JOIN)
172             .addOrder( Order.desc("name") )
173             .list();
174
175         s.delete( parents.get(0) );
176         s.delete( parents.get(1) );
177
178         t.commit();
179         s.close();
180     }
181     
182     public void testSubselectFetchCriteria() {
183         Session s = openSession();
184         Transaction t = s.beginTransaction();
185         Parent p = new Parent("foo");
186         p.getChildren().add( new Child("foo1") );
187         p.getChildren().add( new Child("foo2") );
188         Parent q = new Parent("bar");
189         q.getChildren().add( new Child("bar1") );
190         q.getChildren().add( new Child("bar2") );
191         q.getMoreChildren().addAll( p.getChildren() );
192         s.persist(p);
193         s.persist(q);
194         t.commit();
195         s.close();
196         
197         s = openSession();
198         t = s.beginTransaction();
199         
200         getSessions().getStatistics().clear();
201         
202         List JavaDoc parents = s.createCriteria(Parent.class)
203             .add( Property.forName("name").between("bar", "foo") )
204             .addOrder( Order.desc("name") )
205             .list();
206         p = (Parent) parents.get(0);
207         q = (Parent) parents.get(1);
208
209         assertFalse( Hibernate.isInitialized( p.getChildren() ) );
210         assertFalse( Hibernate.isInitialized( q.getChildren() ) );
211
212         assertEquals( p.getChildren().size(), 2 );
213                 
214         assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
215         
216         assertTrue( Hibernate.isInitialized( q.getChildren() ) );
217         
218         assertEquals( q.getChildren().size(), 2 );
219         
220         assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
221         
222         assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
223         assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
224
225         assertEquals( p.getMoreChildren().size(), 0 );
226         
227         assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
228     
229         assertEquals( q.getMoreChildren().size(), 2 );
230         
231         assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
232         
233         assertEquals( 3, getSessions().getStatistics().getPrepareStatementCount() );
234
235         Child c = (Child) p.getChildren().get(0);
236         c.getFriends().size();
237
238         s.delete(p);
239         s.delete(q);
240
241         t.commit();
242         s.close();
243     }
244     
245     protected String JavaDoc[] getMappings() {
246         return new String JavaDoc[] { "subselectfetch/ParentChild.hbm.xml" };
247     }
248
249     public static Test suite() {
250         return new TestSuite(SubselectFetchTest.class);
251     }
252
253     public String JavaDoc getCacheConcurrencyStrategy() {
254         return null;
255     }
256
257 }
258
259
Popular Tags