Hibernate JPA: @OneToMany delete old, insert new without flush

后端 未结 2 638
情深已故
情深已故 2020-12-24 08:33

I actually never quite understood this behavior in hibernate. I am using a @OneToMany relationship in a Entity called \'Parent\', which is annotated like this:



        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 08:50

    Hibernate doesn't know about, nor respect, all database constraints (e.g. MySQL unique constraints). It's a known issue they don't plan on addressing anytime soon.

    Hibernate has a defined order for the way operations occur during a flush.

    Entity deletions will always happen after inserts. The only answers I know about are to remove the constraint or add the additional flush.

    EDIT: By the way, the reason for the defined order is that this is the only way to guarantee foreign key constraints (one of the constraints they DO care about) aren't violated, even if the user does something out of order.

提交回复
热议问题