1
//$Id: LazyOneToOneTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
2
package org.hibernate.test.lazyonetoone;
6
import junit.framework.Test;
8
import org.hibernate.Hibernate;
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.intercept.FieldInterceptionHelper;
14
import org.hibernate.junit.functional.FunctionalTestCase;
15
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
20
public class LazyOneToOneTest extends FunctionalTestCase {
22
public LazyOneToOneTest(String str) {
26
public String[] getMappings() {
27
return new String[] { "lazyonetoone/Person.hbm.xml" };
30
public void configure(Configuration cfg) {
31
cfg.setProperty(Environment.MAX_FETCH_DEPTH, "2");
32
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
35
public static Test suite() {
36
return new FunctionalTestClassTestSuite( LazyOneToOneTest.class );
39
public static boolean isRunnable() {
40
return FieldInterceptionHelper.isInstrumented( new Person() );
43
public void testLazy() throws Exception {
44
Session s = openSession();
45
Transaction t = s.beginTransaction();
46
Person p = new Person("Gavin");
47
Person p2 = new Person("Emmanuel");
48
Employee e = new Employee(p);
49
new Employment(e, "JBoss");
50
Employment old = new Employment(e, "IFA");
51
old.setEndDate( new Date() );
58
t = s.beginTransaction();
59
p = (Person) s.createQuery("from Person where name='Gavin'").uniqueResult();
60
//assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
61
assertSame( p.getEmployee().getPerson(), p );
62
assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
63
assertEquals( p.getEmployee().getEmployments().size(), 1 );
64
p2 = (Person) s.createQuery("from Person where name='Emmanuel'").uniqueResult();
65
assertNull( p2.getEmployee() );
70
t = s.beginTransaction();
71
p = (Person) s.get(Person.class, "Gavin");
72
//assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
73
assertSame( p.getEmployee().getPerson(), p );
74
assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
75
assertEquals( p.getEmployee().getEmployments().size(), 1 );
76
p2 = (Person) s.get(Person.class, "Emmanuel");
77
assertNull( p2.getEmployee() );