Hello,
sometimes our test suite runs into an unexpected behavior.
The test suite persists an entity A, afterwards not persisted entities B are added to entity A.
At the end a commit is executed and the commit persists the entities B automatically by cascading. But sometimes the cascading doesn't work correctly, so that entities B are not persisted.
Unfortunately we cannot reproduce this bug in the small example. The bug occurs only in a big context.
Do you know something about this bug?
// persist configuration: <cascade-persist always="auto" on-persist="true" on-commit="true" />
public class CascadePersistDoesNotWork {
static int idCounter = 1;
public static void main(String[] args) {
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("objectdb:./db.tmp");
EntityManager em = emf.createEntityManager();
em.setFlushMode(FlushModeType.AUTO);
em.getTransaction().begin();
em.createQuery("DELETE from Object").executeUpdate();
A a = new A();
a.strValue = "A";
a.blist = new HashSet<>();
em.persist(a);
for (int i = 0; i < 1; i++) {
B b = new B();
b.strValue = "B";
a.blist.add(b);
}
em.getTransaction().commit();
em.close();
emf = Persistence.createEntityManagerFactory("objectdb:./db.tmp");
em = emf.createEntityManager();
em.setFlushMode(FlushModeType.AUTO);
em.getTransaction().begin();
Query query = em.createQuery("select b from B b");
assertEquals(1, query.getResultList().size());
em.getTransaction().commit();
em.close();
emf.close();
System.out.println("All done.");
}
@Access (AccessType.FIELD)
@Entity
public static class A {
@Id
int id;
@Basic
String strValue;
@OneToMany (cascade = {CascadeType.ALL }, orphanRemoval = true)
Set<B> blist;
public A() {
id = idCounter++;
}
}
@Access (AccessType.FIELD)
@Entity
public static class B {
@Id
int id;
@Basic
String strValue;
public B() {
id = idCounter++;
}
}
}