item.setName("openjpa");
item.setPrice(0.0);
em.getTransaction().begin();
em.persist(item);
// Persist a customer for finding later
DMCustomer customer = new DMCustomer();
customer.setFirstName("Open");
customer.setLastName("JPA");
em.persist(customer);
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
DMItem itemDetached = em.find(DMItem.class, item.getId());
em.close();
em = emf.createEntityManager();
DMCustomer customer2 = em.find(DMCustomer.class, customer.getId());
DMCustomerInventory customerInventory = new DMCustomerInventory();
customerInventory.setCustomer(customer2);
customerInventory.setItem(itemDetached);
customerInventory.setQuantity(20);
customer2.getCustomerInventories().add(customerInventory);
em.getTransaction().begin();
em.merge(customer2);
// At this point, itemDetached is still detached.
// The following commit causes a persist on CustomerInventory,
// which leads to a cascade-persist on the detached item.