Hibernate - clearing a collection with all-delete-orphan and then adding to it causes ConstraintViolationException

后端 未结 4 2046
独厮守ぢ
独厮守ぢ 2021-02-19 14:19

I have these entities

class Foo{
    Set bars;
}

class Bar{
    Foo parent;
    String localIdentifier;
}

With this mapping (sorry,

4条回答
  •  萌比男神i
    2021-02-19 14:28

    I guess there is no alternative to flushing

    From here:

    Hibernate is violating a unique constraint!

    Hibernate isn't quite as clever with unique constraints as it is with foreign keys. Sometimes you might need to give a little hint.

    A unique constraint violation could occur if two objects are both being updated, one is "releasing" a value and the other is "obtaining" the same value. A workaround is to flush() the session manually after updating the first object and before updating the second.

    (This kind of problem occurs rarely in practice.)

提交回复
热议问题