When application server started to commit, EclipseLink executes first the
SQL insert for child which breaks due to a foreign key violation. Adding
an explicit call to flush() method fixed this issue:
parent.setPk("abc");
em.persist(parent);
em.flush();
child.setFk(parent.getPk());
em.persist(child);
Why doesn't this work without flush?
I'm using Oracle AS 10g, EclipseLink 1.1.1 and Oracle 11 with thin drivers.
EclipseLink performs writes to the database based on constraint
dependencies interpreted from the relational mappings of an Entity. This
aleviates the need for users to managed operation ordering.
In your case, from the sample code, I suspect you have not actually mapped
this relationship but are managing the FKs yourself. In this case
EclipseLink does not have a mapping between these two Entities to
determine operation ordering. I recommend that you use a mapping here and
let EclipseLink manage the foreign keys for you.
If a mapping is not an option you can use a DescriptorCustomizer to add a
constraint dependency (ClassDescriptor.addConstraintDependency) between
the two Entity classes.
Otherwise continue to use the flush() api.
--Gordon
I apologize, I forgot to include mappings to the pseudo code (yes, we had
those). I didn't have access to the source code when I sent the question,
but now I have and here is a more detailed example: