如何使用持久性克隆 java 对象

how to clone a jpa object using persistance

大家好,我使用了下面的代码

我有疑问

entityManager.detach( )

这会只删除 id 和持久性吗?

所以我一直在尝试使用此代码来获取结果,但所有数据都丢失了。

                Pdata= projDao.findById(id);
                setupdata=Pdata.getdata(); // all data is present
                entityManager.detach(setupdata);  // all data is gone
                projDao.save(setupdata);

要求

需要克隆 jpa 对象

分离与克隆无关。

来自 API 文档:

void detach(Object entity)

Remove the given entity from the persistence context, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database. Entities which previously referenced the detached entity will continue to reference it.

JPA 不支持克隆。 您不需要为要克隆的实体做任何特殊的事情。只需克隆它,但不包括 ID。