删除一个子记录时的孤儿删除,cascade =“all-delete-orphan”异常

时间:2017-01-17 10:16:07

标签: spring-mvc jpa cascading-deletes all-delete-orphan

//敏感度表

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;
    }

}

// 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.
}

//删除父级的代码

 @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;
    }

这是删除一个孩子的代码。

 /**
     * 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());
                }
            }
        }
    }

这里删除敏感度表中的一个值它抛出异常

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,当我删除一个子记录时会抛出异常

 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,它应该自动删除所有子节点而不使用orphanRemoval = true吗?

1 个答案:

答案 0 :(得分:1)

您正在删除仍然在父级中引用的元素。

使用orphanRemoval删除子元素非常简单:

parent.getChildren().remove(childToBeRemoved);

然后,如果在事务中没有这样做:

em.merge(parent);