必须管理实体以调用删除 [英] Entity must be managed to call remove

查看:162
本文介绍了必须管理实体以调用删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了什么?

@Stateless
@LocalBean
public class AppointmentCommentDao {
    public void delete(long appointmentCommentId) {
        AppointmentComment ac = em.find(AppointmentComment.class, appointmentCommentId);
        if (ac != null)
        {
            em.merge(ac);
            em.remove(ac);
        }
    }
    @PersistenceContext
    private EntityManager em;
}

致电删除我得到一个 IllegalArgumentException ,消息为必须管理实体才能调用remove:....,尝试合并分离并尝试删除再次。

On the call to remove I get an IllegalArgumentException with the message being Entity must be managed to call remove: ...., try merging the detached and try the remove again.

推荐答案

在你的情况下不需要合并,因为ac在任何点都没有被释放在 em.find em.remove 之间。

In your case merge is not needed, because ac is not deattached in any point between em.find and em.remove.

通常,当实体被释放时,EntityManager的方法合并将实体作为参数,返回托管实例。作为参数给出的实体不会转换为附加。例如,这里解释了这一点: EntityManager。合并。你必须去:

In general when entity is deattached, EntityManager's method merge takes entity as argument and returns managed instance. Entity given as argument does not transform to be attached. This is explained for example here: EntityManager.merge. You have to go for:

    AppointmentComment toBeRemoved = em.merge(ac);
    em.remove(toBeRemoved);

这篇关于必须管理实体以调用删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆