简体   繁体   English

检索对象并使用Hibernate / Spring Data JPA另存为新对象

[英]Retrieve an object and save as new one with Hibernate/Spring Data JPA

In one of the services we perform some operations: 在其中一项服务中,我们执行一些操作:

  1. Update table A - call method with annotated query, method from repository, which extends JpaRepository. 更新表A-带注释查询的调用方法,来自存储库的方法,扩展了JpaRepository。
  2. Select from table B (repository.findOne(B_PK)) 从表B中选择(repository.findOne(B_PK))
  3. Modify retrieved object from point 2 修改从点2检索到的对象
  4. Save modified object as a new entity (repository.save(B_Object)) 将修改后的对象另存为新实体(repository.save(B_Object))

Unfortunately, we are getting an error: "identifier of an instance of B_Object altered from X to Z". 不幸的是,我们收到一个错误:“ B_Object实例的标识符从X更改为Z”。 How we should do this in correct way?? 我们应该如何正确地做到这一点?

您应该像在新实体一样进行更改和存储之前,分离在第2点中获取的对象。

Erase the unique identifier from the object (if it's auto-generated) 从对象中删除唯一标识符(如果它是自动生成的)

If doesnt have a autogen key you need to remove the constraints for the composite key. 如果没有自动生成键,则需要删除组合键的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM