Hibernate ObjectDeleteDexException:已删除的实体传递给persist
我有两个实体,如下所示: 设备: 设备组件管理器。类:Hibernate ObjectDeleteDexException:已删除的实体传递给persist,hibernate,jpa,Hibernate,Jpa,我有两个实体,如下所示: 设备: 设备组件管理器。类: DeviceLinePersistenceManager dlpm=新的DeviceLinePersistenceManager(); 试一试{ dpm=新设备持久性管理器(); Set=newhashset(); 用于(LineStatus LineStatus:列表){ 设备deviceRetrieval=dpm.findDeviceByIp(线路状态 .getDeviceIp()); 如果(deviceRetrieval!=null
DeviceLinePersistenceManager dlpm=新的DeviceLinePersistenceManager();
试一试{
dpm=新设备持久性管理器();
Set=newhashset();
用于(LineStatus LineStatus:列表){
设备deviceRetrieval=dpm.findDeviceByIp(线路状态
.getDeviceIp());
如果(deviceRetrieval!=null){
Set set1=deviceRetrieval.getDeviceLines();
如果(!set1.isEmpty()){
迭代器迭代器=set1.Iterator();
while(iterator.hasNext()){
DeviceLine line=iterator.next();
iterator.remove();
DeviceLineToDeleted=dlpm.find(line.getUid());
dlpm.purge(lineToDeleted.getUid());
}
}
dpm.update(deviceRetrieval);
}
}
当运行上述代码时,iam将获取ObjectDeletedException:deleted实体传递给persist。
我无法删除deviceline条目。调用
dlpm.purge(..)
时会引发异常,请帮助我。尝试dlpm.purge(lineToDeleted);
。当前您正在传递Uid,而不是实体本身。清除方法是什么样子的?@BOZHO,清除方法如下所示…public void purge(D entityToPurge)抛出EntityPurgationException{if(entityToPurge==null){throw new EntityPurgationException(“无法清除实体。找不到实体。”);}否则{EntityTransaction jta=getEntityManager().getTransaction();jta.begin();getEntityManager().remove(entityToPurge);jta.commit();}
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "device")
private Set<DeviceLine> deviceLines;
@ManyToOne
@JoinColumn(name = "device_uid")
private Device device;
DeviceLinePersistenceManager dlpm = new DeviceLinePersistenceManager();
try {
dpm = new DevicePersistenceManager();
Set<DeviceLine> set = new HashSet<DeviceLine>();
for (LineStatus lineStatus : list) {
Device deviceRetrieval = dpm.findDeviceByIp(lineStatus
.getDeviceIp());
if (deviceRetrieval != null) {
Set<DeviceLine> set1 = deviceRetrieval.getDeviceLines();
if (!set1.isEmpty()) {
Iterator<DeviceLine> iterator = set1.iterator();
while (iterator.hasNext()) {
DeviceLine line = iterator.next();
iterator.remove();
DeviceLine lineToDeleted = dlpm.find(line.getUid());
dlpm.purge(lineToDeleted.getUid());
}
}
dpm.update(deviceRetrieval);
}
}