这是怎么回事?
@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;
}
在调用 remove
时,我得到一个 IllegalArgumentException
消息是 Entity must be managed to call remove: ...., try merging the detached并再次尝试删除。
最佳答案
在您的情况下不需要合并,因为 ac 在 em.find 和 em.remove 之间的任何点都没有分离。
通常,当实体被分离时,EntityManager 的方法merge 将实体作为参数并返回托管实例。作为参数给出的实体不会转换为附加。例如,此处对此进行了解释:EntityManager.merge .你必须去:
AppointmentComment toBeRemoved = em.merge(ac);
em.remove(toBeRemoved);
关于java - 实体必须被管理才能调用 remove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338999/