KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > joinfetch > JoinFetchTest


1 //$Id: JoinFetchTest.java,v 1.8 2005/06/17 05:23:15 oneovthafew Exp $
2
package org.hibernate.test.joinfetch;
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.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class JoinFetchTest extends TestCase {
21     
22     public JoinFetchTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testJoinFetch() {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         s.createQuery( "delete from Bid" ).executeUpdate();
30         s.createQuery( "delete from Comment" ).executeUpdate();
31         s.createQuery( "delete from Item" ).executeUpdate();
32         t.commit();
33         s.close();
34         
35         Category cat = new Category("Photography");
36         Item i = new Item(cat, "Camera");
37         Bid b = new Bid(i, 100.0f);
38         new Bid(i, 105.0f);
39         new Comment(i, "This looks like a really good deal");
40         new Comment(i, "Is it the latest version?");
41         new Comment(i, "<comment deleted>");
42         System.out.println( b.getTimestamp() );
43         
44         s = openSession();
45         t = s.beginTransaction();
46         s.persist(cat);
47         s.persist(i);
48         t.commit();
49         s.close();
50         
51         getSessions().evict(Item.class);
52
53         s = openSession();
54         t = s.beginTransaction();
55         i = (Item) s.get( Item.class, i.getId() );
56         assertTrue( Hibernate.isInitialized( i.getBids() ) );
57         assertEquals( i.getBids().size(), 2 );
58         assertTrue( Hibernate.isInitialized( i.getComments() ) );
59         assertEquals( i.getComments().size(), 3 );
60         t.commit();
61         s.close();
62
63         getSessions().evict(Bid.class);
64
65         s = openSession();
66         t = s.beginTransaction();
67         b = (Bid) s.get( Bid.class, b.getId() );
68         assertTrue( Hibernate.isInitialized( b.getItem() ) );
69         assertTrue( Hibernate.isInitialized( b.getItem().getComments() ) );
70         assertEquals( b.getItem().getComments().size(), 3 );
71         System.out.println( b.getTimestamp() );
72         t.commit();
73         s.close();
74
75         getSessions().evictCollection(Item.class.getName() + ".bids");
76         
77         s = openSession();
78         t = s.beginTransaction();
79         i = (Item) s.createCriteria( Item.class )
80             .setFetchMode("bids", FetchMode.SELECT)
81             .setFetchMode("comments", FetchMode.SELECT)
82             .uniqueResult();
83         assertFalse( Hibernate.isInitialized( i.getBids() ) );
84         assertFalse( Hibernate.isInitialized( i.getComments() ) );
85         b = (Bid) i.getBids().iterator().next();
86         assertTrue( Hibernate.isInitialized( b.getItem() ) );
87         t.commit();
88         s.close();
89         
90         s = openSession();
91         t = s.beginTransaction();
92         i = (Item) s.createQuery("from Item i left join fetch i.bids left join fetch i.comments").uniqueResult();
93         assertTrue( Hibernate.isInitialized( i.getBids() ) );
94         assertTrue( Hibernate.isInitialized( i.getComments() ) );
95         assertEquals( i.getComments().size(), 3 );
96         assertEquals( i.getBids().size(), 2 );
97         t.commit();
98         s.close();
99
100         s = openSession();
101         t = s.beginTransaction();
102         Object JavaDoc[] row = (Object JavaDoc[]) s.getNamedQuery(Item.class.getName() + ".all").list().get(0);
103         i = (Item) row[0];
104         assertTrue( Hibernate.isInitialized( i.getBids() ) );
105         assertTrue( Hibernate.isInitialized( i.getComments() ) );
106         assertEquals( i.getComments().size(), 3 );
107         assertEquals( i.getBids().size(), 2 );
108         t.commit();
109         s.close();
110
111         s = openSession();
112         t = s.beginTransaction();
113         i = (Item) s.createCriteria(Item.class).uniqueResult();
114         assertTrue( Hibernate.isInitialized( i.getBids() ) );
115         assertTrue( Hibernate.isInitialized( i.getComments() ) );
116         assertEquals( i.getComments().size(), 3 );
117         assertEquals( i.getBids().size(), 2 );
118         t.commit();
119         s.close();
120
121         s = openSession();
122         t = s.beginTransaction();
123         List JavaDoc bids = s.createQuery("from Bid b left join fetch b.item i left join fetch i.category").list();
124         Bid bid = (Bid) bids.get(0);
125         assertTrue( Hibernate.isInitialized( bid.getItem() ) );
126         assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
127         t.commit();
128         s.close();
129
130         s = openSession();
131         t = s.beginTransaction();
132         List JavaDoc pairs = s.createQuery("from Item i left join i.bids b left join fetch i.category").list();
133         Item item = (Item) ( (Object JavaDoc[]) pairs.get(0) )[0];
134         assertFalse( Hibernate.isInitialized( item.getBids() ) );
135         assertTrue( Hibernate.isInitialized( item.getCategory() ) );
136         s.clear();
137         pairs = s.createQuery("from Item i left join i.bids b left join i.category").list();
138         item = (Item) ( (Object JavaDoc[]) pairs.get(0) )[0];
139         assertFalse( Hibernate.isInitialized( item.getBids() ) );
140         assertTrue( Hibernate.isInitialized( item.getCategory() ) );
141         s.clear();
142         pairs = s.createQuery("from Bid b left join b.item i left join fetch i.category").list();
143         bid = (Bid) ( (Object JavaDoc[]) pairs.get(0) )[0];
144         assertTrue( Hibernate.isInitialized( bid.getItem() ) );
145         assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
146         s.clear();
147         pairs = s.createQuery("from Bid b left join b.item i left join i.category").list();
148         bid = (Bid) ( (Object JavaDoc[]) pairs.get(0) )[0];
149         assertTrue( Hibernate.isInitialized( bid.getItem() ) );
150         assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
151         t.commit();
152         s.close();
153
154         s = openSession();
155         t = s.beginTransaction();
156         s.createQuery( "delete from Bid" ).executeUpdate();
157         s.createQuery( "delete from Comment" ).executeUpdate();
158         s.createQuery( "delete from Item" ).executeUpdate();
159         t.commit();
160         s.close();
161
162     }
163     
164     public void testJoinFetchManyToMany() {
165         Session s = openSession();
166         Transaction t = s.beginTransaction();
167         Group hb = new Group("hibernate");
168         User gavin = new User("gavin");
169         User max = new User("max");
170         hb.getUsers().put("gavin", gavin);
171         hb.getUsers().put("max", max);
172         gavin.getGroups().put("hibernate", hb);
173         max.getGroups().put("hibernate", hb);
174         s.persist(hb);
175         t.commit();
176         s.close();
177         
178         s = openSession();
179         t = s.beginTransaction();
180         hb = (Group) s.get(Group.class, "hibernate");
181         assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
182         gavin = (User) hb.getUsers().get("gavin");
183         assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
184         max = (User) s.get(User.class, "max");
185         assertFalse( Hibernate.isInitialized( max.getGroups() ) );
186         t.commit();
187         s.close();
188
189         s = openSession();
190         t = s.beginTransaction();
191         hb = (Group) s.createCriteria(Group.class)
192             .setFetchMode("users", FetchMode.JOIN)
193             .setFetchMode("users.groups", FetchMode.JOIN)
194             .uniqueResult();
195         assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
196         gavin = (User) hb.getUsers().get("gavin");
197         assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
198         max = (User) s.get(User.class, "max");
199         assertTrue( Hibernate.isInitialized( max.getGroups() ) );
200         t.commit();
201         s.close();
202
203         s = openSession();
204         t = s.beginTransaction();
205         s.delete(hb);
206         t.commit();
207         s.close();
208     }
209     
210     protected String JavaDoc[] getMappings() {
211         return new String JavaDoc[] { "joinfetch/ItemBid.hbm.xml", "joinfetch/UserGroup.hbm.xml" };
212     }
213
214     protected void configure(Configuration cfg) {
215         cfg.setProperty(Environment.MAX_FETCH_DEPTH, "10");
216         cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
217     }
218
219     public static Test suite() {
220         return new TestSuite(JoinFetchTest.class);
221     }
222
223 }
224
225
Popular Tags