KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > orphan > OrphanTest


1 //$Id: OrphanTest.java,v 1.4 2005/06/16 05:03:13 oneovthafew Exp $
2
package org.hibernate.test.orphan;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.hibernate.Hibernate;
8 import org.hibernate.LockMode;
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.test.TestCase;
12 import org.hibernate.util.SerializationHelper;
13
14 /**
15  * @author Gavin King
16  */

17 public class OrphanTest extends TestCase {
18     
19     public OrphanTest(String JavaDoc str) {
20         super(str);
21     }
22     
23     public void testOrphanDeleteOnDelete() {
24         Session session = openSession();
25         Transaction t = session.beginTransaction();
26         Product prod = new Product();
27         prod.setName("Widget");
28         Part part = new Part();
29         part.setName("Widge");
30         part.setDescription("part if a Widget");
31         prod.getParts().add(part);
32         Part part2 = new Part();
33         part2.setName("Get");
34         part2.setDescription("another part if a Widget");
35         prod.getParts().add(part2);
36         session.persist(prod);
37         session.flush();
38         
39         prod.getParts().remove(part);
40         
41         session.delete(prod);
42         
43         t.commit();
44         session.close();
45         
46         session = openSession();
47         t = session.beginTransaction();
48         assertNull( session.get(Part.class, "Widge") );
49         assertNull( session.get(Part.class, "Get") );
50         assertNull( session.get(Product.class, "Widget") );
51         t.commit();
52         session.close();
53     }
54     
55     public void testOrphanDeleteAfterPersist() {
56         Session session = openSession();
57         Transaction t = session.beginTransaction();
58         Product prod = new Product();
59         prod.setName("Widget");
60         Part part = new Part();
61         part.setName("Widge");
62         part.setDescription("part if a Widget");
63         prod.getParts().add(part);
64         Part part2 = new Part();
65         part2.setName("Get");
66         part2.setDescription("another part if a Widget");
67         prod.getParts().add(part2);
68         session.persist(prod);
69         
70         prod.getParts().remove(part);
71         
72         t.commit();
73         session.close();
74         
75         session = openSession();
76         t = session.beginTransaction();
77         assertNull( session.get(Part.class, "Widge") );
78         assertNotNull( session.get(Part.class, "Get") );
79         session.delete( session.get(Product.class, "Widget") );
80         t.commit();
81         session.close();
82     }
83     
84     public void testOrphanDeleteAfterPersistAndFlush() {
85         Session session = openSession();
86         Transaction t = session.beginTransaction();
87         Product prod = new Product();
88         prod.setName("Widget");
89         Part part = new Part();
90         part.setName("Widge");
91         part.setDescription("part if a Widget");
92         prod.getParts().add(part);
93         Part part2 = new Part();
94         part2.setName("Get");
95         part2.setDescription("another part if a Widget");
96         prod.getParts().add(part2);
97         session.persist(prod);
98         session.flush();
99         
100         prod.getParts().remove(part);
101         
102         t.commit();
103         session.close();
104         
105         session = openSession();
106         t = session.beginTransaction();
107         assertNull( session.get(Part.class, "Widge") );
108         assertNotNull( session.get(Part.class, "Get") );
109         session.delete( session.get(Product.class, "Widget") );
110         t.commit();
111         session.close();
112     }
113     
114     public void testOrphanDeleteAfterLock() {
115         Session session = openSession();
116         Transaction t = session.beginTransaction();
117         Product prod = new Product();
118         prod.setName("Widget");
119         Part part = new Part();
120         part.setName("Widge");
121         part.setDescription("part if a Widget");
122         prod.getParts().add(part);
123         Part part2 = new Part();
124         part2.setName("Get");
125         part2.setDescription("another part if a Widget");
126         prod.getParts().add(part2);
127         session.persist(prod);
128         t.commit();
129         session.close();
130         
131         
132         session = openSession();
133         t = session.beginTransaction();
134         session.lock(prod, LockMode.READ);
135         prod.getParts().remove(part);
136         t.commit();
137         session.close();
138         
139         session = openSession();
140         t = session.beginTransaction();
141         assertNull( session.get(Part.class, "Widge") );
142         assertNotNull( session.get(Part.class, "Get") );
143         session.delete( session.get(Product.class, "Widget") );
144         t.commit();
145         session.close();
146     }
147     
148     public void testOrphanDeleteOnSaveOrUpdate() {
149         Session session = openSession();
150         Transaction t = session.beginTransaction();
151         Product prod = new Product();
152         prod.setName("Widget");
153         Part part = new Part();
154         part.setName("Widge");
155         part.setDescription("part if a Widget");
156         prod.getParts().add(part);
157         Part part2 = new Part();
158         part2.setName("Get");
159         part2.setDescription("another part if a Widget");
160         prod.getParts().add(part2);
161         session.persist(prod);
162         t.commit();
163         session.close();
164         
165         prod.getParts().remove(part);
166         
167         session = openSession();
168         t = session.beginTransaction();
169         session.saveOrUpdate(prod);
170         t.commit();
171         session.close();
172         
173         session = openSession();
174         t = session.beginTransaction();
175         assertNull( session.get(Part.class, "Widge") );
176         assertNotNull( session.get(Part.class, "Get") );
177         session.delete( session.get(Product.class, "Widget") );
178         t.commit();
179         session.close();
180     }
181     
182     public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() {
183         Session session = openSession();
184         Transaction t = session.beginTransaction();
185         Product prod = new Product();
186         prod.setName("Widget");
187         Part part = new Part();
188         part.setName("Widge");
189         part.setDescription("part if a Widget");
190         prod.getParts().add(part);
191         Part part2 = new Part();
192         part2.setName("Get");
193         part2.setDescription("another part if a Widget");
194         prod.getParts().add(part2);
195         session.persist(prod);
196         t.commit();
197         session.close();
198         
199         prod.getParts().remove(part);
200         
201         prod = (Product) SerializationHelper.clone(prod);
202         
203         session = openSession();
204         t = session.beginTransaction();
205         session.saveOrUpdate(prod);
206         t.commit();
207         session.close();
208         
209         session = openSession();
210         t = session.beginTransaction();
211         assertNull( session.get(Part.class, "Widge") );
212         assertNotNull( session.get(Part.class, "Get") );
213         session.delete( session.get(Product.class, "Widget") );
214         t.commit();
215         session.close();
216     }
217     
218     public void testOrphanDelete() {
219         Session session = openSession();
220         Transaction t = session.beginTransaction();
221         Product prod = new Product();
222         prod.setName("Widget");
223         Part part = new Part();
224         part.setName("Widge");
225         part.setDescription("part if a Widget");
226         prod.getParts().add(part);
227         Part part2 = new Part();
228         part2.setName("Get");
229         part2.setDescription("another part if a Widget");
230         prod.getParts().add(part2);
231         session.persist(prod);
232         t.commit();
233         session.close();
234         
235         getSessions().evict(Product.class);
236         getSessions().evict(Part.class);
237         
238         session = openSession();
239         t = session.beginTransaction();
240         prod = (Product) session.get(Product.class, "Widget");
241         assertTrue( Hibernate.isInitialized( prod.getParts() ) );
242         part = (Part) session.get(Part.class, "Widge");
243         prod.getParts().remove(part);
244         t.commit();
245         session.close();
246         
247         session = openSession();
248         t = session.beginTransaction();
249         assertNull( session.get(Part.class, "Widge") );
250         assertNotNull( session.get(Part.class, "Get") );
251         session.delete( session.get(Product.class, "Widget") );
252         t.commit();
253         session.close();
254     }
255     
256     public void testOrphanDeleteOnMerge() {
257         Session session = openSession();
258         Transaction t = session.beginTransaction();
259         Product prod = new Product();
260         prod.setName("Widget");
261         Part part = new Part();
262         part.setName("Widge");
263         part.setDescription("part if a Widget");
264         prod.getParts().add(part);
265         Part part2 = new Part();
266         part2.setName("Get");
267         part2.setDescription("another part if a Widget");
268         prod.getParts().add(part2);
269         session.persist(prod);
270         t.commit();
271         session.close();
272         
273         prod.getParts().remove(part);
274         
275         session = openSession();
276         t = session.beginTransaction();
277         session.merge(prod);
278         t.commit();
279         session.close();
280         
281         session = openSession();
282         t = session.beginTransaction();
283         assertNull( session.get(Part.class, "Widge") );
284         assertNotNull( session.get(Part.class, "Get") );
285         session.delete( session.get(Product.class, "Widget") );
286         t.commit();
287         session.close();
288     }
289     
290     
291     protected String JavaDoc[] getMappings() {
292         return new String JavaDoc[] { "orphan/Product.hbm.xml" };
293     }
294
295     public static Test suite() {
296         return new TestSuite(OrphanTest.class);
297     }
298
299 }
300
301
Popular Tags