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

后端 未结 2 642
情深已故
情深已故 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:57

    For the sake of future readers, one way to resolve this issue is to use deferred constraints. PostgreSQL and Oracle support them, maybe other RDBMS' too. Hibernate will issue all statements within a transaction, and deferral will ensure that constraints are enforced upon transaction commit only. In PostgreSQL, for example:

    ALTER TABLE company
        ADD CONSTRAINT name_unique UNIQUE (name) DEFERRABLE INITIALLY DEFERRED;
    

    It is not ideal, but it is simple and effective.

提交回复
热议问题