KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > typedmanytoone > TypedManyToOneTest


1 //$Id: TypedManyToOneTest.java,v 1.2 2005/07/14 23:56:53 oneovthafew Exp $
2
package org.hibernate.test.typedmanytoone;
3
4 import java.util.List JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * @author Gavin King
16  */

17 public class TypedManyToOneTest extends TestCase {
18     
19     public TypedManyToOneTest(String JavaDoc str) {
20         super(str);
21     }
22     
23     public void testCreateQuery() {
24         Customer cust = new Customer();
25         cust.setCustomerId("abc123");
26         cust.setName("Matt");
27         
28         Address ship = new Address();
29         ship.setStreet("peachtree rd");
30         ship.setState("GA");
31         ship.setCity("ATL");
32         ship.setZip("30326");
33         ship.setAddressId( new AddressId("SHIPPING", "xyz123") );
34         ship.setCustomer(cust);
35         
36         Address bill = new Address();
37         bill.setStreet("peachtree rd");
38         bill.setState("GA");
39         bill.setCity("ATL");
40         bill.setZip("30326");
41         bill.setAddressId( new AddressId("BILLING", "xyz123") );
42         bill.setCustomer(cust);
43         
44         cust.setBillingAddress(bill);
45         cust.setShippingAddress(ship);
46         
47         Session s = openSession();
48         Transaction t = s.beginTransaction();
49         s.persist(cust);
50         t.commit();
51         s.close();
52         
53         s = openSession();
54         t = s.beginTransaction();
55         List JavaDoc results = s.createQuery("from Customer cust left join fetch cust.billingAddress where cust.customerId='abc123'").list();
56         //List results = s.createQuery("from Customer cust left join fetch cust.billingAddress left join fetch cust.shippingAddress").list();
57
cust = (Customer) results.get(0);
58         assertFalse( Hibernate.isInitialized( cust.getShippingAddress() ) );
59         assertTrue( Hibernate.isInitialized( cust.getBillingAddress() ) );
60         assertEquals( "30326", cust.getBillingAddress().getZip() );
61         assertEquals( "30326", cust.getShippingAddress().getZip() );
62         assertEquals( "BILLING", cust.getBillingAddress().getAddressId().getType() );
63         assertEquals( "SHIPPING", cust.getShippingAddress().getAddressId().getType() );
64         t.commit();
65         s.close();
66         
67         s = openSession();
68         t = s.beginTransaction();
69         s.saveOrUpdate(cust);
70         ship = cust.getShippingAddress();
71         cust.setShippingAddress(null);
72         s.delete("ShippingAddress", ship);
73         t.commit();
74         s.close();
75     }
76     
77     public void testCreateQueryNull() {
78         Customer cust = new Customer();
79         cust.setCustomerId("xyz123");
80         cust.setName("Matt");
81         
82         Session s = openSession();
83         Transaction t = s.beginTransaction();
84         s.persist(cust);
85         t.commit();
86         s.close();
87         
88         s = openSession();
89         t = s.beginTransaction();
90         List JavaDoc results = s.createQuery("from Customer cust left join fetch cust.billingAddress where cust.customerId='xyz123'").list();
91         //List results = s.createQuery("from Customer cust left join fetch cust.billingAddress left join fetch cust.shippingAddress").list();
92
cust = (Customer) results.get(0);
93         assertNull( cust.getShippingAddress() );
94         assertNull( cust.getBillingAddress() );
95         t.commit();
96         s.close();
97         
98     }
99     
100     protected String JavaDoc[] getMappings() {
101         return new String JavaDoc[] { "typedmanytoone/Customer.hbm.xml" };
102     }
103
104     public static Test suite() {
105         return new TestSuite(TypedManyToOneTest.class);
106     }
107
108 }
109
110
Popular Tags