具有关系CascadeType.ALL JPA的IllegalStateException

时间:2017-06-30 16:16:23

标签: java jpa one-to-many entitymanager illegalstateexception

我有主子实体关系,主实体是请求,子实体是requestitem

我已按照以下方式定义了它们之间的关系

@OneToMany(mappedBy="iagrequest", cascade={CascadeType.ALL},fetch=FetchType.EAGER)
    private List<IagRequestItem> iagrequestitems;

In my logic I am trying to update the existing entities like this, 



     String ret = "";
     EntityTransaction trans = null;
     IagRequest existingDB = this.findById(request.getAccRequestId());
     List <IagRequestItem> updatedReqItems = new ArrayList<IagRequestItem>();
     List <IagRequestItem> existingReqItems = new ArrayList<IagRequestItem>();
     try {
         updatedReqItems = request.getIagrequestitems();
         existingReqItems = existingDB.getIagrequestitems();

         trans = this.entityManager.getTransaction();
         for(int i=0;i<updatedReqItems.size();i++) {
             IagRequestItem updatedReqItem = updatedReqItems.get(i);
             IagRequestItem existingReqItem = existingReqItems.get(i);
             updatedReqItem.setAccRequestItemId(existingReqItem.getAccRequestItemId());
             updatedReqItem.setIagrequest(existingReqItem.getIagrequest());
             updatedReqItem.setRequestItemConnector(existingReqItem.getRequestItemConnector());
             updatedReqItem.setRequestItemName(existingReqItem.getRequestItemName());
             updatedReqItem.setRequestItemValidFrom(existingReqItem.getRequestItemValidFrom());
             updatedReqItem.setRequestItemValidTo(existingReqItem.getRequestItemValidTo());
             updatedReqItem.setRequestItemType(existingReqItem.getRequestItemType());
             updatedReqItem.setRequestItemVersion(existingReqItem.getRequestItemVersion());
             updatedReqItems.set(i, updatedReqItem);
             existingDB.getIagrequestitems().set(i, updatedReqItem);
             this.iagrequestitemDao.persist(updatedReqItem);
             //this.iagrequestitemDao.persist(updatedReqItem);

         }
        trans.begin();          
        this.entityManager.persist(existingDB);                     
        trans.commit();      

抛出&gt;带有原因

的java.lang.IllegalStateException

&#34;目前没有任何交易活跃&#34; 但是,如果我改变了这样的主孩子关系

然后它不会抛出任何异常,无论如何在DB中没有更新     @OneToMany(mappedBy =&#34; iagrequest&#34;,fetch = FetchType.EAGER)

看起来我错过了一些东西,或者我的基本原理还不清楚 请提供指导 提前致谢

我也将代码更改为以下

 trans = this.entityManager.getTransaction();
     trans.begin(); 
     for(int i=0;i<updatedReqItems.size();i++) {
         IagRequestItem updatedReqItem = updatedReqItems.get(i);
         IagRequestItem existingReqItem = existingReqItems.get(i);
         updatedReqItem.setAccRequestItemId(existingReqItem.getAccRequestItemId());
         updatedReqItem.setIagrequest(existingReqItem.getIagrequest());
         updatedReqItem.setRequestItemConnector(existingReqItem.getRequestItemConnector());
         updatedReqItem.setRequestItemName(existingReqItem.getRequestItemName());
         updatedReqItem.setRequestItemValidFrom(existingReqItem.getRequestItemValidFrom());
         updatedReqItem.setRequestItemValidTo(existingReqItem.getRequestItemValidTo());
         updatedReqItem.setRequestItemType(existingReqItem.getRequestItemType());
         updatedReqItem.setRequestItemVersion(existingReqItem.getRequestItemVersion());
         updatedReqItems.set(i, updatedReqItem);
         existingDB.removeIagrequestitem(existingReqItem);
         existingDB.addIagrequestitem(updatedReqItem);

         this.iagrequestitemDao.persist(updatedReqItem);
         //this.iagrequestitemDao.persist(updatedReqItem);

     }

但我仍然得到同样的错误

1 个答案:

答案 0 :(得分:0)

我能够弄清楚这一点。 当我们有以下映射时 @OneToMany(mappedBy =“iagrequest”,cascade = {CascadeType.ALL},fetch = FetchType.EAGER)

我们必须先从数据库中获取主要订单项,然后循环显示其现有订单项 然后我们通过将新值从传入列表复制到现有行项目并将它们添加到新列表

来创建新列表

之后我们只是坚持主实体,它就像一个魅力

这是

的代码
    updatedReqItems = request.getIagrequestitems();
        existingReqItems = existingDB.getIagrequestitems();
        trans = this.entityManager.getTransaction();
        trans.begin(); 
        for(int i=0;i<length;i++) {
        IagRequestItem currentItem = updatedReqItems.get(i);
        //DB Item
        IagRequestItem dbItem = existingDB.getIagrequestitems().get(i);
        dbItem.setRequestItemAction(currentItem.getRequestItemAction());
        newReqItems.add(dbItem);
    }


    existingDB.setIagrequestitems(newReqItems);
    this.entityManager.persist(existingDB);
    trans.commit();