KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > OneToOneCacheTest


1 //$Id: OneToOneCacheTest.java,v 1.2 2004/09/26 05:27:23 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * Simple testcase to illustrate HB-992
16  *
17  * @author Wolfgang Voelkl, michael
18  */

19 public class OneToOneCacheTest extends TestCase {
20
21     public OneToOneCacheTest(String JavaDoc x) {
22         super(x);
23     }
24
25     public static Test suite() {
26         return new TestSuite(OneToOneCacheTest.class);
27     }
28
29     private Serializable JavaDoc generatedId;
30
31     public void testOneToOneCache() throws HibernateException {
32
33             //create a new MainObject
34
createMainObject();
35             // load the MainObject
36
readMainObject();
37
38             //create and add Ojbect2
39
addObject2();
40
41             //here the newly created Object2 is written to the database
42
//but the MainObject does not know it yet
43
MainObject mainObject = readMainObject();
44
45             TestCase.assertNotNull(mainObject.getObj2());
46
47             // after evicting, it works.
48
getSessions().evict(MainObject.class);
49
50             mainObject = readMainObject();
51
52             TestCase.assertNotNull(mainObject.getObj2());
53
54     }
55
56     /**
57      * creates a new MainObject
58      *
59      * one hibernate transaction !
60      */

61     private void createMainObject() throws HibernateException {
62         Session session = openSession();
63         Transaction tx = session.beginTransaction();
64
65         MainObject mo = new MainObject();
66         mo.setDescription("Main Test");
67
68         generatedId = session.save(mo);
69
70         tx.commit();
71         session.close();
72     }
73
74     /**
75      * loads the newly created MainObject
76      * and adds a new Object2 to it
77      *
78      * one hibernate transaction
79      */

80     private void addObject2() throws HibernateException {
81         Session session = openSession();
82         Transaction tx = session.beginTransaction();
83
84         MainObject mo =
85             (MainObject) session.load(MainObject.class, generatedId);
86
87         Object2 toAdd = new Object2();
88         toAdd.setDummy("test");
89
90         //toAdd should now be saved by cascade
91
mo.setObj2(toAdd);
92
93         tx.commit();
94         session.close();
95     }
96
97     /**
98      * reads the newly created MainObject
99      * and its Object2 if it exists
100      *
101      * one hibernate transaction
102      */

103     private MainObject readMainObject() throws HibernateException {
104         Long JavaDoc returnId = null;
105         Session session = openSession();
106         Transaction tx = session.beginTransaction();
107
108         Serializable JavaDoc id = generatedId;
109
110         MainObject mo = (MainObject) session.load(MainObject.class, id);
111
112         tx.commit();
113         session.close();
114
115         return mo;
116     }
117
118
119     /* (non-Javadoc)
120      * @see org.hibernate.test.TestCase#getMappings()
121      */

122     protected String JavaDoc[] getMappings() {
123         return new String JavaDoc[] { "legacy/Object2.hbm.xml", "legacy/MainObject.hbm.xml" };
124     }
125 }
126
Popular Tags