KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > cid > CompositeIdTest


1 //$Id: CompositeIdTest.java,v 1.10 2005/05/16 02:19:13 oneovthafew Exp $
2
package org.hibernate.test.cid;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11
12 import org.hibernate.Hibernate;
13 import org.hibernate.Session;
14 import org.hibernate.Transaction;
15 import org.hibernate.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class CompositeIdTest extends TestCase {
21     
22     public CompositeIdTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testQuery() {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         s.createQuery("from LineItem ol where ol.order.id.customerId = 'C111'").list();
30         t.commit();
31         s.close();
32     }
33     
34     public void testCompositeIds() {
35         Session s = openSession();
36         Transaction t = s.beginTransaction();
37         
38         Product p = new Product();
39         p.setProductId("A123");
40         p.setDescription("nipple ring");
41         p.setPrice( new BigDecimal JavaDoc(1.0) );
42         p.setNumberAvailable(1004);
43         s.persist(p);
44         
45         Product p2 = new Product();
46         p2.setProductId("X525");
47         p2.setDescription("nose stud");
48         p2.setPrice( new BigDecimal JavaDoc(3.0) );
49         p2.setNumberAvailable(105);
50         s.persist(p2);
51         
52         Customer c = new Customer();
53         c.setAddress("St Kilda Rd, MEL, 3000");
54         c.setName("Virginia");
55         c.setCustomerId("C111");
56         s.persist(c);
57         
58         Order o = new Order(c);
59         o.setOrderDate( Calendar.getInstance() );
60         LineItem li = new LineItem(o, p);
61         li.setQuantity(2);
62
63         t.commit();
64         s.close();
65         
66         s = openSession();
67         t = s.beginTransaction();
68         o = (Order) s.get( Order.class, new Order.Id("C111", 0) );
69         assertEquals( o.getTotal().intValue(), 2 );
70         o.getCustomer().getName();
71         t.commit();
72         s.close();
73
74         s = openSession();
75         t = s.beginTransaction();
76         s.createQuery("from Customer c left join fetch c.orders o left join fetch o.lineItems li left join fetch li.product p").list();
77         t.commit();
78         s.close();
79         
80         s = openSession();
81         t = s.beginTransaction();
82         s.createQuery("from Order o left join fetch o.lineItems li left join fetch li.product p").list();
83         t.commit();
84         s.close();
85         
86         s = openSession();
87         t = s.beginTransaction();
88         Iterator JavaDoc iter = s.createQuery("select o.id, li.id from Order o join o.lineItems li").list().iterator();
89         while ( iter.hasNext() ) {
90             Object JavaDoc[] stuff = (Object JavaDoc[]) iter.next();
91             assertTrue(stuff.length==2);
92         }
93         iter = s.createQuery("from Order o join o.lineItems li").iterate();
94         while ( iter.hasNext() ) {
95             Object JavaDoc[] stuff = (Object JavaDoc[]) iter.next();
96             assertTrue(stuff.length==2);
97         }
98         t.commit();
99         s.close();
100         
101         s = openSession();
102         t = s.beginTransaction();
103         c = (Customer) s.get( Customer.class, "C111" );
104         Order o2 = new Order(c);
105         o2.setOrderDate( Calendar.getInstance() );
106         s.flush();
107         LineItem li2 = new LineItem(o2, p2);
108         li2.setQuantity(5);
109         List JavaDoc bigOrders = s.createQuery("from Order o where o.total>10.0").list();
110         assertEquals( bigOrders.size(), 1 );
111         t.commit();
112         s.close();
113         
114         s = openSession();
115         t = s.beginTransaction();
116         s.createQuery("delete from LineItem").executeUpdate();
117         s.createQuery("delete from Order").executeUpdate();
118         s.createQuery("delete from Customer").executeUpdate();
119         s.createQuery("delete from Product").executeUpdate();
120         t.commit();
121         s.close();
122     }
123
124     
125     public void testNonLazyFetch() {
126         Session s = openSession();
127         Transaction t = s.beginTransaction();
128         
129         Product p = new Product();
130         p.setProductId("A123");
131         p.setDescription("nipple ring");
132         p.setPrice( new BigDecimal JavaDoc(1.0) );
133         p.setNumberAvailable(1004);
134         s.persist(p);
135         
136         Product p2 = new Product();
137         p2.setProductId("X525");
138         p2.setDescription("nose stud");
139         p2.setPrice( new BigDecimal JavaDoc(3.0) );
140         p2.setNumberAvailable(105);
141         s.persist(p2);
142         
143         Customer c = new Customer();
144         c.setAddress("St Kilda Rd, MEL, 3000");
145         c.setName("Virginia");
146         c.setCustomerId("C111");
147         s.persist(c);
148         
149         Order o = new Order(c);
150         o.setOrderDate( Calendar.getInstance() );
151         LineItem li = new LineItem(o, p);
152         li.setQuantity(2);
153
154         t.commit();
155         s.close();
156         
157         s = openSession();
158         t = s.beginTransaction();
159         o = (Order) s.get( Order.class, new Order.Id("C111", 0) );
160         assertEquals( o.getTotal().intValue(), 2 );
161         o.getCustomer().getName();
162         t.commit();
163         s.close();
164
165         s = openSession();
166         t = s.beginTransaction();
167         o = (Order) s.createQuery("from Order o left join fetch o.lineItems li left join fetch li.product p").uniqueResult();
168         assertTrue( Hibernate.isInitialized( o.getLineItems() ) );
169         li = (LineItem) o.getLineItems().iterator().next();
170         assertTrue( Hibernate.isInitialized( li ) );
171         assertTrue( Hibernate.isInitialized( li.getProduct() ) );
172         t.commit();
173         s.close();
174
175         s = openSession();
176         t = s.beginTransaction();
177         o = (Order) s.createQuery("from Order o").uniqueResult();
178         assertTrue( Hibernate.isInitialized( o.getLineItems() ) );
179         li = (LineItem) o.getLineItems().iterator().next();
180         assertTrue( Hibernate.isInitialized( li ) );
181         assertFalse( Hibernate.isInitialized( li.getProduct() ) );
182         t.commit();
183         s.close();
184         
185         
186         s = openSession();
187         t = s.beginTransaction();
188         s.createQuery("delete from LineItem").executeUpdate();
189         s.createQuery("delete from Order").executeUpdate();
190         s.createQuery("delete from Customer").executeUpdate();
191         s.createQuery("delete from Product").executeUpdate();
192         t.commit();
193         s.close();
194         
195     }
196
197     public void testMultipleCollectionFetch() {
198         Session s = openSession();
199         Transaction t = s.beginTransaction();
200         
201         Product p = new Product();
202         p.setProductId("A123");
203         p.setDescription("nipple ring");
204         p.setPrice( new BigDecimal JavaDoc(1.0) );
205         p.setNumberAvailable(1004);
206         s.persist(p);
207         
208         Product p2 = new Product();
209         p2.setProductId("X525");
210         p2.setDescription("nose stud");
211         p2.setPrice( new BigDecimal JavaDoc(3.0) );
212         p2.setNumberAvailable(105);
213         s.persist(p2);
214         
215         Customer c = new Customer();
216         c.setAddress("St Kilda Rd, MEL, 3000");
217         c.setName("Virginia");
218         c.setCustomerId("C111");
219         s.persist(c);
220         
221         Order o = new Order(c);
222         o.setOrderDate( Calendar.getInstance() );
223         LineItem li = new LineItem(o, p);
224         li.setQuantity(2);
225         LineItem li2 = new LineItem(o, p2);
226         li2.setQuantity(3);
227
228         Order o2 = new Order(c);
229         o2.setOrderDate( Calendar.getInstance() );
230         LineItem li3 = new LineItem(o2, p);
231         li3.setQuantity(1);
232         LineItem li4 = new LineItem(o2, p2);
233         li4.setQuantity(1);
234
235         t.commit();
236         s.close();
237         
238         s = openSession();
239         t = s.beginTransaction();
240         c = (Customer) s.createQuery("from Customer c left join fetch c.orders o left join fetch o.lineItems li left join fetch li.product p").uniqueResult();
241         assertTrue( Hibernate.isInitialized( c.getOrders() ) );
242         assertEquals( c.getOrders().size(), 2 );
243         assertTrue( Hibernate.isInitialized( ( (Order) c.getOrders().get(0) ).getLineItems() ) );
244         assertTrue( Hibernate.isInitialized( ( (Order) c.getOrders().get(1) ).getLineItems() ) );
245         assertEquals( ( (Order) c.getOrders().get(0) ).getLineItems().size(), 2 );
246         assertEquals( ( (Order) c.getOrders().get(1) ).getLineItems().size(), 2 );
247         t.commit();
248         s.close();
249                 
250         s = openSession();
251         t = s.beginTransaction();
252         s.createQuery("delete from LineItem").executeUpdate();
253         s.createQuery("delete from Order").executeUpdate();
254         s.createQuery("delete from Customer").executeUpdate();
255         s.createQuery("delete from Product").executeUpdate();
256         t.commit();
257         s.close();
258     }
259
260     protected String JavaDoc[] getMappings() {
261         return new String JavaDoc[] {
262                 "cid/Customer.hbm.xml",
263                 "cid/Order.hbm.xml",
264                 "cid/LineItem.hbm.xml",
265                 "cid/Product.hbm.xml"
266         };
267     }
268
269     public static Test suite() {
270         return new TestSuite(CompositeIdTest.class);
271     }
272
273 }
274
275
Popular Tags