本文介绍了在分离的实体上调用jpapersist不会插入重复的行.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经意识到,我们项目中的某些类使用户成为em.persist(Book);更新现有的图书记录.

We have realized that some classes in our project make user ofem.persist(Book); to update existing book records.

首先找回书

em.find(id);

然后通过用户输入修改字段,然后将其保留

then fields are modified through user input and then persisted

em.persist(book);

尽管到目前为止一切正常,但仍然使我感到好奇,因为我希望JPA在调用em.persist时插入一个具有不同id的新Book.分离对象引用时,我找不到有关persist方法的行为的任何资源.通过.

Although this worked fine without any problems till now it is making me curious since I expect JPA to insert a new Book with a different id when em.persist is called. I could not find any resource about persist method's behaviour when detached object ref. is passed.

预先感谢

public void A() throws Exception{ // bean method

         try {

       oEntity=oServis.findById(kesintiLogEntity.getOrtaklikId()); //Transactional spring service method returns an existing record
         oEntity.setOrtakSureAy(ortakEntity.getOrtakSureAy()-1);//modify detached object
         oServis.saveOrtak(oEntity); //em.persist(oEntity);
} 
catch (Exception e){
}
}

推荐答案

它很好用,因为您的find方法会捕获一个具有id字段的现有项目,如果您想要新对象,只需创建一个新对象并从找到的对象中复制值然后保留新创建的对象

it works fine since your find method grabs an existing item with id field, if you want new object just create a new object and copy values from the object you find and then persist the newly created object

请看这里是描述实体状态的漂亮图表 http://www.objectdb.com/java/jpa/persistence/managed

Please look here is a nice diagram describing entity stateshttp://www.objectdb.com/java/jpa/persistence/managed

这篇关于在分离的实体上调用jpapersist不会插入重复的行.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:28