我有一个问题,需要执行完整的删除,然后再插入。
尝试了一种方法,可以建议还有其他更好的方法吗?
{
"incidentTime": 1491207083634,
"estCode": 152,
"incidentParamTrans": [
{
"paramValueList": [
11,
12,
14
]
}
]
}
这是主要的实体类。
@Entity
@Table(name="IR_TB_INCIDENT_HDR")
public class IncidentHdr implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR", sequenceName="IR_SEQ_INCIDENT_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR")
@Column(name="INCIDENT_ID")
private long incidentId;
@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY, mappedBy="incidentHdr")
private Set<IncidentParamTran> incidentParamTrans;
public IncidentHdr() {
}
}
这是具有一对多映射的实体类。在DTO中(从请求json)获取此paramValueList并将其作为3(paramId)记录插入到IncidentParamTrans表中。
/**
* The persistent class.
*
*/
@Entity
@Table(name="IR_TB_INCIDENT_PARAM_TRAN")
public class IncidentParamTran implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="IR_TB_INCIDENT_PARAM_TRAN_GENERATOR", sequenceName="IR_SEQ_INCIDENT_PARAM_RUN_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_PARAM_TRAN_GENERATOR")
@Column(name="PARAM_RUN_ID")
private long paramRunId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="INCIDENT_ID")
private IncidentHdr incidentHdr;
@Column(name="PARAM_ID")
private BigDecimal paramId;
public IncidentParamTran() {
}
}
如果输入是这样的。
{
"incidentId": 4700,
"incidentTime": 1491207083634,
"estCode": 152,
"incidentParamTrans": [
{
"paramValueList": [
10,
14,
]
}
]
}
必须删除IncidentParamTrans表中的所有数据,事件ID为4700。其中事件ID不是主键。
之后,我必须将所有新记录(2条记录)插入IncidentParamTrans。
执行完整的删除并进行新的插入,这又将引发刷新异常。
我尝试过的代码
服务层代码段
@Override
@Transactional(rollbackFor=IncidentReportingException.class)
public IncidentHdrDto saveIncidentReport(IncidentHdrDto incidentHdrDto)
if(incidentHdrDto.getIncidentParamTrans()!= null ){
Set<IncidentParamTranDto> incidentParamTranDtos = new HashSet<IncidentParamTranDto>();
// dto.getSensitivityPattern().remove
IncidentHdr inc = mapper.map(incidentHdrDto, IncidentHdr.class);
incidentParamTransRepo.deleteByIncidentHdr(inc);
for(IncidentParamTranDto item:incidentHdrDto.getIncidentParamTrans()){
if(item != null){
for(BigDecimal paramItem: item.getParamValueList()){
IncidentParamTranDto val = new IncidentParamTranDto();
val.setParamId(paramItem);
val.setIncidentHdr(incidentHdrDto);
incidentParamTranDtos.add(val);
}
}
}
incidentHdrDto.setIncidentParamTrans(incidentParamTranDtos);
}
result = saveIncidentHdr(incidentHdrDto);
}
将dto映射到实体并调用repo save方法。
@Override
public IncidentHdrDto saveIncidentHdr(IncidentHdrDto incidentHdrDto)
throws IncidentReportingException {
return mapper.map(iReportingRepo.save(mapper.map(incidentHdrDto, IncidentHdr.class)),IncidentHdrDto.class);
}
Repo class used to delete
public interface IncidentParamTransRepo extends JpaRepository<IncidentParamTran, Long> {
Long deleteByIncidentHdr(IncidentHdr inc);
}
例外情况:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: om.gov.moh.irs.model.entity.incident.IncidentHdr
最佳答案
我相信它会失败,因为IncidentHdr.incidentParamTrans
集合保持无效状态,因为事件参数的实际删除是由incidentParamTransRepo.deleteByIncidentHdr(inc);
存储库调用处理的,但另一方面,对CRUD操作的所有权已设置为IncidentHdr.incidentParamTrans
通过级联行为配置。
为避免混淆,我建议尝试以下方法:
首先将'IncidentHdr.incidentParamTrans'转换为DTO,以备后用
清除IncidentHdr.incidentParamTrans
集合:inc .getIncidentParamTrans().clear()
冲洗保存IncidentHdr
:incidentHdrRepository.**saveAndFlush**(inc)
。这将触发您通过手动存储库调用执行的删除查询。重要的是要执行刷新操作,以便在再次填充集合之前在此点执行实际的删除查询。
再次填写'IncidentHdr.incidentParamTrans'并保存/保存
这种方法的优点是Hibernate可以跟踪对IncidentHdr.incidentParamTrans
集合所做的所有更改,并确保正确处理了状态。
这种方法的缺点之一是它将整个IncidentHdr.incidentParamTrans
集合加载到内存中,但是,根据您的配置,我认为这应该不是问题,因为您已经这样做了(通过委托IncidentHdr.incidentParamTrans
的CRUD管理)到IncidentHdr
实体)。
希望这可以帮助。