1 package org.hibernate.test.annotations.manytomany; 3 4 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashSet ; 8 import java.util.Set ; 9 10 import org.hibernate.Session; 11 import org.hibernate.Transaction; 12 import org.hibernate.test.annotations.TestCase; 13 14 19 public class ManyToManyTest extends TestCase { 20 21 public ManyToManyTest(String x) { 22 super(x); 23 } 24 25 public void testDefault() throws Exception { 26 Session s; 27 Transaction tx; 28 s = openSession(); 29 tx = s.beginTransaction(); 30 Store fnac = new Store(); 31 fnac.setName("Fnac"); 32 KnownClient emmanuel = new KnownClient(); 33 emmanuel.setName("Emmanuel"); 34 emmanuel.setStores( new HashSet <Store>() ); 35 fnac.setCustomers( new HashSet <KnownClient>() ); 36 fnac.getCustomers().add(emmanuel); 37 emmanuel.getStores().add(fnac); 38 fnac.setImplantedIn( new HashSet <City>() ); 39 City paris = new City(); 40 fnac.getImplantedIn().add(paris); 41 paris.setName("Paris"); 42 s.persist(fnac); 43 tx.commit(); 44 s.close(); 45 46 s = openSession(); 47 tx = s.beginTransaction(); 48 Store store; 49 KnownClient knownClient; 50 City city; 51 store = (Store) s.get( Store.class, fnac.getId() ); 52 assertNotNull(store); 53 assertNotNull( store.getCustomers() ); 54 assertEquals( 1, store.getCustomers().size() ); 55 knownClient = (KnownClient) store.getCustomers().iterator().next(); 56 assertEquals( emmanuel.getName(), knownClient.getName() ); 57 assertNotNull( store.getImplantedIn() ); 58 assertEquals( 1, store.getImplantedIn().size() ); 59 city = (City) store.getImplantedIn().iterator().next(); 60 assertEquals( paris.getName(), city.getName() ); 61 tx.commit(); 62 s.close(); 63 64 s = openSession(); 65 tx = s.beginTransaction(); 66 knownClient = (KnownClient) s.get( KnownClient.class, emmanuel.getId() ); 67 assertNotNull(knownClient); 68 assertNotNull( knownClient.getStores() ); 69 assertEquals( 1, knownClient.getStores().size() ); 70 store = (Store) knownClient.getStores().iterator().next(); 71 assertEquals( fnac.getName(), store.getName() ); 72 tx.commit(); 73 s.close(); 74 } 75 76 public void testDefaultCompositePk() throws Exception { 77 Session s; 78 Transaction tx; 79 80 s = openSession(); 81 tx = s.beginTransaction(); 82 CatPk catPk = new CatPk(); 83 catPk.setName("Minou"); 84 catPk.setThoroughbred("Persan"); 85 Cat cat = new Cat(); 86 cat.setId(catPk); 87 cat.setAge(32); 88 Woman woman = new Woman(); 89 WomanPk womanPk = new WomanPk(); 90 womanPk.setFirstName("Emma"); 91 womanPk.setLastName("Peel"); 92 woman.setId(womanPk); 93 woman.setCats( new HashSet <Cat>() ); 94 woman.getCats().add(cat); 95 cat.setHumanContacts( new HashSet <Woman>() ); 96 cat.getHumanContacts().add(woman); 97 s.persist(woman); 98 s.persist(cat); 99 tx.commit(); 100 s.close(); 101 102 s = openSession(); 103 tx = s.beginTransaction(); 104 Cat sameCat = (Cat) s.get( Cat.class, cat.getId() ); 105 assertNotNull(sameCat); 106 assertNotNull( sameCat.getHumanContacts() ); 107 assertEquals( 1, sameCat.getHumanContacts().size() ); 108 Woman sameWoman = sameCat.getHumanContacts().iterator().next(); 109 assertEquals( sameWoman.getId().getLastName(), woman.getId().getLastName() ); 110 tx.commit(); 111 s.close(); 112 113 s = openSession(); 114 tx = s.beginTransaction(); 115 sameWoman = (Woman) s.get( Woman.class, woman.getId() ); 116 assertNotNull(sameWoman); 117 assertNotNull( sameWoman.getCats() ); 118 assertEquals( 1, sameWoman.getCats().size() ); 119 sameCat = sameWoman.getCats().iterator().next(); 120 assertEquals( cat.getAge(), sameCat.getAge() ); 121 tx.commit(); 122 s.close(); 123 } 124 125 public void testMappedBy() throws Exception { 126 Session s; 127 Transaction tx; 128 s = openSession(); 129 tx = s.beginTransaction(); 130 Store fnac = new Store(); 131 fnac.setName("Fnac"); 132 Supplier emi = new Supplier(); 133 emi.setName("Emmanuel"); 134 emi.setSuppStores( new HashSet <Store>() ); 135 fnac.setSuppliers( new HashSet <Supplier>() ); 136 fnac.getSuppliers().add(emi); 137 emi.getSuppStores().add(fnac); 138 s.persist(fnac); 139 tx.commit(); 140 s.close(); 141 142 s = openSession(); 143 tx = s.beginTransaction(); 144 Store store; 145 Supplier supplier; 146 store = (Store) s.get( Store.class, fnac.getId() ); 147 assertNotNull(store); 148 assertNotNull( store.getSuppliers() ); 149 assertEquals( 1, store.getSuppliers().size() ); 150 supplier = (Supplier) store.getSuppliers().iterator().next(); 151 assertEquals( emi.getName(), supplier.getName() ); 152 tx.commit(); 153 s.close(); 154 155 s = openSession(); 156 tx = s.beginTransaction(); 157 supplier = (Supplier) s.get( Supplier.class, emi.getId() ); 158 assertNotNull(supplier); 159 assertNotNull( supplier.getSuppStores() ); 160 assertEquals( 1, supplier.getSuppStores().size() ); 161 store = (Store) supplier.getSuppStores().iterator().next(); 162 assertEquals( fnac.getName(), store.getName() ); 163 tx.commit(); 164 s.close(); 165 } 166 167 public void testBasic() throws Exception { 168 Session s; 169 Transaction tx; 170 s = openSession(); 171 tx = s.beginTransaction(); 172 Employer er = new Employer(); 173 Employee ee = new Employee(); 174 s.persist(ee); 175 Collection erColl = new ArrayList (); 176 Collection eeColl = new ArrayList (); 177 erColl.add(ee); 178 eeColl.add(er); 179 er.setEmployees(erColl); 180 ee.setEmployers(eeColl); 181 tx.commit(); 183 s.close(); 184 185 s = openSession(); 186 tx = s.beginTransaction(); 187 er = (Employer) s.load(Employer.class, er.getId() ); 188 assertNotNull(er); 189 assertNotNull( er.getEmployees() ); 190 assertEquals( 1, er.getEmployees().size() ); 191 Employee eeFromDb = (Employee) er.getEmployees().iterator().next(); 192 assertEquals( ee.getId(), eeFromDb.getId() ); 193 tx.commit(); 194 s.close(); 195 } 196 197 public void testSelf() throws Exception { 198 Session s; 199 Transaction tx; 200 s = openSession(); 201 tx = s.beginTransaction(); 202 Friend f = new Friend(); 203 Friend sndF = new Friend(); 204 f.setName("Starsky"); 205 sndF.setName("Hutch"); 206 Set frnds = new HashSet (); 207 frnds.add(sndF); 208 f.setFriends(frnds); 209 s.persist(f); 211 tx.commit(); 212 s.close(); 213 214 s = openSession(); 215 tx = s.beginTransaction(); 216 f = (Friend) s.load( Friend.class, f.getId() ); 217 assertNotNull(f); 218 assertNotNull( f.getFriends() ); 219 assertEquals( 1, f.getFriends().size() ); 220 Friend fromDb2ndFrnd = (Friend) f.getFriends().iterator().next(); 221 assertEquals(fromDb2ndFrnd.getId(), sndF.getId() ); 222 assertEquals( 0, fromDb2ndFrnd.getFriends().size() ); 223 tx.commit(); 224 s.close(); 225 } 226 227 public void testCompositePk() throws Exception { 228 Session s; 229 Transaction tx; 230 231 ManPk m1pk = new ManPk(); 232 m1pk.setElder(true); 233 m1pk.setFirstName("Lucky"); 234 m1pk.setLastName("Luke"); 235 ManPk m2pk = new ManPk(); 236 m2pk.setElder(false); 237 m2pk.setFirstName("Joe"); 238 m2pk.setLastName("Dalton"); 239 240 Man m1 = new Man(); 241 m1.setId(m1pk); 242 m1.setCarName("Jolly Jumper"); 243 Man m2 = new Man(); 244 m2.setId(m2pk); 245 246 WomanPk w1pk = new WomanPk(); 247 w1pk.setFirstName("Ma"); 248 w1pk.setLastName("Dalton"); 249 WomanPk w2pk = new WomanPk(); 250 w2pk.setFirstName("Carla"); 251 w2pk.setLastName("Bruni"); 252 253 Woman w1 = new Woman(); 254 w1.setId(w1pk); 255 Woman w2 = new Woman(); 256 w2.setId(w2pk); 257 258 Set <Woman> womens = new HashSet <Woman>(); 259 womens.add(w1); 260 m1.setWomens(womens); 261 Set <Woman> womens2 = new HashSet <Woman>(); 262 womens2.add(w1); 263 womens2.add(w2); 264 m2.setWomens(womens2); 265 266 Set <Man> mens = new HashSet <Man>(); 267 mens.add(m1); 268 mens.add(m2); 269 w1.setMens(mens); 270 Set <Man> mens2 = new HashSet <Man>(); 271 mens2.add(m2); 272 w2.setMens(mens2); 273 274 s = openSession(); 275 tx = s.beginTransaction(); 276 s.persist(m1); 277 s.persist(m2); 278 tx.commit(); 279 s.close(); 280 281 s = openSession(); 282 tx = s.beginTransaction(); 283 m1 = (Man) s.load(Man.class, m1pk); 284 assertFalse( m1.getWomens().isEmpty() ); 285 assertEquals( 1, m1.getWomens().size() ); 286 w1 = (Woman) s.load(Woman.class, w1pk); 287 assertFalse( w1.getMens().isEmpty() ); 288 assertEquals( 2, w1.getMens().size() ); 289 290 tx.commit(); 291 s.close(); 292 } 293 294 297 protected Class [] getMappings() { 298 return new Class [] { 299 Friend.class, 300 Employer.class, 301 Employee.class, 302 Man.class, 303 Woman.class, 304 Store.class, 305 KnownClient.class, 306 Supplier.class, 307 City.class, 308 Cat.class 309 }; 310 } 311 312 } | Popular Tags |