删除一个 child 记录时的孤儿删除,级联="all-delete-orphan" 异常
Orphan Removal on removing one child record, cascade="all-delete-orphan" exception
//灵敏度Table
public class Sensitivity implements Serializable {
private static final long serialVersionUID = 1L;
private long sensitivityId;
public Set<SensitivityPattern> sensitivityPattern = new HashSet<SensitivityPattern>(0);
public Sensitivity() {
}
@Id
@SequenceGenerator(name=EntityConstants.SQ_SENSITIVITY_NAME, schema=EntityConstants.CDCIS_LAB_SCHEMA , sequenceName=EntityConstants.SQ_SENSITIVITY, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=EntityConstants.SQ_SENSITIVITY_NAME)
@Column(name="sensitivity_id", unique=true, nullable=false, precision=10, scale=0)
public long getSensitivityId() {
return this.sensitivityId;
}
public void setSensitivityId(long sensitivityId) {
this.sensitivityId = sensitivityId;
}
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy="sensitivity")
public Set<SensitivityPattern> getSensitivityPattern() {
return sensitivityPattern;
}
public void setSensitivityPattern(Set<SensitivityPattern> sensitivityPattern) {
this.sensitivityPattern = sensitivityPattern;
}
}
//灵敏度模式
public class SensitivityPattern extends AuditableEntity implements Serializable {
private static final long serialVersionUID = 1L;
private long sensitivityPtId;
private Sensitivity sensitivity;
public SensitivityPattern() {}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "sensitivity_id")
public Sensitivity getSensitivity() {
return sensitivity;
}
public void setSensitivity(Sensitivity sensitivity) {
this.sensitivity = sensitivity;
}
@Id
@SequenceGenerator(name = EntityConstants.SQ_SENSITIVITY_PATTERN_NAME, schema = EntityConstants.CDCIS_LAB_SCHEMA, sequenceName = EntityConstants.SQ_SENSITIVITY_PATTERN, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = EntityConstants.SQ_SENSITIVITY_PATTERN_NAME)
@Column(name = "sensitivity_pt_id", unique = true, nullable = false, precision = 10, scale = 0)
public long getSensitivityPtId() {
return sensitivityPtId;
}
public void setSensitivityPtId(long sensitivityPtId) {
this.sensitivityPtId = sensitivityPtId;
}
//hash and equals overided.
}
//要删除的代码Parent
@Override
public boolean saveSensitivityData(List < SensitivityDto > sensitivity, long orderId) throws LabWorkListException {
if (sensitivity != null) {
try {
for (SensitivityDto dto: sensitivity) {
if (!dto.isIsSelected()) {
//senPatternRepo.deleteById(dto.getSensitivityId());
super.delete(dto);
}else {
dto.setInvstid(orderId);
updateSensitivityPattern(dto);
super.saveOneEntity(dto);
}
}
} catch (GenericException e) {
logger.error("", e);
}
}
return true;
}
这是删除一个 child 的代码。
/**
* To update the sensitivity data.
* @param dto
*/
private void updateSensitivityPattern(SensitivityDto dto) {
if (dto != null && dto.getSensitivityPattern() != null) {
for (SensitivityPatternDto sPattern: dto.getSensitivityPattern()) {
sPattern.setSensitivity(dto);
if (!sPattern.isIsSelected()) {
dto.setSensitivityPattern(null);
senPatternRepo.delete(sPattern.getSensitivityPtId());
}
}
}
}
此处删除敏感度中的一个值table它抛出异常
org.postgresql.util.PSQLException: ERROR: update or delete on table "lab_tb_sensitivity" violates foreign key constraint "fk_sensitivity_id" on table "lab_tb_sensitivity_pattern"
我搜索了一个解决方案,发现 orphanRemoval=true
可以解决这个问题。是的,它解决了这个问题。
但是一旦添加了 orphanRemoval,当我删除一个时 child 记录它抛出的异常
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: om.gov.moh.model.cdc.Sensitivity.sensitivityPattern
我该如何解决这个问题。
或 Q2) 我可以删除 Parent,它应该自动删除所有 child 而无需使用 orphanRemoval = true 吗?
您要删除的元素仍在父元素中引用。
使用 orphanRemoval
删除子元素非常简单:
parent.getChildren().remove(childToBeRemoved);
然后,如果这不是在交易中完成的:
em.merge(parent);
//灵敏度Table
public class Sensitivity implements Serializable {
private static final long serialVersionUID = 1L;
private long sensitivityId;
public Set<SensitivityPattern> sensitivityPattern = new HashSet<SensitivityPattern>(0);
public Sensitivity() {
}
@Id
@SequenceGenerator(name=EntityConstants.SQ_SENSITIVITY_NAME, schema=EntityConstants.CDCIS_LAB_SCHEMA , sequenceName=EntityConstants.SQ_SENSITIVITY, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=EntityConstants.SQ_SENSITIVITY_NAME)
@Column(name="sensitivity_id", unique=true, nullable=false, precision=10, scale=0)
public long getSensitivityId() {
return this.sensitivityId;
}
public void setSensitivityId(long sensitivityId) {
this.sensitivityId = sensitivityId;
}
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy="sensitivity")
public Set<SensitivityPattern> getSensitivityPattern() {
return sensitivityPattern;
}
public void setSensitivityPattern(Set<SensitivityPattern> sensitivityPattern) {
this.sensitivityPattern = sensitivityPattern;
}
}
//灵敏度模式
public class SensitivityPattern extends AuditableEntity implements Serializable {
private static final long serialVersionUID = 1L;
private long sensitivityPtId;
private Sensitivity sensitivity;
public SensitivityPattern() {}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "sensitivity_id")
public Sensitivity getSensitivity() {
return sensitivity;
}
public void setSensitivity(Sensitivity sensitivity) {
this.sensitivity = sensitivity;
}
@Id
@SequenceGenerator(name = EntityConstants.SQ_SENSITIVITY_PATTERN_NAME, schema = EntityConstants.CDCIS_LAB_SCHEMA, sequenceName = EntityConstants.SQ_SENSITIVITY_PATTERN, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = EntityConstants.SQ_SENSITIVITY_PATTERN_NAME)
@Column(name = "sensitivity_pt_id", unique = true, nullable = false, precision = 10, scale = 0)
public long getSensitivityPtId() {
return sensitivityPtId;
}
public void setSensitivityPtId(long sensitivityPtId) {
this.sensitivityPtId = sensitivityPtId;
}
//hash and equals overided.
}
//要删除的代码Parent
@Override
public boolean saveSensitivityData(List < SensitivityDto > sensitivity, long orderId) throws LabWorkListException {
if (sensitivity != null) {
try {
for (SensitivityDto dto: sensitivity) {
if (!dto.isIsSelected()) {
//senPatternRepo.deleteById(dto.getSensitivityId());
super.delete(dto);
}else {
dto.setInvstid(orderId);
updateSensitivityPattern(dto);
super.saveOneEntity(dto);
}
}
} catch (GenericException e) {
logger.error("", e);
}
}
return true;
}
这是删除一个 child 的代码。
/**
* To update the sensitivity data.
* @param dto
*/
private void updateSensitivityPattern(SensitivityDto dto) {
if (dto != null && dto.getSensitivityPattern() != null) {
for (SensitivityPatternDto sPattern: dto.getSensitivityPattern()) {
sPattern.setSensitivity(dto);
if (!sPattern.isIsSelected()) {
dto.setSensitivityPattern(null);
senPatternRepo.delete(sPattern.getSensitivityPtId());
}
}
}
}
此处删除敏感度中的一个值table它抛出异常
org.postgresql.util.PSQLException: ERROR: update or delete on table "lab_tb_sensitivity" violates foreign key constraint "fk_sensitivity_id" on table "lab_tb_sensitivity_pattern"
我搜索了一个解决方案,发现 orphanRemoval=true
可以解决这个问题。是的,它解决了这个问题。
但是一旦添加了 orphanRemoval,当我删除一个时 child 记录它抛出的异常
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: om.gov.moh.model.cdc.Sensitivity.sensitivityPattern
我该如何解决这个问题。
或 Q2) 我可以删除 Parent,它应该自动删除所有 child 而无需使用 orphanRemoval = true 吗?
您要删除的元素仍在父元素中引用。
使用 orphanRemoval
删除子元素非常简单:
parent.getChildren().remove(childToBeRemoved);
然后,如果这不是在交易中完成的:
em.merge(parent);