Dear all,
I have a problem with adding items (entities) into other entity list. When the classes are not enhanced, added items are doubled. Here is an example:
@Entity public class Customer implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @OneToMany(mappedBy = "customer",fetch= FetchType.EAGER, cascade= CascadeType.ALL,orphanRemoval=true) List itemList; ... getters and setters } @Entity public class Item implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @ManyToOne private Customer customer; ... getters and setters } public class JavaApplication { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/Test.odb"); EntityManager em = emf.createEntityManager(); em.find(Customer.class, Customer.class); em.find(Item.class, Item.class); // delete all customers and items List l = em.createQuery("select c from Customer c").getResultList(); em.getTransaction().begin(); for(Object o : l){ em.remove(o); } em.getTransaction().commit(); l = em.createQuery("select i from Item i").getResultList(); em.getTransaction().begin(); for(Object o : l){ em.remove(o); } em.getTransaction().commit(); em.getTransaction().begin(); Customer c = new Customer(); em.persist(c); em.getTransaction().commit(); em.refresh(c); em.getTransaction().begin(); Item i = new Item(); i.setCustomer(c); c.getItemList().add(i); em.merge(c); em.getTransaction().commit(); em.refresh(c); System.out.println("Item count = "+c.getItemList().size()); em.close(); emf.close(); } }
The result is "Item count = 2". Is this another bug?
Michael