In my code I remove and persist objects in the same transaction:
for (final GZAFixInterval actStat : actualStatus) {
...
em.remove(actStat);
...
}
for (final Interval newInterval : newIntervals) {
final GZAFixInterval newGZAfi = new GZAFixInterval();
... setters etc
em.persist(newGZAfi);
}
em.flush();
Sometimes I get a PSQLException: ERROR: conflicting key value violates exclusion constraint "constraint_name". This is a special exclusion constraint.
The situation when constraint error occurs is as follows:
- em.remove(objectA)
- em.persist(objectB)
objectA and objectB must be mututally excluded from existing in the table (this is the constraint). Maybe objectA is not really removed from the DB before inserting objectB.
The question is: if this is the case, how to enforce JPA to really remove objectA before insering objectB? Sould a flush resolve it?
Currently I'm playing with deferred constraints, but any idea could be welcome.