简体   繁体   中英

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.
  2. Select from table B (repository.findOne(B_PK))
  3. Modify retrieved object from point 2
  4. Save modified object as a new entity (repository.save(B_Object))

Unfortunately, we are getting an error: "identifier of an instance of B_Object altered from X to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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