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.