org.hibernate.ObjectDeletedException:已删除的对象将由cascade重新保存:尝试删除Patient对象时出错

时间:2017-08-27 23:57:14

标签: java spring hibernate spring-mvc jpa

尝试使用Hibernate从数据库中删除Patient对象时,标题中出现错误。

这是我的Patient Java课程:

package com.vivalio.springmvc.model

@Entity
@Table(name = "PATIENT")
@PrimaryKeyJoinColumn(name = "id")
public class Patient extends User implements Serializable {

    @ManyToOne
    @JoinColumn(name = "docteur_id", referencedColumnName = "id")
    private Docteur docteur;

    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "patient", cascade = CascadeType.ALL)
    @OrderBy("dateCreation desc")
    private Set<Consultation> consultations = new HashSet<Consultation>();

    public Set<Consultation> getConsultations() {
        return consultations;
    }

    public void setConsultations(Set<Consultation> consultations) {
        this.consultations = consultations;
    }

    public Docteur getDocteur() {
        return docteur;
    }

    public void setDocteur(Docteur docteur) {
        this.docteur = docteur;
    }

}

这是我的咨询课:

@Entity
@Table(name = "CONSULTATION")
public class Consultation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CONSID")
    private Integer id;

    // poid
    @NotEmpty
    @Column(name = "PARAM1", nullable = false)
    private String param1;

    // appetit
    @NotEmpty
    @Column(name = "PARAM2", nullable = false)
    private String param2;

    // faiblesse
    @NotEmpty
    @Column(name = "PARAM3", nullable = false)
    private String param3;

    // douleur
    @NotEmpty
    @Column(name = "PARAM4", nullable = false)
    private String param4;

    // boule
    @NotEmpty
    @Column(name = "PARAM5", nullable = false)
    private String param5;

    // fievre
    @NotEmpty
    @Column(name = "PARAM6", nullable = false)
    private String param6;

    // Commentaire
    @NotEmpty
    @Column(name = "COMMENTAIRE", nullable = false)
    private String commentaire;

    @Column(name = "DTCREATION", nullable = true)
    private String dateCreation;

    @Column(name = "JJCREATION", nullable = true)
    private String jjCreation;

    @Column(name = "MMCREATION", nullable = true)
    private String mmCreation;

    @Column(name = "YYCREATION", nullable = true)
    private String aaCreation;

    @ManyToOne
    @JoinColumn(name = "patient_id")
    private Patient patient;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }

    public String getParam3() {
        return param3;
    }

    public void setParam3(String param3) {
        this.param3 = param3;
    }

    public String getParam4() {
        return param4;
    }

    public void setParam4(String param4) {
        this.param4 = param4;
    }

    public String getParam5() {
        return param5;
    }

    public void setParam5(String param5) {
        this.param5 = param5;
    }

    public String getParam6() {
        return param6;
    }

    public void setParam6(String param6) {
        this.param6 = param6;
    }

    public String getDateCreation() {
        return dateCreation;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public void setDateCreation(String dateCreation) {
        this.dateCreation = dateCreation;
    }

    public String getJjCreation() {
        return jjCreation;
    }

    public void setJjCreation(String jjCreation) {
        this.jjCreation = jjCreation;
    }

    public String getMmCreation() {
        return mmCreation;
    }

    public void setMmCreation(String mmCreation) {
        this.mmCreation = mmCreation;
    }

    public String getAaCreation() {
        return aaCreation;
    }

    public void setAaCreation(String aaCreation) {
        this.aaCreation = aaCreation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCommentaire() {
        return commentaire;
    }

    public void setCommentaire(String commentaire) {
        this.commentaire = commentaire;
    }
}

执行下面的代码后,我遇到了这个错误:

@Override
    public void deleteBySSO(String sso) {
        Criteria crit = createEntityCriteria();
        crit.add(Restrictions.eq("ssoId", sso));
        Patient user = (Patient) crit.uniqueResult();
        delete(user);
    }
  

SEVERE:servlet [dispatcher]的Servlet.service()与上下文有关   路径[/ vivalio]引发异常[请求处理失败;嵌套   例外是   org.springframework.dao.InvalidDataAccessApiUsageException:已删除   对象将通过级联重新保存(从中删除已删除的对象)   协会):[com.vivalio.springmvc.model.Patient#34];嵌套   异常是org.hibernate.ObjectDeletedException:已删除的对象   将通过级联重新保存(从中删除已删除的对象)   协会):[com.vivalio.springmvc.model.Patient#34]]与root   导致org.hibernate.ObjectDeletedException:删除的对象将是   通过级联重新保存(从关联中删除已删除的对象):   [com.vivalio.springmvc.model.Patient#34] at   org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1272)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:187)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)     在   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)     在   org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)

2 个答案:

答案 0 :(得分:1)

您需要从集合中删除对象或将FetchType设置为延迟加载

deleted object would be re-saved by cascade (remove deleted object from associations)

答案 1 :(得分:0)

从包含一组患者的另一个类设置FetchType为Lazy加载后工作。