KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: OneToOneTest.java,v 1.4 2005/02/21 14:41:01 oneovthafew Exp $
2
package org.hibernate.test.onetoone.singletable;
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         assertEquals( addressStats.getFetchCount(), 0 );
52         assertEquals( mailingAddressStats.getFetchCount(), 0 );
53         
54         p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
55         assertNotNull(p.address); assertNull(p.mailingAddress);
56         s.clear();
57         
58         assertEquals( addressStats.getFetchCount(), 0 );
59         assertEquals( mailingAddressStats.getFetchCount(), 1 );
60
61         p = (Person) s.createQuery("from Person").uniqueResult();
62         assertNotNull(p.address); assertNull(p.mailingAddress);
63         s.clear();
64         
65         assertEquals( addressStats.getFetchCount(), 1 );
66         assertEquals( mailingAddressStats.getFetchCount(), 2 );
67
68         p = (Person) s.createQuery("from Entity").uniqueResult();
69         assertNotNull(p.address); assertNull(p.mailingAddress);
70         s.clear();
71         
72         assertEquals( addressStats.getFetchCount(), 2 );
73         assertEquals( mailingAddressStats.getFetchCount(), 3 );
74
75         //note that in here join fetch is used for the nullable
76
//one-to-one, due to a very special case of default
77
p = (Person) s.get(Person.class, "Gavin");
78         assertNotNull(p.address); assertNull(p.mailingAddress);
79         s.clear();
80         
81         assertEquals( addressStats.getFetchCount(), 2 );
82         assertEquals( mailingAddressStats.getFetchCount(), 3 );
83
84         p = (Person) s.get(Entity.class, "Gavin");
85         assertNotNull(p.address); assertNull(p.mailingAddress);
86         s.clear();
87         
88         assertEquals( addressStats.getFetchCount(), 2 );
89         assertEquals( mailingAddressStats.getFetchCount(), 3 );
90         
91         t.commit();
92         s.close();
93         
94         s = openSession();
95         t = s.beginTransaction();
96         Org org = new Org();
97         org.name = "IFA";
98         Address a2 = new Address();
99         a2.entityName = "IFA";
100         a2.zip = "3181";
101         a2.state = "VIC";
102         a2.street = "Orrong Rd";
103         org.addresses.add(a2);
104         s.persist(org);
105         t.commit();
106         s.close();
107         
108         s = openSession();
109         t = s.beginTransaction();
110         org = (Org) s.get(Entity.class, "IFA");
111         s.clear();
112         
113         List JavaDoc list = s.createQuery("from Entity e order by e.name").list();
114         p = (Person) list.get(0);
115         assertNotNull(p.address); assertNull(p.mailingAddress);
116         org = (Org) list.get(1);
117         assertEquals( org.addresses.size(), 1 );
118         s.clear();
119         
120         list = s.createQuery("from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name").list();
121         p = (Person) list.get(0);
122         org = (Org) list.get(1);
123         assertNotNull(p.address); assertNull(p.mailingAddress);
124         assertEquals( org.addresses.size(), 1 );
125         
126         s.delete(p);
127         s.delete(org);
128         
129         t.commit();
130         s.close();
131         
132     }
133     
134     protected String JavaDoc[] getMappings() {
135         return new String JavaDoc[] { "onetoone/singletable/Person.hbm.xml" };
136     }
137
138     protected void configure(Configuration cfg) {
139         cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
140         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
141     }
142     
143     public static Test suite() {
144         return new TestSuite(OneToOneTest.class);
145     }
146
147 }
148
149
Popular Tags