1 package org.hibernate.test.annotations.onetomany; 3 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.HashSet ; 7 import java.util.List ; 8 import java.util.SortedSet ; 9 import java.util.TreeSet ; 10 11 import org.hibernate.Hibernate; 12 import org.hibernate.HibernateException; 13 import org.hibernate.Session; 14 import org.hibernate.Transaction; 15 import org.hibernate.test.annotations.Customer; 16 import org.hibernate.test.annotations.Discount; 17 import org.hibernate.test.annotations.Passport; 18 import org.hibernate.test.annotations.TestCase; 19 import org.hibernate.test.annotations.Ticket; 20 import org.hibernate.test.annotations.TicketComparator; 21 22 26 public class OneToManyTest extends TestCase { 27 28 public OneToManyTest(String x) { 29 super(x); 30 } 31 32 public void testListWithBagSemantic() throws Exception { 33 Session s; 34 Transaction tx; 35 s = openSession(); 36 tx = s.beginTransaction(); 37 City paris = new City(); 38 paris.setName("Paris"); 39 s.persist(paris); 40 Street rochechoir = new Street(); 41 rochechoir.setStreetName("Rochechoir"); 42 rochechoir.setCity(paris); 43 Street chmpsElysees = new Street(); 44 chmpsElysees.setStreetName("Champs Elysees"); 45 chmpsElysees.setCity(paris); 46 Street grandeArmee = new Street(); 47 grandeArmee.setStreetName("Grande Armée"); 48 grandeArmee.setCity(paris); 49 s.persist(rochechoir); 50 s.persist(chmpsElysees); 51 s.persist(grandeArmee); 52 paris.addMainStreet(chmpsElysees); 53 paris.addMainStreet(grandeArmee); 54 tx.commit(); 55 s.clear(); 56 tx = s.beginTransaction(); 57 paris = (City) s.get( City.class, paris.getId() ); 58 assertEquals( 3, paris.getStreets().size() ); 59 assertEquals( chmpsElysees.getStreetName(), paris.getStreets().get(0).getStreetName() ); 60 List <Street> mainStreets = paris.getMainStreets(); 61 assertEquals( 2, mainStreets.size() ); 62 Integer previousId = new Integer (-1); 63 for (Street street : mainStreets) { 64 assertTrue(previousId < street.getId() ); 65 previousId = street.getId(); 66 } 67 tx.commit(); 68 s.close(); 69 70 } 71 72 public void testUnidirectionalDefault() throws Exception { 73 Session s; 74 Transaction tx; 75 Trainer trainer = new Trainer(); 76 trainer.setName("First trainer"); 77 Tiger regularTiger = new Tiger(); 78 regularTiger.setName("Regular Tiger"); 79 Tiger whiteTiger = new Tiger(); 80 whiteTiger.setName("White Tiger"); 81 trainer.setTrainedTigers( new HashSet <Tiger>() ); 82 s = openSession(); 83 tx = s.beginTransaction(); 84 s.persist(trainer); 85 s.persist(regularTiger); 86 s.persist(whiteTiger); 87 trainer.getTrainedTigers().add(regularTiger); 88 trainer.getTrainedTigers().add(whiteTiger); 89 90 tx.commit(); 91 s.close(); 92 93 s = openSession(); 94 tx = s.beginTransaction(); 95 trainer = (Trainer) s.get( Trainer.class, trainer.getId() ); 96 assertNotNull(trainer); 97 assertNotNull( trainer.getTrainedTigers() ); 98 assertEquals( 2, trainer.getTrainedTigers().size() ); 99 tx.rollback(); 100 s.close(); 101 102 s = openSession(); 103 tx = s.beginTransaction(); 104 trainer = new Trainer(); 105 trainer.setName("new trainer"); 106 trainer.setTrainedTigers( new HashSet <Tiger>() ); 107 trainer.getTrainedTigers().add(whiteTiger); 108 try { 109 s.persist(trainer); 110 tx.commit(); 111 fail("A one to many should not allow several trainer per Tiger"); 112 } catch (HibernateException ce) { 113 tx.rollback(); 114 } 116 s.close(); 117 } 118 119 public void testUnidirectionalExplicit() throws Exception { 120 Session s; 121 Transaction tx; 122 Trainer trainer = new Trainer(); 123 trainer.setName("First trainer"); 124 Monkey regularMonkey = new Monkey(); 125 regularMonkey.setName("Regular Monkey"); 126 Monkey miniMonkey = new Monkey(); 127 miniMonkey.setName("Mini Monkey"); 128 trainer.setTrainedMonkeys( new HashSet <Monkey>() ); 129 s = openSession(); 130 tx = s.beginTransaction(); 131 s.persist(trainer); 132 s.persist(regularMonkey); 133 s.persist(miniMonkey); 134 trainer.getTrainedMonkeys().add(regularMonkey); 135 trainer.getTrainedMonkeys().add(miniMonkey); 136 tx.commit(); 137 s.close(); 138 139 s = openSession(); 140 tx = s.beginTransaction(); 141 trainer = (Trainer) s.get( Trainer.class, trainer.getId() ); 142 assertNotNull(trainer); 143 assertNotNull( trainer.getTrainedMonkeys() ); 144 assertEquals( 2, trainer.getTrainedMonkeys().size() ); 145 tx.rollback(); 146 s.close(); 147 } 148 149 public void testFetching() throws Exception { 150 Session s; 151 Transaction tx; 152 s = openSession(); 153 tx = s.beginTransaction(); 154 Troop t = new Troop(); 155 t.setName("Final cut"); 156 Soldier vandamme = new Soldier(); 157 vandamme.setName("JC Vandamme"); 158 t.addSoldier(vandamme); 159 Soldier rambo = new Soldier(); 160 rambo.setName("Rambo"); 161 t.addSoldier(rambo); 162 s.persist(t); 163 tx.commit(); 164 s.close(); 165 166 s = openSession(); 167 tx = s.beginTransaction(); 168 t = (Troop) s.get( Troop.class, t.getId() ); 169 assertNotNull( t.getSoldiers() ); 170 assertFalse( Hibernate.isInitialized( t.getSoldiers() ) ); 171 assertEquals( 2, t.getSoldiers().size() ); 172 assertEquals( rambo.getName(), t.getSoldiers().iterator().next().getName() ); 173 tx.commit(); 174 s.close(); 175 176 s = openSession(); 177 tx = s.beginTransaction(); 178 t = (Troop) s.createQuery( "from " + Troop.class.getName() + " as t where t.id = :id") 179 .setParameter( "id", t.getId() ).uniqueResult(); 180 assertFalse( Hibernate.isInitialized( t.getSoldiers() ) ); 181 tx.commit(); 182 s.close(); 183 184 s = openSession(); 185 tx = s.beginTransaction(); 186 rambo = (Soldier) s.get( Soldier.class, rambo.getId() ); 187 assertTrue( Hibernate.isInitialized( rambo.getTroop() ) ); 188 tx.commit(); 189 s.close(); 190 191 s = openSession(); 192 tx = s.beginTransaction(); 193 rambo = (Soldier) s.createQuery("from " + Soldier.class.getName() + " as s where s.id = :rid") 194 .setParameter("rid", rambo.getId() ).uniqueResult(); 195 assertTrue( "fetching strategy used when we do query", Hibernate.isInitialized( rambo.getTroop() ) ); 196 tx.commit(); 197 s.close(); 198 } 199 200 public void testCascadeDeleteOrphan() throws Exception { 201 Session s; 202 Transaction tx; 203 s = openSession(); 204 tx = s.beginTransaction(); 205 Troop disney = new Troop(); 206 disney.setName("Disney"); 207 Soldier mickey = new Soldier(); 208 mickey.setName("Mickey"); 209 disney.addSoldier(mickey); 210 s.persist(disney); 211 tx.commit(); 212 s.close(); 213 s = openSession(); 214 tx = s.beginTransaction(); 215 Troop troop = (Troop) s.get( Troop.class, disney.getId() ); 216 Soldier soldier = (Soldier) troop.getSoldiers().iterator().next(); 217 troop.getSoldiers().remove(soldier); 218 tx.commit(); 219 s.close(); 220 s = openSession(); 221 tx = s.beginTransaction(); 222 soldier = (Soldier) s.get( Soldier.class, mickey.getId() ); 223 assertNull( "delete-orphan should work", soldier ); 224 troop = (Troop) s.get( Troop.class, disney.getId() ); 225 s.delete( troop ); 226 tx.commit(); 227 s.close(); 228 } 229 230 public void testCascadeDelete() throws Exception { 231 Session s; 232 Transaction tx; 233 s = openSession(); 234 tx = s.beginTransaction(); 235 Troop disney = new Troop(); 236 disney.setName("Disney"); 237 Soldier mickey = new Soldier(); 238 mickey.setName("Mickey"); 239 disney.addSoldier(mickey); 240 s.persist(disney); 241 tx.commit(); 242 s.close(); 243 s = openSession(); 244 tx = s.beginTransaction(); 245 Troop troop = (Troop) s.get( Troop.class, disney.getId() ); 246 s.delete(troop); 247 tx.commit(); 248 s.close(); 249 s = openSession(); 250 tx = s.beginTransaction(); 251 Soldier soldier = (Soldier) s.get( Soldier.class, mickey.getId() ); 252 assertNull( "delete-orphan should work", soldier ); 253 tx.commit(); 254 s.close(); 255 } 256 257 public void testSimpleOneToManySet() throws Exception { 258 Session s; 259 Transaction tx; 260 s = openSession(); 261 tx = s.beginTransaction(); 262 Ticket t = new Ticket(); 263 t.setNumber("33A"); 264 Ticket t2 = new Ticket(); 265 t2.setNumber("234ER"); 266 Customer c = new Customer(); 267 s.persist(c); 268 SortedSet <Ticket> tickets = new TreeSet <Ticket>( new TicketComparator() ); 270 tickets.add(t); 271 tickets.add(t2); 272 c.setTickets(tickets); 273 274 tx.commit(); 275 s.close(); 276 277 s = openSession(); 278 tx = s.beginTransaction(); 279 c = (Customer) s.load( Customer.class, c.getId() ); 280 assertNotNull(c); 281 assertTrue( Hibernate.isInitialized( c.getTickets() ) ); 282 assertNotNull( c.getTickets() ); 283 tickets = c.getTickets(); 284 assertTrue(tickets.size() > 0); 285 assertEquals( t2.getNumber(), c.getTickets().first().getNumber() ); 286 tx.commit(); 287 s.close(); 288 } 289 290 public void testSimpleOneToManyCollection() throws Exception { 291 Session s; 292 Transaction tx; 293 s = openSession(); 294 tx = s.beginTransaction(); 295 Discount d = new Discount(); 296 d.setDiscount(10); 297 Customer c = new Customer(); 298 List discounts = new ArrayList (); 299 discounts.add(d); 300 d.setOwner(c); 301 c.setDiscountTickets(discounts); 302 s.persist(c); 303 tx.commit(); 304 s.close(); 305 306 s = openSession(); 307 tx = s.beginTransaction(); 308 c = (Customer) s.load( Customer.class, c.getId() ); 309 assertNotNull(c); 310 assertFalse( Hibernate.isInitialized( c.getDiscountTickets() ) ); 311 assertNotNull( c.getDiscountTickets() ); 312 Collection collecDiscount = c.getDiscountTickets(); 313 assertTrue(collecDiscount.size() > 0); 314 tx.commit(); 315 s.close(); 316 } 317 318 public void testJoinColumns() throws Exception { 319 Parent parent = new Parent(); 320 ParentPk pk = new ParentPk(); 321 pk.firstName = "Bruce"; 322 pk.lastName = "Willis"; 323 pk.isMale = true; 324 parent.id = pk; 325 parent.age = 40; 326 Child child = new Child(); 327 Child child2 = new Child(); 328 parent.addChild(child); 329 parent.addChild(child2); 330 Session s; 331 Transaction tx; 332 s = openSession(); 333 tx = s.beginTransaction(); 334 s.persist(parent); 335 tx.commit(); 336 s.close(); 337 338 assertNotNull(child.id); 339 assertNotNull(child2.id); 340 assertNotSame(child.id, child2.id); 341 342 s = openSession(); 343 tx = s.beginTransaction(); 344 parent = (Parent) s.get(Parent.class, pk); 345 assertNotNull(parent.children); 346 Hibernate.initialize(parent.children); 347 assertEquals( 2, parent.children.size() ); 348 tx.commit(); 349 s.close(); 350 } 351 352 355 protected Class [] getMappings() { 356 return new Class [] { 357 Troop.class, 358 Soldier.class, 359 Customer.class, 360 Ticket.class, 361 Discount.class, 362 Passport.class, 363 Parent.class, 364 Child.class, 365 Trainer.class, 366 Tiger.class, 367 Monkey.class, 368 City.class, 369 Street.class 370 }; 371 } 372 373 } 374 | Popular Tags |