Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/test/joinfetch/JoinFetchTest.java


1   //$Id: JoinFetchTest.java 9879 2006-05-04 19:07:57Z max.andersen@jboss.com $
2   package org.hibernate.test.joinfetch;
3   
4   import java.util.List;
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.Projections;
16  import org.hibernate.criterion.Restrictions;
17  import org.hibernate.test.TestCase;
18  
19  /**
20   * @author Gavin King
21   */
22  public class JoinFetchTest extends TestCase {
23    
24    public JoinFetchTest(String str) {
25      super(str);
26    }
27    
28    public void testProjection() {
29      Session s = openSession();
30      Transaction t = s.beginTransaction();
31      s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult();
32      s.createCriteria(Item.class).uniqueResult();
33      t.commit();
34      s.close();
35    }
36  
37    public void testJoinFetch() {
38      Session s = openSession();
39      Transaction t = s.beginTransaction();
40      s.createQuery( "delete from Bid" ).executeUpdate();
41      s.createQuery( "delete from Comment" ).executeUpdate();
42      s.createQuery( "delete from Item" ).executeUpdate();
43      t.commit();
44      s.close();
45      
46      Category cat = new Category("Photography");
47      Item i = new Item(cat, "Camera");
48      Bid b = new Bid(i, 100.0f);
49      new Bid(i, 105.0f);
50      new Comment(i, "This looks like a really good deal");
51      new Comment(i, "Is it the latest version?");
52      new Comment(i, "<comment deleted>");
53      System.out.println( b.getTimestamp() );
54      
55      s = openSession();
56      t = s.beginTransaction();
57      s.persist(cat);
58      s.persist(i);
59      t.commit();
60      s.close();
61      
62      getSessions().evict(Item.class);
63  
64      s = openSession();
65      t = s.beginTransaction();
66      i = (Item) s.get( Item.class, i.getId() );
67      assertTrue( Hibernate.isInitialized( i.getBids() ) );
68      assertEquals( i.getBids().size(), 2 );
69      assertTrue( Hibernate.isInitialized( i.getComments() ) );
70      assertEquals( i.getComments().size(), 3 );
71      t.commit();
72      s.close();
73  
74      getSessions().evict(Bid.class);
75  
76      s = openSession();
77      t = s.beginTransaction();
78      b = (Bid) s.get( Bid.class, b.getId() );
79      assertTrue( Hibernate.isInitialized( b.getItem() ) );
80      assertTrue( Hibernate.isInitialized( b.getItem().getComments() ) );
81      assertEquals( b.getItem().getComments().size(), 3 );
82      System.out.println( b.getTimestamp() );
83      t.commit();
84      s.close();
85  
86      getSessions().evictCollection(Item.class.getName() + ".bids");
87      
88      s = openSession();
89      t = s.beginTransaction();
90      i = (Item) s.createCriteria( Item.class )
91        .setFetchMode("bids", FetchMode.SELECT)
92        .setFetchMode("comments", FetchMode.SELECT)
93        .uniqueResult();
94      assertFalse( Hibernate.isInitialized( i.getBids() ) );
95      assertFalse( Hibernate.isInitialized( i.getComments() ) );
96      b = (Bid) i.getBids().iterator().next();
97      assertTrue( Hibernate.isInitialized( b.getItem() ) );
98      t.commit();
99      s.close();
100     
101     s = openSession();
102     t = s.beginTransaction();
103     i = (Item) s.createQuery("from Item i left join fetch i.bids left join fetch i.comments").uniqueResult();
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     Object[] row = (Object[]) s.getNamedQuery(Item.class.getName() + ".all").list().get(0);
114     i = (Item) row[0];
115     assertTrue( Hibernate.isInitialized( i.getBids() ) );
116     assertTrue( Hibernate.isInitialized( i.getComments() ) );
117     assertEquals( i.getComments().size(), 3 );
118     assertEquals( i.getBids().size(), 2 );
119     t.commit();
120     s.close();
121 
122     s = openSession();
123     t = s.beginTransaction();
124     i = (Item) s.createCriteria(Item.class).uniqueResult();
125     assertTrue( Hibernate.isInitialized( i.getBids() ) );
126     assertTrue( Hibernate.isInitialized( i.getComments() ) );
127     assertEquals( i.getComments().size(), 3 );
128     assertEquals( i.getBids().size(), 2 );
129     t.commit();
130     s.close();
131 
132     s = openSession();
133     t = s.beginTransaction();
134     List bids = s.createQuery("from Bid b left join fetch b.item i left join fetch i.category").list();
135     Bid bid = (Bid) bids.get(0);
136     assertTrue( Hibernate.isInitialized( bid.getItem() ) );
137     assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
138     t.commit();
139     s.close();
140 
141     s = openSession();
142     t = s.beginTransaction();
143     List pairs = s.createQuery("from Item i left join i.bids b left join fetch i.category").list();
144     Item item = (Item) ( (Object[]) pairs.get(0) )[0];
145     assertFalse( Hibernate.isInitialized( item.getBids() ) );
146     assertTrue( Hibernate.isInitialized( item.getCategory() ) );
147     s.clear();
148     pairs = s.createQuery("from Item i left join i.bids b left join i.category").list();
149     item = (Item) ( (Object[]) pairs.get(0) )[0];
150     assertFalse( Hibernate.isInitialized( item.getBids() ) );
151     assertTrue( Hibernate.isInitialized( item.getCategory() ) );
152     s.clear();
153     pairs = s.createQuery("from Bid b left join b.item i left join fetch i.category").list();
154     bid = (Bid) ( (Object[]) pairs.get(0) )[0];
155     assertTrue( Hibernate.isInitialized( bid.getItem() ) );
156     assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
157     s.clear();
158     pairs = s.createQuery("from Bid b left join b.item i left join i.category").list();
159     bid = (Bid) ( (Object[]) pairs.get(0) )[0];
160     assertTrue( Hibernate.isInitialized( bid.getItem() ) );
161     assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
162     t.commit();
163     s.close();
164 
165     s = openSession();
166     t = s.beginTransaction();
167     s.createQuery( "delete from Bid" ).executeUpdate();
168     s.createQuery( "delete from Comment" ).executeUpdate();
169     s.createQuery( "delete from Item" ).executeUpdate();
170     t.commit();
171     s.close();
172 
173   }
174   
175   public void testCollectionFilter() {
176     Session s = openSession();
177     Transaction t = s.beginTransaction();
178     Group hb = new Group("hibernate");
179     User gavin = new User("gavin");
180     User max = new User("max");
181     hb.getUsers().put("gavin", gavin);
182     hb.getUsers().put("max", max);
183     gavin.getGroups().put("hibernate", hb);
184     max.getGroups().put("hibernate", hb);
185     s.persist(hb);
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.SELECT)
193         .add( Restrictions.idEq("hibernate") )
194         .uniqueResult();
195     assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
196     //gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult();
197     Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult();
198     assertEquals( new Long(2), size );
199     assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
200     s.delete(hb);
201     t.commit();
202     s.close();
203     
204   }
205   
206   public void testJoinFetchManyToMany() {
207     Session s = openSession();
208     Transaction t = s.beginTransaction();
209     Group hb = new Group("hibernate");
210     User gavin = new User("gavin");
211     User max = new User("max");
212     hb.getUsers().put("gavin", gavin);
213     hb.getUsers().put("max", max);
214     gavin.getGroups().put("hibernate", hb);
215     max.getGroups().put("hibernate", hb);
216     s.persist(hb);
217     t.commit();
218     s.close();
219     
220     s = openSession();
221     t = s.beginTransaction();
222     hb = (Group) s.get(Group.class, "hibernate");
223     assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
224     gavin = (User) hb.getUsers().get("gavin");
225     assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
226     max = (User) s.get(User.class, "max");
227     assertFalse( Hibernate.isInitialized( max.getGroups() ) );
228     t.commit();
229     s.close();
230 
231     s = openSession();
232     t = s.beginTransaction();
233     hb = (Group) s.createCriteria(Group.class)
234       .setFetchMode("users", FetchMode.JOIN)
235       .setFetchMode("users.groups", FetchMode.JOIN)
236       .uniqueResult();
237     assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
238     gavin = (User) hb.getUsers().get("gavin");
239     assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
240     max = (User) s.get(User.class, "max");
241     assertTrue( Hibernate.isInitialized( max.getGroups() ) );
242     t.commit();
243     s.close();
244 
245     s = openSession();
246     t = s.beginTransaction();
247     s.delete(hb);
248     t.commit();
249     s.close();
250   }
251   
252   protected String[] getMappings() {
253     return new String[] { "joinfetch/ItemBid.hbm.xml", "joinfetch/UserGroup.hbm.xml" };
254   }
255 
256   protected void configure(Configuration cfg) {
257     cfg.setProperty(Environment.MAX_FETCH_DEPTH, "10");
258     cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
259   }
260 
261   public static Test suite() {
262     return new TestSuite(JoinFetchTest.class);
263   }
264 
265 }
266