繁体   English   中英

jpa实体未找到异常

[英]jpa entity not found exception

我有以下问题:类Foo是带有项目列表的EntityBean。

  public class Foo {
    @OneToMany(cascade = {}, fetch = FetchType.LAZY)
    private List<Item> items = new ArrayList<Item>()

在我的应用程序的某个时刻,我执行以下操作:

  1. 从foo中删除ID为3的项目
  2. 调用updateFoo()

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void updateFoo(Foo foo){
   if(item has been removed from the list of foo){
      //item is from the List of the parameter foo
      entityManager.remove(item);
    }
      entityManager.flush();

     //update remaining items 
     for(Item item: foo.getItems()){
        //set some field values on item
        ...
        entityManager.merge(item);
       }        

    //here occurs the exception
    entityManager.merge(foo);  
  }

一切工作正常,直到执行最后一行。 然后例外

 javax.persistence.EntityNotFoundException: Unable to find Item with id 3.

最好的祝福

埃德蒙

在执行entityManager.remove之前,您应该从集合中手动删除项目。

我必须使该关系成为双向关系,注释掉对flush的调用,最后添加merge,persist和remove作为层叠类型。

暂无
暂无

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

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