簡體   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