1 package org.hibernate.test.onetooneformula; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 import org.hibernate.FetchMode; 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.criterion.Property; 14 import org.hibernate.test.TestCase; 15 16 19 public class OneToOneFormulaTest extends TestCase { 20 21 public OneToOneFormulaTest(String str) { 22 super(str); 23 } 24 25 public void testOneToOneFormula() { 26 Person p = new Person(); 27 p.setName("Gavin King"); 28 Address a = new Address(); 29 a.setPerson(p); 30 a.setType("HOME"); 31 a.setZip("3181"); 32 a.setState("VIC"); 33 a.setStreet("Karbarook Ave"); 34 p.setAddress(a); 35 36 Session s = openSession(); 37 Transaction t = s.beginTransaction(); 38 s.persist(p); 39 t.commit(); 40 s.close(); 41 42 s = openSession(); 43 t = s.beginTransaction(); 44 p = (Person) s.createQuery("from Person").uniqueResult(); 45 46 assertNotNull( p.getAddress() ); 47 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 48 assertNull( p.getMailingAddress() ); 49 50 s.clear(); 51 52 p = (Person) s.createQuery("from Person p left join fetch p.mailingAddress left join fetch p.address").uniqueResult(); 53 54 assertNotNull( p.getAddress() ); 55 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 56 assertNull( p.getMailingAddress() ); 57 58 s.clear(); 59 60 p = (Person) s.createQuery("from Person p left join fetch p.address").uniqueResult(); 61 62 assertNotNull( p.getAddress() ); 63 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 64 assertNull( p.getMailingAddress() ); 65 66 s.clear(); 67 68 p = (Person) s.createCriteria(Person.class) 69 .createCriteria("address") 70 .add( Property.forName("zip").eq("3181") ) 71 .uniqueResult(); 72 assertNotNull(p); 73 74 s.clear(); 75 76 p = (Person) s.createCriteria(Person.class) 77 .setFetchMode("address", FetchMode.JOIN) 78 .uniqueResult(); 79 80 assertNotNull( p.getAddress() ); 81 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 82 assertNull( p.getMailingAddress() ); 83 84 s.clear(); 85 86 p = (Person) s.createCriteria(Person.class) 87 .setFetchMode("mailingAddress", FetchMode.JOIN) 88 .uniqueResult(); 89 90 assertNotNull( p.getAddress() ); 91 assertTrue( Hibernate.isInitialized( p.getAddress() ) ); 92 assertNull( p.getMailingAddress() ); 93 94 s.delete(p); 95 96 t.commit(); 97 s.close(); 98 99 } 100 101 public void testOneToOneEmbeddedCompositeKey() { 102 Person p = new Person(); 103 p.setName("Gavin King"); 104 Address a = new Address(); 105 a.setPerson(p); 106 a.setType("HOME"); 107 a.setZip("3181"); 108 a.setState("VIC"); 109 a.setStreet("Karbarook Ave"); 110 p.setAddress(a); 111 112 Session s = openSession(); 113 Transaction t = s.beginTransaction(); 114 s.persist(p); 115 t.commit(); 116 s.close(); 117 118 s = openSession(); 119 t = s.beginTransaction(); 120 121 a = new Address(); 122 a.setType("HOME"); 123 a.setPerson(p); 124 a = (Address) s.load(Address.class, a); 125 assertFalse( Hibernate.isInitialized(a) ); 126 a.getPerson(); 127 a.getType(); 128 assertFalse( Hibernate.isInitialized(a) ); 129 assertEquals(a.getZip(), "3181"); 130 131 s.clear(); 132 133 a = new Address(); 134 a.setType("HOME"); 135 a.setPerson(p); 136 Address a2 = (Address) s.get(Address.class, a); 137 assertTrue( Hibernate.isInitialized(a) ); 138 assertSame(a2, a); 139 assertSame(a2.getPerson(), p); assertEquals(a.getZip(), "3181"); 141 142 s.delete(a2); 143 s.delete( s.get( Person.class, p.getName() ) ); 145 t.commit(); 146 s.close(); 147 148 } 149 150 protected String [] getMappings() { 151 return new String [] { "onetooneformula/Person.hbm.xml" }; 152 } 153 154 protected void configure(Configuration cfg) { 155 cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false"); 156 cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); 157 cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "2"); 158 } 159 160 public static Test suite() { 161 return new TestSuite(OneToOneFormulaTest.class); 162 } 163 164 } 165 | Popular Tags |