Hi. In the example created for this question (please see: https://github.com/koen-serneels/hibernate-uc-violation) is an entity with a single property ‘value’ and unique constraint. Initially there is an existing instance with it’s property ‘value’ equal to ‘val1’. When we now do following within the same transaction:
- change the value on the existing entity from “val1” to “val2”
- add a new entity with “val1”
We get a ConstraintViolationException because Hibernate is executing the insert before the update. Two questions about this behavior
- Is this actually expected knowing that Hibernate knows about the unique constraint and that it would make more sense to perform the insert after the update?
- Is there a more elegant way to deal with this than performing manual flushes as illustrated by the test case?