1
votes

I am having the error in the title while trying to delete a Patient object from the database using Hibernate.

Here is my Patient Java class :

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

}

here is my consultation class :

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

After executing the code below, i am having this error :

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

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/vivalio] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.vivalio.springmvc.model.Patient#34]; nested exception is org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.vivalio.springmvc.model.Patient#34]] with root cause org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.vivalio.springmvc.model.Patient#34] at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1272) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:187) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)

2
stackoverflow.com/questions/11649249/… Remove the patient from the collectionjonathan.ihm

2 Answers

1
votes

You will need to remove the object from collection or set FetchType to lazy load

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

0
votes

Worked after setting FetchType to Lazy load from another Class that contains a Set of patients.