Merge an entity, change its id, merge again, cause “mapped to a primary key column in the database. Updates are not allowed” error

前端 未结 4 1212
一整个雨季
一整个雨季 2021-02-14 12:22

I have a JPA program where EclipseLink is the Persistence provider. When I merge an user entity, change its ID, and try to merge the same user instance again, an error is thrown

4条回答
  •  青春惊慌失措
    2021-02-14 12:33

    The reason is that the Id may be inserted/defined - as you do in your second example, but not changed/updated - as you try in your first example. The JPA provider tries to reflect the change in the database and fails.

    JPA 2 spec §2.4 says

    The application must not change the value of the primary key. The behavior is undefined if this occurs.

提交回复
热议问题