KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > unidir > BackrefTest


1 //$Id: BackrefTest.java,v 1.6 2005/07/16 22:28:30 oneovthafew Exp $
2
package org.hibernate.test.unidir;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.hibernate.test.TestCase;
10
11 /**
12  * @author Gavin King
13  */

14 public class BackrefTest extends TestCase {
15     
16     public BackrefTest(String JavaDoc str) {
17         super(str);
18     }
19     
20     public void testBackRef() {
21         Session s = openSession();
22         Transaction t = s.beginTransaction();
23         Parent p = new Parent("Marc");
24         Parent p2 = new Parent("Nathalie");
25         Child c = new Child("Elvira");
26         Child c2 = new Child("Blase");
27         p.getChildren().add(c);
28         p.getChildren().add(c2);
29         s.persist(p);
30         s.persist(p2);
31         t.commit();
32         s.close();
33
34         s = openSession();
35         t = s.beginTransaction();
36         c = (Child) s.get(Child.class, "Elvira");
37         c.setAge(2);
38         t.commit();
39         s.close();
40
41         s = openSession();
42         t = s.beginTransaction();
43         p = (Parent) s.get(Parent.class, "Marc");
44         c = (Child) s.get(Child.class, "Elvira");
45         c.setAge(18);
46         t.commit();
47         s.close();
48
49         s = openSession();
50         t = s.beginTransaction();
51         p = (Parent) s.get(Parent.class, "Marc");
52         p2 = (Parent) s.get(Parent.class, "Nathalie");
53         c = (Child) s.get(Child.class, "Elvira");
54         assertEquals( p.getChildren().indexOf(c), 0 );
55         p.getChildren().remove(c);
56         p2.getChildren().add(c);
57         t.commit();
58
59         s.close();
60         s = openSession();
61         t = s.beginTransaction();
62         Parent p3 = new Parent("Marion");
63         p3.getChildren().add( new Child("Gavin") );
64         s.merge(p3);
65         t.commit();
66         s.close();
67     }
68     
69     protected String JavaDoc[] getMappings() {
70         return new String JavaDoc[] { "unidir/ParentChild.hbm.xml" };
71     }
72
73     public static Test suite() {
74         return new TestSuite(BackrefTest.class);
75     }
76
77     public String JavaDoc getCacheConcurrencyStrategy() {
78         return null;
79     }
80
81 }
82
83
Popular Tags