ejb - 无法合并已删除的实体

标签 ejb ejb-3.0 ejb-3.1

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

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

最佳答案

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

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

关于ejb - 无法合并已删除的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553677/

相关文章:

java - 使用 Wicket/JPA 注入(inject)实体管理器时出错

jboss - 如何在 Jboss 7 的 Application Client Container 中 jun 一个 jar 文件?

scheduling - 异步EJB调度

ejb - 作业调度 : EJB 3. 1 计时器还是 quartz ?

java - Glassfish 初始化 Singleton bean 两次

jakarta-ee - 如何将事件分发到(Wildfly)集群中的所有节点?

java - 如何对客户端应用程序和 Web 应用程序使用单个 EJB

java - 排除由 CDI 拦截器调用的特定方法

java - 容器管理事务的 Activity

java - EJB 3 JMS 配置加载异常 || 7 号