java 当我尝试删除实体时,必须管理实体以调用 remove

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29775627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:55:12  来源:igfitidea点击:

Entity must be managed to call remove when I try to delete an entity

javajpajpql

提问by Ivan Vilanculo

I have this method to delete the entity selected in the list. but when called generates this error and I can not see why.

我有这个方法来删除列表中选择的实体。但是当被调用时会产生这个错误,我不明白为什么。

java.lang.IllegalArgumentException: Entity must be managed to call remove: HP Envy 15, try merging the detached and try the remove again.

java.lang.IllegalArgumentException: Entity must be managed to call remove: HP Envy 15,尝试合并分离的并再次尝试删除。

public void delete(Stock stock){
        EntityManager em = ConnectionFactory.createEntityManager();
        em.getTransaction().begin();
        em.detach(stock);
        em.remove(stock);
        em.getTransaction().commit();        
        em.close();
    }

I've read other related posts

我读过其他相关的帖子

Entity must be managed to call remove

必须管理实体才能调用 remove

IllegalArgumentException: Entity must be managed to call remove

IllegalArgumentException:必须管理实体才能调用 remove

回答by Robby Cornelissen

You can't remove the entity if it is not attached. If the entity is still attached, you can remove it as-is. If it is no longer attached, you can re-attach it using merge:

如果未附加实体,则无法删除该实体。如果实体仍处于连接状态,您可以按原样将其删除。如果它不再附加,您可以使用merge以下命令重新附加它:

if (!em.contains(stock)) {
    stock = em.merge(stock);
}

em.remove(stock);

回答by Alexey Malev

You detach an entity from a session, and then delete it. That won't work.

您从会话中分离实体,然后将其删除。那行不通。

Try removing em.detach(stock);and pass some entity to the method which is guaranteed to be attached to the session, i.e. fetch something from DB and then delete it at once. If that works, you are using your method in a wrong way, most likely with detached or just-created entities.

尝试删除em.detach(stock);某些实体并将其传递给保证附加到会话的方法,即从数据库中获取某些内容,然后立即将其删除。如果可行,则说明您以错误的方式使用您的方法,最有可能使用分离的或刚刚创建的实体。

回答by griFlo

remove the

除掉

em.detach(stock);

detach removes your entity from the entityManager

分离从 entityManager 中删除您的实体

回答by Steph

Why do you detach the object ? An IllegalArgumentException is thrown by detach if the argument is not an entity object. If the argument stock is managed by entity manager, delete the detach line, else merge the entity.

为什么要分离对象?如果参数不是实体对象,则 detach 会抛出 IllegalArgumentException。如果参数库存由实体管理器管理,则删除分离线,否则合并实体。

Try this:

试试这个:

public void delete(Stock stock){
        EntityManager em = ConnectionFactory.createEntityManager();
        em.getTransaction().begin();
        Stock mStock2 = em.merge(stock);
        em.remove(mStock2);
        em.getTransaction().commit();        
        em.close();
    }

回答by Ivan Vilanculo

Very thanks guys You helped me to heal my head ache Here is the code after correcting the error

非常感谢你们帮助我治愈了我的头疼这是纠正错误后的代码

EntityManager em = ConnectionFactory.createEntityManager();
em.getTransaction().begin();
if (!em.contains(stock)) {
    current = em.merge(stock);
}
em.remove(current);
em.getTransaction().commit();
em.close();