just noticed something interesting here. If I run the following piece of code, by calling the flush only at the end of the list, JPA appears to be inserting the objects in random order into the database. The reason I can tell is because I have the database to create the identity column. And the object sequence id doesn't correspond to the order of the identity column.
public void persistList(List<Object> objectList) {
for (Object object : objectList) {
em.persist(object);
}
em.flush()
}
However, if I run the following code it works. All I did is added the em.flush()
at immediately after each em.persist()
. Has anyone run into this issue before?
public void persistList(List<Object> objectList) {
for (Object object : objectList) {
em.persist(object);
em.flush();
}
}
for example, objectList has object1,objec2,object3. So theoretically, the sequence inserted to database should be object1,then object2, then object3. However, sometimes it doesn't.