Toy toy2 = new Toy(2);
toy2.setToyName("Toy Plane");
// Create toybox
ToyBox toybox = new ToyBox(1);
toybox.setOwnerName("Evan");
// Establish relationship
toy1.setToybox(toybox);
toy2.setToybox(toybox);
toybox.getToyList().add(toy1);
toybox.getToyList().add(toy2);
// Perform the merge
em.getTransaction().begin();
ToyBox mergedToyBox = em.merge(toybox);
assertNotNull("Assert em.merge() didn't return null", mergedToyBox);
em.getTransaction().commit();
// Verify the merge
ToyBox toyboxFind = em.find(ToyBox.class, 1);
Toy toy1Find = em.find(Toy.class, 1);
Toy toy2Find = em.find(Toy.class, 2);
assertNotNull("Assert em.find() for ToyBox(id=1) did not return null.", toyboxFind);
assertNotNull("Assert em.find() for Toy(id=1) did not return null.", toy1Find);
assertNotNull("Assert em.find() for Toy(id=2) did not return null.", toy2Find);
assertTrue("Assert em.find() returns the ToyBox returned by em.merge()", mergedToyBox == toyboxFind);
assertTrue("Assert tahat ToyBox(id=1).toyList is size 2", toyboxFind.getToyList().size() == 2);
assertTrue("Assert that ToyBox(id=1).toyList contains the managed Toy(id=1).", toyboxFind.getToyList()
.contains(toy1Find));
assertTrue("Assert that ToyBox(id=1).toyList contains the managed Toy(id=2).", toyboxFind.getToyList()
.contains(toy2Find));
assertTrue("Assert that Toy(id=1) references the managed ToyBox(id=1).",
toy1Find.getToybox() == toyboxFind);
assertTrue("Assert that Toy(id=2) references the managed ToyBox(id=1).",
toy2Find.getToybox() == toyboxFind);