在使用外键关系保存数据时,它会抛出异常
敏感度实体类
@Entity
@Table(name="lab_tb_sensitivity")
public class Sensitivity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name=EntityConstants.SQ_NAME, sequenceName=EntityConstants.SQ_SS)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=EntityConstants.SQ__NAME)
@Column(name="sensitivity_id")
private long sensitivityId;
@Column(name="audit_yn")
private String auditYn;
//bi-directional many-to-one association to SensitivityPattern
@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY, mappedBy="sensitivity")
private Set<SensitivityPattern> SensitivityPattern;
/** Constructor and getters and setters
}
与SensitivityPattern
有一对多的关系SensitivityPattern实体类
@Entity
@Table(name="lab_tb_sensitivity_pattern")
public class SensitivityPattern implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private SensitivityPatternPK id;
private String remarks;
@ManyToOne
@JoinColumn(name="sensitivity_id", nullable = false, insertable = false, updatable = false)
private Sensitivity sensitivity;
/**Constructor and geets setters */
}
主键是复合键(sensitivityId + abId) SensitivityPatternPK实体类 ---------------------------------
@Embeddable
public class SensitivityPatternPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="sensitivity_id")
private long sensitivityId;
@Column(name="ab_id")
private long abId;
/**Constructor and geets setters */
}
保存代码
@Override
public boolean saveSensitivityData(List<SensitivityDto> sensitivity) {
if(sensitivity != null){
for(SensitivityDto dto : sensitivity) {
try {
updateSensitivityPattern(dto);
super.saveOneEntity(dto);
} catch (GenericException e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* To update the sensitivity data.
* @param dto
*/
private void updateSensitivityPattern(SensitivityDto dto) {
if(dto != null && dto.getSensitivityPattern() != null){
for(SensitivityPatternDto sPattern : dto.getSensitivityPattern() ){
SensitivityPatternPKDto sensitivityPatternPKDto = new SensitivityPatternPKDto();
sensitivityPatternPKDto.setSensitivityId(dto.getSensitivityId());
sPattern.setId(sensitivityPatternPKDto);
sPattern.setSensitivity(dto);
}
}
这是将数据保存到数据库中的DAO类
@Transactional(rollbackOn = GenericException.class)
public D saveOneEntity(D dto) throws GenericException {
T result;
try {
result = repository.save(mapper.map(dto, entityClass));
} catch (DataAccessException dae) {
throw new GenericException(dae, dae.getRootCause().getMessage());
} catch (Exception e) {
throw new GenericException(
e,
messageSource.getMessage(CodeEnum.UNKNOWN_EXCEPTION_ON_SAVE_METHOD.getValue(), null, Locale.ENGLISH));
}
return mapper.map(result, dtoClass);
}
保存时使用以下数据保存以下数据
{
"sensitivity": [
{
"auditYn": "Y",
"sensitivityPattern": [
{
"id": {
"abId": 11
},
"remarks": "test"
}
]
}
]
}
抛出异常
“引起:org.postgresql.util.PSQLException:错误:插入或更新 在表“lab_tb_sensitivity_pattern”违反外键约束 “fk_sensitivity_id”细节:Key(sensitivity_id)=(0)不存在 在表“lab_tb_sensitivity”中。
答案 0 :(得分:2)
请修正以下代码,
try {
updateSensitivityPattern(dto);
super.saveOneEntity(dto);
}
catch (GenericException e) {
e.printStackTrace();
}
您需要persist
先Sensitivity
,然后SensitivityPattern
。
目前它失败了,因为没有找到entity
,因此没有生成id
。
相反,
try {
//please verify as per you code
CorrespondingSentivityDTO persistedDTO = super.saveOneEntity(dto);
updateSensitivityPattern(persistedDTO);
}
catch (GenericException e) {
e.printStackTrace();
}