KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > lazyonetoone > LazyOneToOneTest


1 //$Id: LazyOneToOneTest.java,v 1.7 2005/07/10 16:51:17 oneovthafew Exp $
2
package org.hibernate.test.lazyonetoone;
3
4 import java.util.Date JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import net.sf.cglib.transform.impl.InterceptFieldEnabled;
9
10 import org.hibernate.Hibernate;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13 import org.hibernate.cfg.Configuration;
14 import org.hibernate.cfg.Environment;
15 import org.hibernate.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class LazyOneToOneTest extends TestCase {
21     
22     public LazyOneToOneTest(String JavaDoc str) {
23         super(str);
24     }
25
26     public void testLazy() throws Exception JavaDoc {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         Person p = new Person("Gavin");
30         Employee e = new Employee(p);
31         new Employment(e, "JBoss");
32         Employment old = new Employment(e, "IFA");
33         old.setEndDate( new Date JavaDoc() );
34         s.persist(p);
35         t.commit();
36         s.close();
37         
38         s = openSession();
39         t = s.beginTransaction();
40         p = (Person) s.createQuery("from Person").uniqueResult();
41         assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
42         assertSame( p.getEmployee().getPerson(), p );
43         assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
44         assertEquals( p.getEmployee().getEmployments().size(), 1 );
45         t.commit();
46         s.close();
47
48         s = openSession();
49         t = s.beginTransaction();
50         p = (Person) s.get(Person.class, "Gavin");
51         assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
52         assertSame( p.getEmployee().getPerson(), p );
53         assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
54         assertEquals( p.getEmployee().getEmployments().size(), 1 );
55         s.delete(old);
56         s.delete(p);
57         t.commit();
58         s.close();
59     }
60     
61     protected void configure(Configuration cfg) {
62         cfg.setProperty(Environment.MAX_FETCH_DEPTH, "2");
63         cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
64     }
65     protected String JavaDoc[] getMappings() {
66         return new String JavaDoc[] { "lazyonetoone/Person.hbm.xml" };
67     }
68
69     public static Test suite() {
70         return new TestSuite(LazyOneToOneTest.class);
71     }
72
73     public static boolean isRunnable() {
74         return new Person() instanceof InterceptFieldEnabled;
75     }
76 }
77
78
Popular Tags