【问题标题】:Cannot merge an entity that has been removed无法合并已移除的实体
【发布时间】:2012-07-19 03:52:40
【问题描述】:

我在开发工作中遇到了一些非常令人费解的事情。

removePreviousFoodMenuItems(oldRefList);
shFood.setNewFoodMenuItems(newRefList);
em.merge(shFood);
em.flush(); //Error occurs 

如果我在合并之前调用 removePreviousFoodMenuItems,我将在运行时收到“无法合并已删除的实体”异常。但是,这不应该发生,因为我已将 shFood 设置为引用一组新的食物菜单项 (newRefList)。那么为什么merge 还在尝试合并已经被移除的oldRefList 元素呢?如果我将 removePreviousFoodMenuItems 放在 flush 语句之后,则不会出现此问题。

shFood.setNewFoodMenuItems(newRefList);
em.merge(shFood);
em.flush(); //Error does not occur
removePreviousFoodMenuItems(oldRefList);

下面是 removePreviousFoodMenuItems 的代码

public void removePreviousFoodMenuItems(ArrayList<FoodMenuItem> oldRefList){

        for (Object f : oldRefList) {

            FoodMenuItem foodMenuItem = (FoodMenuItem) f;
            foodMenuItem.setStakeholderFood(null);
            foodMenuItem.setPhotoEntity(null);

            em.remove(foodMenuItem);
            //em.flush();
        }//end for

    }//end removePreviousFoodMenuItems

非常感谢您对此提出建议!

更新:newRefList 是如何创建的:

StakeholderFood stakeholder = em.find(StakeholderFood.class, stakeholderID);
ArrayList<FoodMenuItem> newRefList = new ArrayList<FoodMenuItem>();

for (Object o : menuItem) {
            FoodMenuItem fmi = (FoodMenuItem) o;
            FoodMenuItem newFmi = new FoodMenuItem();
            String previousName = fmi.getItemName();

            newFmi.setItemName(previousName);
            newFmi.setItemPrice(fmi.getItemPrice());
            newFmi.setPhotoEntity(fmi.getPhotoEntity());

            //Upload the photos for each item attached to menuItem
            Photo photo = fmi.getPhotoEntity();

            if(photo!=null){
                photo.setFoodmenuItem(newFmi); //set new relationship, break off with old
                em.merge(photo); //This will merge newFmi as well Fix this tomorrow
                em.flush(); //update the links immediately
            }

            if (photo != null && fmi.getContainsImage() == Boolean.FALSE) {
                uploadFoodMenuItemImages(photo);                    
                newFmi.setPhotoEntity(photo);
                newFmi.setContainsImage(Boolean.TRUE);
                newFmi.setRenderedImage(Boolean.FALSE);
                newFmi.setRenderedImageAltText(Boolean.FALSE);
            }//end photo
            else {
                newFmi.setRenderedImageAltText(Boolean.TRUE);
            }

            newFmi.setStakeholderFood(stakeholder);
            newRefList.add(newFmi);

        }//end for

【问题讨论】:

    标签: ejb ejb-3.0 ejb-3.1


    【解决方案1】:

    您在oldRefList 和newRefList 中都有一个或多个相同的FoodMenuItem 实例。将删除应用于oldRefList 中的所有项目,然后会导致newRefList 中的一些实体被删除。

    结果是shFood 拥有这样一个列表,其中至少删除了一个FoodMenuItem。如果在删除之前执行flush,那么在flush发生的那一刻,就没有这样的问题,因为shFood没有引用删除的实例。

    【讨论】:

    • 嗯,但这不应该发生。因为我为 oldRefList 中的每个实例创建了一个新实例。我已经更新了我的代码以展示我如何创建我的 newRefList。期待您的回音!
    • 嗨,米科!谢谢!我终于明白,罪魁祸首是 photoEntity。旧的和新的 foodmenuitems(newFmi 和 fmi)都指向同一个 photoEntity。因为 FoodMenuItem 具有带有 photoEntity 的 ALL 级联类型,所以删除旧的 foodmenuitem (fmi) 也将删除 photoEntity。由于 fmi 和 newfmi 都引用同一个 photoEntity,这将产生与您刚才描述的相同的问题。
    猜你喜欢
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 2019-04-29
    • 2014-05-09
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多