KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > unconstrained > UnconstrainedTest


1 //$Id: UnconstrainedTest.java,v 1.2 2005/04/03 05:07:20 oneovthafew Exp $
2
package org.hibernate.test.unconstrained;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.hibernate.FetchMode;
8 import org.hibernate.Hibernate;
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.criterion.Restrictions;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * @author Gavin King
16  */

17 public class UnconstrainedTest extends TestCase {
18     
19     public UnconstrainedTest(String JavaDoc str) {
20         super(str);
21     }
22     
23     public void testUnconstrainedNoCache() {
24         Session session = openSession();
25         Transaction tx = session.beginTransaction();
26         Person p = new Person("gavin");
27         p.setEmployeeId("123456");
28         session.persist(p);
29         tx.commit();
30         session.close();
31         
32         getSessions().evict(Person.class);
33         
34         session = openSession();
35         tx = session.beginTransaction();
36         p = (Person) session.get(Person.class, "gavin");
37         assertNull( p.getEmployee() );
38         p.setEmployee( new Employee("123456") );
39         tx.commit();
40         session.close();
41
42         getSessions().evict(Person.class);
43         
44         session = openSession();
45         tx = session.beginTransaction();
46         p = (Person) session.get(Person.class, "gavin");
47         assertTrue( Hibernate.isInitialized( p.getEmployee() ) );
48         assertNotNull( p.getEmployee() );
49         session.delete(p);
50         tx.commit();
51         session.close();
52     }
53
54     public void testUnconstrainedOuterJoinFetch() {
55         Session session = openSession();
56         Transaction tx = session.beginTransaction();
57         Person p = new Person("gavin");
58         p.setEmployeeId("123456");
59         session.persist(p);
60         tx.commit();
61         session.close();
62         
63         getSessions().evict(Person.class);
64         
65         session = openSession();
66         tx = session.beginTransaction();
67         p = (Person) session.createCriteria(Person.class)
68             .setFetchMode("employee", FetchMode.JOIN)
69             .add( Restrictions.idEq("gavin") )
70             .uniqueResult();
71         assertNull( p.getEmployee() );
72         p.setEmployee( new Employee("123456") );
73         tx.commit();
74         session.close();
75
76         getSessions().evict(Person.class);
77         
78         session = openSession();
79         tx = session.beginTransaction();
80         p = (Person) session.createCriteria(Person.class)
81             .setFetchMode("employee", FetchMode.JOIN)
82             .add( Restrictions.idEq("gavin") )
83             .uniqueResult();
84         assertTrue( Hibernate.isInitialized( p.getEmployee() ) );
85         assertNotNull( p.getEmployee() );
86         session.delete(p);
87         tx.commit();
88         session.close();
89     }
90
91     public void testUnconstrained() {
92         Session session = openSession();
93         Transaction tx = session.beginTransaction();
94         Person p = new Person("gavin");
95         p.setEmployeeId("123456");
96         session.persist(p);
97         tx.commit();
98         session.close();
99         
100         session = openSession();
101         tx = session.beginTransaction();
102         p = (Person) session.get(Person.class, "gavin");
103         assertNull( p.getEmployee() );
104         p.setEmployee( new Employee("123456") );
105         tx.commit();
106         session.close();
107
108         session = openSession();
109         tx = session.beginTransaction();
110         p = (Person) session.get(Person.class, "gavin");
111         assertTrue( Hibernate.isInitialized( p.getEmployee() ) );
112         assertNotNull( p.getEmployee() );
113         session.delete(p);
114         tx.commit();
115         session.close();
116     }
117
118     protected String JavaDoc[] getMappings() {
119         return new String JavaDoc[] { "unconstrained/Person.hbm.xml" };
120     }
121
122     public static Test suite() {
123         return new TestSuite(UnconstrainedTest.class);
124     }
125
126 }
127
128
Popular Tags