简体   繁体   English

使用EntityManager时将分离的实体传递给Pesist

[英]Detached Entity Passed to Pesist when Using EntityManager

I Have two entity's mapped with one to many relationship.I am persist two objects In Database with Id 1,2 我有两个实体以一对多关系进行映射。我在ID为1,2的数据库中持久存在两个对象

with same type 同类型

scenario 1. I am Trying to remove Id '1' with getEntityManager.remove(object1) its show the 场景1.我正在尝试使用getEntityManager.remove(object1)删除getEntityManager.remove(object1) ',其显示

detached entity passed to persist for Child entity..even DB record found. 传递了分离实体以持久保留子实体。甚至找到DB记录。

scenario 2. when I am Trying to remove Id '2' with getEntityManager.remove(object2) its deleting the record 2 . 场景2。当我尝试使用getEntityManager.remove(object2)删除getEntityManager.remove(object2) '时,删除记录2。

this Exception occur only for before enter record with same time. 此异常仅在同一时间进入记录之前发生。

after I am close and re open the page I am remove the ID 1,2 from Db its successfully worked. 关闭并重新打开页面后,我从成功运行的Db中删除了ID 1,2。

com.manam.mobee.persist.entity.MobeeLabelTemplate#null com.manam.mobee.persist.entity.MobeeLabelTemplate#null

Inspect the mobeeLabel.getMobeeLabelTemplates() inside the deleteLabel() and make sure they have the id . 检查mobeeLabel.getMobeeLabelTemplates()内的deleteLabel()并确保它们具有id


Edit: 编辑:

You need the id in the objects to update and delete. 您需要对象中的ID进行更新和删除。 Before deleting mobeeLabel, perform a findById and then remove it. 删除mobeeLabel之前,请执行findById,然后将其删除。 There should be some non-inserted mobeeLabelTemplates (or not updated - ids out of sync with the db). 应该有一些未插入的mobeeLabelTemplates(或未更新-ID与数据库不同步)。 Try something like this: 尝试这样的事情:

MobeeLabel freshMobeeToDelete = (MobeeLabel) getEntityManager().find(MobeeLabel.class, mobeeLabel.getId());
getEntityManager().remove(freshMobeeToDelete);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM