[英]Entity must be managed to call remove
What's going on here? 这里发生了什么?
@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;
}
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.
在remove
调用时,我得到一个IllegalArgumentException
,消息是Entity must be managed to call remove: ...., try merging the detached and try the remove again.
In your case merge is not needed, because ac is not deattached in any point between em.find and em.remove . 在您的情况下,不需要合并,因为在em.find和em.remove之间的任何点都不会释放ac。
In general when entity is deattached, EntityManager's method merge takes entity as argument and returns managed instance . 通常,当实体被释放时,EntityManager的方法merge将实体作为参数并返回托管实例 。 Entity given as argument does not transform to be attached. 作为参数给出的实体不会转换为附加。 This is explained for example here: EntityManager.merge . 例如,这里解释了这一点: EntityManager.merge 。 You have to go for: 你必须去:
AppointmentComment toBeRemoved = em.merge(ac);
em.remove(toBeRemoved);
Try this: 试试这个:
entity = getEntityManager().getReference(AppointmentComment.class, entity.getId());
getEntityManager().remove(entity);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.