Cloning JPA entity

前端 未结 8 1612
闹比i
闹比i 2020-11-29 01:55

I have a JPA entity already persisted in the database.
I would like to have a copy of it (with a different id), with some fields modified.

What is the easiest wa

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 02:29

    Use EntityManager.detach. It makes the bean no longer linked to the EntityManager. Then set the Id to the new Id (or null if automatic), change the fields that you need and persist.

提交回复
热议问题