gpt4 book ai didi

java - org.hibernate.ObjectDeletedException : deleted object would be re-saved by cascade : Error while trying to delete a Patient object

转载 作者:行者123 更新时间:2023-11-30 06:32:04 26 4
gpt4 key购买 nike

我在尝试使用 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;
}

}

这是我的咨询类(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;
}
}

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

@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)

最佳答案

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

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

关于java - org.hibernate.ObjectDeletedException : deleted object would be re-saved by cascade : Error while trying to delete a Patient object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com