KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > cuk > CompositePropertyRefTest


1 //$Id: CompositePropertyRefTest.java,v 1.3 2005/02/18 02:53:22 oneovthafew Exp $
2
package org.hibernate.test.cuk;
3
4 import java.util.List JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
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 CompositePropertyRefTest extends TestCase {
21     
22     public CompositePropertyRefTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testOneToOnePropertyRef() {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         Person p = new Person();
30         p.setName("Steve");
31         p.setUserId("steve");
32         Address a = new Address();
33         a.setAddress("Texas");
34         a.setCountry("USA");
35         p.setAddress(a);
36         a.setPerson(p);
37         s.save(p);
38         Person p2 = new Person();
39         p2.setName("Max");
40         p2.setUserId("max");
41         s.save(p2);
42         Account act = new Account();
43         act.setType('c');
44         act.setUser(p2);
45         p2.getAccounts().add(act);
46         s.save(act);
47         s.flush();
48         s.clear();
49         
50         p = (Person) s.get( Person.class, p.getId() ); //get address reference by outer join
51
p2 = (Person) s.get( Person.class, p2.getId() ); //get null address reference by outer join
52
assertNull( p2.getAddress() );
53         assertNotNull( p.getAddress() );
54         List JavaDoc l = s.createQuery("from Person").list(); //pull address references for cache
55
assertEquals( l.size(), 2 );
56         assertTrue( l.contains(p) && l.contains(p2) );
57         s.clear();
58         
59         l = s.createQuery("from Person p order by p.name").list(); //get address references by sequential selects
60
assertEquals( l.size(), 2 );
61         assertNull( ( (Person) l.get(0) ).getAddress() );
62         assertNotNull( ( (Person) l.get(1) ).getAddress() );
63         s.clear();
64         
65         l = s.createQuery("from Person p left join fetch p.address a order by a.country").list(); //get em by outer join
66
assertEquals( l.size(), 2 );
67         if ( ( (Person) l.get(0) ).getName().equals("Max") ) {
68             assertNull( ( (Person) l.get(0) ).getAddress() );
69             assertNotNull( ( (Person) l.get(1) ).getAddress() );
70         }
71         else {
72             assertNull( ( (Person) l.get(1) ).getAddress() );
73             assertNotNull( ( (Person) l.get(0) ).getAddress() );
74         }
75         s.clear();
76         
77         l = s.createQuery("from Person p left join p.accounts").list();
78         for ( int i=0; i<2; i++ ) {
79             Object JavaDoc[] row = (Object JavaDoc[]) l.get(i);
80             Person px = (Person) row[0];
81             Set JavaDoc accounts = px.getAccounts();
82             assertFalse( Hibernate.isInitialized(accounts) );
83             assertTrue( px.getAccounts().size()>0 || row[1]==null );
84         }
85         s.clear();
86
87         l = s.createQuery("from Person p left join fetch p.accounts a order by p.name").list();
88         Person p0 = (Person) l.get(0);
89         assertTrue( Hibernate.isInitialized( p0.getAccounts() ) );
90         assertEquals( p0.getAccounts().size(), 1 );
91         assertSame( ( (Account) p0.getAccounts().iterator().next() ).getUser(), p0 );
92         Person p1 = (Person) l.get(1);
93         assertTrue( Hibernate.isInitialized( p1.getAccounts() ) );
94         assertEquals( p1.getAccounts().size(), 0 );
95         s.clear();
96         
97         l = s.createQuery("from Account a join fetch a.user").list();
98         
99         s.clear();
100         
101         l = s.createQuery("from Person p left join fetch p.address").list();
102         
103         t.commit();
104         s.close();
105     }
106
107     
108     protected String JavaDoc[] getMappings() {
109         return new String JavaDoc[] { "cuk/Person.hbm.xml" };
110     }
111
112     public static Test suite() {
113         return new TestSuite(CompositePropertyRefTest.class);
114     }
115
116     protected void configure(Configuration cfg) {
117         cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "1");
118     }
119 }
120
121
Popular Tags