KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > onetoone > joined > OneToOneTest


1 //$Id: OneToOneTest.java,v 1.5 2005/02/21 14:41:01 oneovthafew Exp $
2
package org.hibernate.test.onetoone.joined;
3
4 import java.util.List JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.cfg.Configuration;
12 import org.hibernate.cfg.Environment;
13 import org.hibernate.stat.EntityStatistics;
14 import org.hibernate.test.TestCase;
15
16 /**
17  * @author Gavin King
18  */

19 public class OneToOneTest extends TestCase {
20     
21     public OneToOneTest(String JavaDoc str) {
22         super(str);
23     }
24     
25     public void testOneToOneOnSubclass() {
26         Person p = new Person();
27         p.name = "Gavin";
28         Address a = new Address();
29         a.entityName = "Gavin";
30         a.zip = "3181";
31         a.state = "VIC";
32         a.street = "Karbarook Ave";
33         p.address = a;
34         
35         Session s = openSession();
36         Transaction t = s.beginTransaction();
37         s.persist(p);
38         t.commit();
39         s.close();
40         
41         s = openSession();
42         t = s.beginTransaction();
43         
44         EntityStatistics addressStats = getSessions().getStatistics().getEntityStatistics( Address.class.getName() );
45         EntityStatistics mailingAddressStats = getSessions().getStatistics().getEntityStatistics("MailingAddress");
46
47         p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
48         assertNotNull(p.address); assertNull(p.mailingAddress);
49         s.clear();
50
51         p = (Person) s.createQuery("select p from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
52         assertNotNull(p.address); assertNull(p.mailingAddress);
53         s.clear();
54
55         Object JavaDoc[] stuff = (Object JavaDoc[]) s.createQuery("select p.name, p from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
56         assertEquals(stuff.length, 2);
57         p = (Person) stuff[1];
58         assertNotNull(p.address); assertNull(p.mailingAddress);
59         s.clear();
60
61         assertEquals( addressStats.getFetchCount(), 0 );
62         assertEquals( mailingAddressStats.getFetchCount(), 0 );
63         
64         p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
65         assertNotNull(p.address); assertNull(p.mailingAddress);
66         s.clear();
67         
68         assertEquals( addressStats.getFetchCount(), 0 );
69         assertEquals( mailingAddressStats.getFetchCount(), 1 );
70
71         p = (Person) s.createQuery("from Person").uniqueResult();
72         assertNotNull(p.address); assertNull(p.mailingAddress);
73         s.clear();
74         
75         assertEquals( addressStats.getFetchCount(), 0 );
76         assertEquals( mailingAddressStats.getFetchCount(), 2 );
77
78         p = (Person) s.createQuery("from Entity").uniqueResult();
79         assertNotNull(p.address); assertNull(p.mailingAddress);
80         s.clear();
81         
82         assertEquals( addressStats.getFetchCount(), 0 );
83         assertEquals( mailingAddressStats.getFetchCount(), 3 );
84
85         //note that in here join fetch is used for the nullable
86
//one-to-one, due to a very special case of default
87
p = (Person) s.get(Person.class, "Gavin");
88         assertNotNull(p.address); assertNull(p.mailingAddress);
89         s.clear();
90         
91         assertEquals( addressStats.getFetchCount(), 0 );
92         assertEquals( mailingAddressStats.getFetchCount(), 3 );
93
94         p = (Person) s.get(Entity.class, "Gavin");
95         assertNotNull(p.address); assertNull(p.mailingAddress);
96         s.clear();
97         
98         assertEquals( addressStats.getFetchCount(), 0 );
99         assertEquals( mailingAddressStats.getFetchCount(), 3 );
100         
101         t.commit();
102         s.close();
103         
104         s = openSession();
105         t = s.beginTransaction();
106         Org org = new Org();
107         org.name = "IFA";
108         Address a2 = new Address();
109         a2.entityName = "IFA";
110         a2.zip = "3181";
111         a2.state = "VIC";
112         a2.street = "Orrong Rd";
113         s.persist(org);
114         s.persist(a2);
115         t.commit();
116         s.close();
117         
118         s = openSession();
119         t = s.beginTransaction();
120         org = (Org) s.get(Entity.class, "IFA");
121         s.clear();
122         
123         List JavaDoc list = s.createQuery("from Entity e order by e.name").list();
124         p = (Person) list.get(0);
125         assertNotNull(p.address); assertNull(p.mailingAddress);
126         org = (Org) list.get(1);
127         s.clear();
128         
129         list = s.createQuery("from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name").list();
130         p = (Person) list.get(0);
131         org = (Org) list.get(1);
132         assertNotNull(p.address); assertNull(p.mailingAddress);
133         
134         s.delete(p);
135         
136         t.commit();
137         s.close();
138         
139     }
140     
141     protected String JavaDoc[] getMappings() {
142         return new String JavaDoc[] { "onetoone/joined/Person.hbm.xml" };
143     }
144
145     protected void configure(Configuration cfg) {
146         cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
147         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
148     }
149     
150     public static Test suite() {
151         return new TestSuite(OneToOneTest.class);
152     }
153
154 }
155
156
Popular Tags