Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate throws unique constraint violation exception while updating field part of unique key

Below is the use case: I have a unique index defined on 3 columns say A,B,C. Assume the values in them are A1,B1,C1. My java code is adding a new record say A1,B1,C1 but before this record is added, i update the previous value from C1 to C2. While trying to add the new record (after the update), hibernate is throwing an unique constraint violation exception. Any reason as to why it does? All the above statements are executed within the same transaction. My assumption is the insert happens before the update and hence the reason for the exception.

Any thoughts/suggestions ?

like image 480
Cshah Avatar asked Aug 19 '10 17:08

Cshah


1 Answers

Try to use session.flush() after the update.

like image 81
Rafael Avatar answered Nov 15 '22 01:11

Rafael