1 package org.hibernate.test.typedonetoone; 3 4 import java.util.List ; 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 17 public class TypedOneToOneTest extends TestCase { 18 19 public TypedOneToOneTest(String 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", "abc123") ); 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", "abc123") ); 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 results = s.createQuery("from Customer cust left join fetch cust.billingAddress where cust.customerId='abc123'").list(); 56 cust = (Customer) results.get(0); 58 assertTrue( 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 68 public void testCreateQueryNull() { 69 Customer cust = new Customer(); 70 cust.setCustomerId("xyz123"); 71 cust.setName("Matt"); 72 73 Session s = openSession(); 74 Transaction t = s.beginTransaction(); 75 s.persist(cust); 76 t.commit(); 77 s.close(); 78 79 s = openSession(); 80 t = s.beginTransaction(); 81 List results = s.createQuery("from Customer cust left join fetch cust.billingAddress where cust.customerId='xyz123'").list(); 82 cust = (Customer) results.get(0); 84 assertNull( cust.getShippingAddress() ); 85 assertNull( cust.getBillingAddress() ); 86 t.commit(); 87 s.close(); 88 89 } 90 91 protected String [] getMappings() { 92 return new String [] { "typedonetoone/Customer.hbm.xml" }; 93 } 94 95 public static Test suite() { 96 return new TestSuite(TypedOneToOneTest.class); 97 } 98 99 } 100 101 | Popular Tags |