Hello,
I have just updated my objectdb version from 2.4.6 to 2.5.5.
I observed a new bug or a feature on an ordinary merge operation.
This is the test case ( work in 2.4.6 version ) :
2 parents object share the same child on a ManyToMany relationship with Cascade.ALL constraint.
public final class MyTestCase {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"objectdb:$objectdb/db/test.tmp;drop");
EntityManager em = null ;
// Merge parent1
em = emf.createEntityManager();
if (! em.getTransaction().isActive()) {
em.getTransaction().begin();
}
MyEntity e1 = new MyEntity("parent1");
e1.addChild(String.valueOf("child1"));
em.merge(e1);
em.getTransaction().commit();
em.clear();
em.close();
// Merge parent2
em = emf.createEntityManager();
if (! em.getTransaction().isActive()) {
em.getTransaction().begin();
}
MyEntity e2 = new MyEntity("parent2");
e2.addChild(String.valueOf("child1"));
em.merge(e2);
em.getTransaction().commit();
em.clear();
em.close();
emf.close();
}
@Entity
public static class MyEntity {
@Id
private String name;
@ManyToMany(cascade = {
CascadeType.ALL
})
private List <MyEntityChild> lst = new ArrayList <MyEntityChild> ();
MyEntity(String name) {
this.name = name;
}
public void addChild(String name) {
lst.add(new MyEntityChild(name));
}
}
@Entity
public static class MyEntityChild {
@Id
private String name;
MyEntityChild(String name) {
this.name = name;
}
}
}
With ObjectDb 2.5.5, i have the following exception :
Caused by: javax.persistence.EntityExistsException: com.objectdb.o.UserException: Attempt to reuse an existing primary key value (MyTestCase$MyEntityChild:'child1') at com.objectdb.o._EntityExistsException.b(_EntityExistsException.java:46) at com.objectdb.o.JPE.g(JPE.java:98) at com.objectdb.o.JPE.g(JPE.java:78) ... 4 more
Is that behaviour normal, is it a regression ?
If that behaviour is considered as "normal", so how i can have in a simple way 2 parents sharing the same children ? ( without retrieving child on Db or using other tricks ).
Thanks,
Xirt
Edit : Using CascadeType.MERGE make the test case working !
I think the CascadeType.PERSIST ( included in CascadeType.ALL ) is the problem.
In my case i never explicit a "persist" operation, but a "merge" one.
It seems that ObjectDb ( on 2.5.5 ) made this "persist" operation, also in merge case.