1 package org.hibernate.test.annotations.onetoone; 3 4 import org.hibernate.Query; 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import org.hibernate.test.annotations.Customer; 8 import org.hibernate.test.annotations.Discount; 9 import org.hibernate.test.annotations.Passport; 10 import org.hibernate.test.annotations.TestCase; 11 import org.hibernate.test.annotations.Ticket; 12 13 16 public class OneToOneTest extends TestCase { 17 18 public OneToOneTest(String x) { 19 super(x); 20 } 21 22 public void testEagerFetching() throws Exception { 23 Session s; 24 Transaction tx; 25 s = openSession(); 26 tx = s.beginTransaction(); 27 Client c = new Client(); 28 c.setName("Emmanuel"); 29 Address a = new Address(); 30 a.setCity("Courbevoie"); 31 c.setAddress(a); 32 s.persist(c); 33 tx.commit(); 34 s.close(); 35 36 s = openSession(); 37 tx = s.beginTransaction(); 38 Query q = s.createQuery("select c from Client c where c.name = :name"); 39 q.setString( "name", c.getName() ); 40 c = (Client) q.uniqueResult(); 41 assertNotNull(c); 43 tx.commit(); 44 s.close(); 45 assertNotNull( c.getAddress() ); 46 48 } 49 50 public void testDefaultOneToOne() throws Exception { 51 Session s; 53 Transaction tx; 54 s = openSession(); 55 tx = s.beginTransaction(); 56 Customer c = new Customer(); 57 c.setName("Hibernatus"); 58 Passport p = new Passport(); 59 p.setNumber("123456789"); 60 s.persist(c); c.setPassport(p); 62 p.setOwner(c); 63 p.setId( c.getId() ); 64 tx.commit(); 65 s.close(); 66 s = openSession(); 67 tx = s.beginTransaction(); 68 c = (Customer) s.get( Customer.class, c.getId() ); 69 assertNotNull(c); 70 p = c.getPassport(); 71 assertNotNull( p ); 72 assertEquals( "123456789", p.getNumber() ); 73 assertNotNull( p.getOwner() ); 74 assertEquals( "Hibernatus", p.getOwner().getName() ); 75 tx.commit(); s.close(); 77 } 78 79 public void testOneToOneWithExplicitFk() throws Exception { 80 Client c = new Client(); 81 Address a = new Address(); 82 a.setCity("Paris"); 83 c.setName("Emmanuel"); 84 c.setAddress(a); 85 86 Session s; 87 Transaction tx; 88 s = openSession(); 89 tx = s.beginTransaction(); 90 s.persist(c); 91 tx.commit(); 92 s.close(); 93 94 s = openSession(); 95 tx = s.beginTransaction(); 96 c = (Client) s.get( Client.class, c.getId() ); 97 assertNotNull(c); 98 assertNotNull( c.getAddress() ); 99 assertEquals( "Paris", c.getAddress().getCity() ); 100 tx.commit(); 101 s.close(); 102 } 103 104 public void testUnidirectionalTrueOneToOne() throws Exception { 105 Body b = new Body(); 106 Heart h = new Heart(); 107 b.setHeart(h); 108 b.setId( new Integer (1) ); 109 h.setId( b.getId() ); Session s; 111 Transaction tx; 112 s = openSession(); 113 tx = s.beginTransaction(); 114 s.persist(h); 115 s.persist(b); 116 tx.commit(); 117 s.close(); 118 119 s = openSession(); 120 tx = s.beginTransaction(); 121 b = (Body) s.get( Body.class, b.getId() ); 122 assertNotNull(b); 123 assertNotNull( b.getHeart() ); 124 assertEquals( h.getId(), b.getHeart().getId() ); 125 tx.commit(); 126 s.close(); 127 128 } 129 130 public void testCompositePk() throws Exception { 131 Session s; 132 Transaction tx; 133 s = openSession(); 134 tx = s.beginTransaction(); 135 ComputerPk cid = new ComputerPk(); 136 cid.setBrand("IBM"); 137 cid.setModel("ThinkPad"); 138 Computer c = new Computer(); 139 c.setId(cid); 140 c.setCpu("2 GHz"); 141 SerialNumberPk sid = new SerialNumberPk(); 142 sid.setBrand( cid.getBrand() ); 143 sid.setModel( cid.getModel() ); 144 SerialNumber sn = new SerialNumber(); 145 sn.setId(sid); 146 sn.setValue("REZREZ23424"); 147 c.setSerial(sn); 148 s.persist(c); 149 tx.commit(); 150 s.close(); 151 152 s = openSession(); 153 tx = s.beginTransaction(); 154 c = (Computer) s.get(Computer.class, cid); 155 assertNotNull(c); 156 assertNotNull( c.getSerial() ); 157 assertEquals( sn.getValue(), c.getSerial().getValue() ); 158 tx.commit(); 159 s.close(); 160 } 161 162 165 protected Class [] getMappings() { 166 return new Class [] { 167 Customer.class, 168 Ticket.class, 169 Discount.class, 170 Passport.class, 171 Client.class, 172 Address.class, 173 Computer.class, 174 SerialNumber.class, 175 Body.class, 176 Heart.class 177 }; 178 } 179 180 } 181 | Popular Tags |