KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > comppropertyref > ComponentPropertyRefTest


1 //$Id: ComponentPropertyRefTest.java,v 1.1 2005/07/21 01:22:38 oneovthafew Exp $
2
package org.hibernate.test.comppropertyref;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.hibernate.Hibernate;
8 import org.hibernate.Session;
9 import org.hibernate.Transaction;
10 import org.hibernate.cfg.Configuration;
11 import org.hibernate.test.TestCase;
12
13 /**
14  * @author Gavin King
15  */

16 public class ComponentPropertyRefTest extends TestCase {
17     
18     public ComponentPropertyRefTest(String JavaDoc str) {
19         super(str);
20     }
21     
22     public void testComponentPropertyRef() {
23         Person p = new Person();
24         p.setIdentity( new Identity() );
25         Account a = new Account();
26         a.setNumber("123-12345-1236");
27         a.setOwner(p);
28         p.getIdentity().setName("Gavin");
29         p.getIdentity().setSsn("123-12-1234");
30         Session s = openSession();
31         Transaction tx = s.beginTransaction();
32         s.persist(p);
33         s.persist(a);
34         s.flush();
35         s.clear();
36         
37         a = (Account) s.createQuery("from Account a left join fetch a.owner").uniqueResult();
38         assertTrue( Hibernate.isInitialized( a.getOwner() ) );
39         assertNotNull( a.getOwner() );
40         assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
41         s.clear();
42         
43         a = (Account) s.get(Account.class, "123-12345-1236");
44         assertFalse( Hibernate.isInitialized( a.getOwner() ) );
45         assertNotNull( a.getOwner() );
46         assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
47         assertTrue( Hibernate.isInitialized( a.getOwner() ) );
48         
49         s.clear();
50         
51         getSessions().evict(Account.class);
52         getSessions().evict(Person.class);
53         
54         a = (Account) s.get(Account.class, "123-12345-1236");
55         assertTrue( Hibernate.isInitialized( a.getOwner() ) );
56         assertNotNull( a.getOwner() );
57         assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
58         assertTrue( Hibernate.isInitialized( a.getOwner() ) );
59         
60         tx.commit();
61         s.close();
62         
63     }
64
65     protected String JavaDoc[] getMappings() {
66         return new String JavaDoc[] { "comppropertyref/PersonAccount.hbm.xml" };
67     }
68
69     public static Test suite() {
70         return new TestSuite(ComponentPropertyRefTest.class);
71     }
72
73     protected void configure(Configuration cfg) {
74         //cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
75
}
76
77 }
78
79
Popular Tags