gt:             SparkPlug plug6Read = em.find(SparkPlug.class, plug6.getId());
context: /*******************************************************************************
   * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
   * This program and the accompanying materials are made available under the
   * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
   * which accompanies this distribution.
   * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
   * and the Eclipse Distribution License is available at
   * http://www.eclipse.org/org/documents/edl-v10.php.
   *
   * Contributors:
   *     05/1/2009-2.0 Guy Pelletier/David Minsky
   *       - 249033: JPA 2.0 Orphan removal
   *     08/12/2015-2.6 Mythily Parthasarathy
   *       - 474752: Add test for 1-M in Embeddable
   ******************************************************************************/
  package org.eclipse.persistence.testing.tests.jpa.orphanremoval;
  
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.*;
  import junit.framework.*;
  import org.eclipse.persistence.testing.framework.junit.JUnitTestCase;
  import org.eclipse.persistence.testing.models.jpa.orphanremoval.*;
  
  public class OrphanRemovalJUnitTestCase extends JUnitTestCase {
  
      public OrphanRemovalJUnitTestCase(String name) {
          super(name);
      }
  
      public void compareObjects(Object obj, Object objRead) {
          assertTrue("The object [" + obj + "] when read back did not match the original", getServerSession().compareObjects(obj, objRead));
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite("Orphan removal suite");
          if (! JUnitTestCase.isJPA10()) {
              // Setup:
              suite.addTest(new OrphanRemovalJUnitTestCase("testSetup"));
  
              // JPA testing:
              suite.addTest(new OrphanRemovalJUnitTestCase("test12M"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test12MWithCascade"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test12MWithCascadeFromOR"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test12MFromExistingObject"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test12MFromExistingObjectModification"));
  
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithCascade"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithCascadeFromOR"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121FromExistingObject"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121FromExistingObjectModification"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121IgnoredFromRemovedExistingObject"));
  
              suite.addTest(new OrphanRemovalJUnitTestCase("testEmbeddedWithCascadeFromOR"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121ChangeFromExistingObject"));
  
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithCascadeRemoveFromOR"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithNoCascadePersist"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithNoCascadeMerge1"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithNoCascadeMerge2"));
              suite.addTest(new OrphanRemovalJUnitTestCase("test121WithNoCascadeMerge3"));
              suite.addTest(new OrphanRemovalJUnitTestCase("testORInEmbeddedEntityWithToMany"));
          }
          return suite;
      }
  
      /**
       * Test removal of a private owned object with a referenced embedded object
       */
      public void testEmbeddedWithCascadeFromOR() {
          EntityManager em = createEntityManager();
  
          try {
              // Step 1 - Create the objects
              beginTransaction(em);
  
              Vehicle vehicle = new Vehicle("GT-X");
              Chassis chassis1 = new Chassis(1l);
              vehicle.setChassis(chassis1);
  
              Wheel wheel1 = new Wheel(1l);
              chassis1.addWheel(wheel1);
  
              Tire tire1 = new Tire();
              tire1.setManufacturer("ACME");
              tire1.setType("Radial");
              wheel1.setTire(tire1);
  
              em.persist(vehicle);
  
              Chassis chassis2 = new Chassis(2l);
              vehicle.setChassis(chassis2);
  
              commitTransaction(em);
              closeEntityManager(em);
  
              // Step 2 - Verification
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleRead = em.find(Vehicle.class, vehicle.getId());
              compareObjects(vehicle, vehicleRead);
  
              Chassis chassis2Read = em.find(Chassis.class, chassis2.getId());
              compareObjects(chassis2, chassis2Read);
  
              assertNull("Chassis 1 should not have been inserted", em.find(Chassis.class, chassis1.getId()));
              assertNull("Wheel 1 should not have been inserted", em.find(Wheel.class, wheel1.getId()));
  
              // Step 3 - Cleanup
              em.remove(vehicleRead);
              em.remove(chassis2Read);
              commitTransaction(em);
              closeEntityManager(em);
          }  catch (RuntimeException e) {
              if (isTransactionActive(em)){
                  rollbackTransaction(em);
              }
  
              closeEntityManager(em);
              throw e;
          }
      }
  
      /**
       * The simplest test for orphan removal (1:M)
       */
      public void test12M() {
          EntityManager em = createEntityManager();
  
          try {
              // Step 1 - Create the objects
              beginTransaction(em);
  
              Vehicle vehicle = new Vehicle("GT-X");
              Chassis chassis = new Chassis(1);
              vehicle.setChassis(chassis);
  
              Wheel wheel1 = new Wheel(1l);
              Wheel wheel2 = new Wheel(2l);
              Wheel wheel3 = new Wheel(3l);
              Wheel wheel4 = new Wheel(4l);
              Wheel wheel5 = new Wheel(5l);
  
              wheel1.setTire(new Tire());
              wheel2.setTire(new Tire());
              wheel3.setTire(new Tire());
              wheel4.setTire(new Tire());
              wheel5.setTire(new Tire());
              
              chassis.addWheel(wheel1);
              chassis.addWheel(wheel2);
              chassis.addWheel(wheel3);
              chassis.addWheel(wheel4);
              chassis.addWheel(wheel5);
  
              em.persist(vehicle);
  
              chassis.removeWheel(wheel5); // never a good idea to have a 5th wheel
  
              commitTransaction(em);
              closeEntityManager(em);
  
              // Step 2 - verification
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleRead = em.find(Vehicle.class, vehicle.getId());
              compareObjects(vehicle, vehicleRead);
              Chassis chassisRead = em.find(Chassis.class, chassis.getId());
              compareObjects(chassis, chassisRead);
              Wheel wheel1Read = em.find(Wheel.class, wheel1.getId());
              compareObjects(wheel1, wheel1Read);
              Wheel wheel2Read = em.find(Wheel.class, wheel2.getId());
              compareObjects(wheel2, wheel2Read);
              Wheel wheel3Read = em.find(Wheel.class, wheel3.getId());
              compareObjects(wheel3, wheel3Read);
              Wheel wheel4Read = em.find(Wheel.class, wheel4.getId());
              compareObjects(wheel4, wheel4Read);
  
              assertNull("Wheel5 should not be inserted", em.find(Wheel.class, wheel5.getId()));
  
              // Step 3 - Cleanup
              em.remove(vehicleRead);
              em.remove(chassisRead);
              em.remove(wheel1Read);
              em.remove(wheel2Read);
              em.remove(wheel3Read);
              em.remove(wheel4Read);
              commitTransaction(em);
              closeEntityManager(em);
          } catch (RuntimeException e) {
              if (isTransactionActive(em)){
                  rollbackTransaction(em);
              }
  
              closeEntityManager(em);
              throw e;
          }
      }
  
      public void test12MFromExistingObject() {
          EntityManager em = createEntityManager();
  
          try {
              // Step 1 - Create the objects.
              beginTransaction(em);
  
              Vehicle vehicle = new Vehicle("GT-X");
              Chassis chassis1 = new Chassis(1l);
              vehicle.setChassis(chassis1);
  
              Wheel wheel1 = new Wheel(1l);
              wheel1.setTire(new Tire());
              chassis1.addWheel(wheel1);
              Wheel wheel2 = new Wheel(2l);
              wheel2.setTire(new Tire());
              chassis1.addWheel(wheel2);
  
              em.persist(vehicle);
  
              commitTransaction(em);
              closeEntityManager(em);
  
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleRead = em.find(Vehicle.class, vehicle.getId());
              compareObjects(vehicle, vehicleRead);
  
              Chassis chassisRead = vehicleRead.getChassis();
              chassisRead.setSerialNumber(74923402);
              chassisRead.removeWheel(wheel1);
              chassisRead.removeWheel(wheel2);
              chassisRead.setWheels(null);
  
              commitTransaction(em);
              closeEntityManager(em);
  
              // Step 2 - Verification
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleReadAgain = em.find(Vehicle.class, vehicle.getId());
              // This check fails.
              //compareObjects(vehicleRead, vehicleReadAgain);
  
              assertNull("Wheel1 should have been removed", em.find(Wheel.class, wheel1.getId()));
              assertNull("Wheel2 should have been removed", em.find(Wheel.class, wheel2.getId()));
  
              // Step 3 - Cleanup
              em.remove(vehicleReadAgain);
              commitTransaction(em);
              closeEntityManager(em);
          } catch (RuntimeException e) {
              if (isTransactionActive(em)){
                  rollbackTransaction(em);
              }
  
              closeEntityManager(em);
              throw e;
          }
      }
  
      public void test12MFromExistingObjectModification() {
          EntityManager em = createEntityManager();
  
          try {
              // Step 1 - Create the objects.
              beginTransaction(em);
  
              Vehicle vehicle = new Vehicle("GT-X");
              Chassis chassis1 = new Chassis(1l);
              vehicle.setChassis(chassis1);
  
              Wheel wheel1 = new Wheel(1l);
              Wheel wheel2 = new Wheel(2l);
              wheel1.setTire(new Tire());
              wheel2.setTire(new Tire());
              chassis1.addWheel(wheel1);
              chassis1.addWheel(wheel2);
  
              em.persist(vehicle);
  
              commitTransaction(em);
              closeEntityManager(em);
  
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleRead = em.find(Vehicle.class, vehicle.getId());
              compareObjects(vehicle, vehicleRead);
  
              Chassis chassis = vehicleRead.getChassis();
              for (Wheel wheel : chassis.getWheels()) {
                  wheel.setSerialNumber(wheel.getSerialNumber() + 10);
              }
  
              commitTransaction(em);
              closeEntityManager(em);
  
              // Step 2 - Verification
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Vehicle vehicleReadAgain = em.find(Vehicle.class, vehicle.getId());
              compareObjects(vehicleRead, vehicleReadAgain);
  
              // Step 3 - Cleanup
              em.remove(vehicleReadAgain);
              commitTransaction(em);
              closeEntityManager(em);
          } catch (RuntimeException e) {
              if (isTransactionActive(em)){
                  rollbackTransaction(em);
              }
  
              closeEntityManager(em);
              throw e;
          }
      }
  
      /**
       * Test cascading with removing 1:M orphan removal objects
       */
      public void test12MWithCascade() {
          EntityManager em = createEntityManager();
  
          try {
              // Step 1 - Create the objects.
              beginTransaction(em);
  
              Vehicle vehicle = new Vehicle("GT-X");
              Engine engine = new Engine(123456789l);
              SparkPlug plug1 = new SparkPlug(1);
              SparkPlug plug2 = new SparkPlug(2);
              SparkPlug plug3 = new SparkPlug(3);
              SparkPlug plug4 = new SparkPlug(4);
              SparkPlug plug5 = new SparkPlug(5);
              SparkPlug plug6 = new SparkPlug(6);
  
              vehicle.setEngine(engine);
  
              engine.addSparkPlug(plug1);
              engine.addSparkPlug(plug2);
              engine.addSparkPlug(plug3);
              engine.addSparkPlug(plug4);
              engine.addSparkPlug(plug5);
              engine.addSparkPlug(plug6);
  
              em.persist(engine);
              em.persist(vehicle);
  
              engine.removeSparkPlug(plug3);
              engine.removeSparkPlug(plug5);
  
              commitTransaction(em);
              closeEntityManager(em);
  
              // Step 2 - Verification
              clearCache();
              em = createEntityManager();
              beginTransaction(em);
  
              Engine engineRead = em.find(Engine.class, engine.getId());
              compareObjects(engine, engineRead);
              SparkPlug plug1Read = em.find(SparkPlug.class, plug1.getId());
              compareObjects(plug1, plug1Read);
              SparkPlug plug2Read = em.find(SparkPlug.class, plug2.getId());
              compareObjects(plug2, plug2Read);
              SparkPlug plug4Read = em.find(SparkPlug.class, plug4.getId());
              compareObjects(plug4, plug4Read);
  