~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/lazyonetoone/LazyOneToOneTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: LazyOneToOneTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
 
2
package org.hibernate.test.lazyonetoone;
 
3
 
 
4
import java.util.Date;
 
5
 
 
6
import junit.framework.Test;
 
7
 
 
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;
 
16
 
 
17
/**
 
18
 * @author Gavin King
 
19
 */
 
20
public class LazyOneToOneTest extends FunctionalTestCase {
 
21
        
 
22
        public LazyOneToOneTest(String str) {
 
23
                super(str);
 
24
        }
 
25
 
 
26
        public String[] getMappings() {
 
27
                return new String[] { "lazyonetoone/Person.hbm.xml" };
 
28
        }
 
29
 
 
30
        public void configure(Configuration cfg) {
 
31
                cfg.setProperty(Environment.MAX_FETCH_DEPTH, "2");
 
32
                cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
 
33
        }
 
34
 
 
35
        public static Test suite() {
 
36
                return new FunctionalTestClassTestSuite( LazyOneToOneTest.class );
 
37
        }
 
38
 
 
39
        public static boolean isRunnable() {
 
40
                return FieldInterceptionHelper.isInstrumented( new Person() );
 
41
        }
 
42
 
 
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() );
 
52
                s.persist(p);
 
53
                s.persist(p2);
 
54
                t.commit();
 
55
                s.close();
 
56
                
 
57
                s = openSession();
 
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() );
 
66
                t.commit();
 
67
                s.close();
 
68
 
 
69
                s = openSession();
 
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() );
 
78
                s.delete(p2);
 
79
                s.delete(old);
 
80
                s.delete(p);
 
81
                t.commit();
 
82
                s.close();
 
83
        }
 
84
}
 
85