gpt4 book ai didi

java - hibernate 。删除对象

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:27 27 4
gpt4 key购买 nike

我的学生实体和发布之间存在一对多的关系。我需要删除学生的出版物。当我尝试删除发布对象但总是遇到异常时:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org.irs.entities.GroupStudent#1]

我不知道为什么会发生这种情况。我使用 Spring MVC 3.2 和 Hibernate。

学生实体

@Entity
@org.hibernate.annotations.DynamicUpdate(value = true)
@Table(name = "Student")

public class Student implements Serializable {

public Student() {}

public Student(String studentFullName, String studentBook,
int studentEnter, String studentOKR) {
this.studentFullName = studentFullName;
this.studentBook = studentBook;
this.studentEnter =studentEnter;
this.studentOKR = studentOKR;
}

// create connectivity with table GroupStudent
private GroupStudent groupStudent;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "GroupStudentId")
public GroupStudent getGroupStudent() {
return this.groupStudent;
}

public void setGroupStudent(GroupStudent groupStudent) {
this.groupStudent = groupStudent;
}

// create connectivity with table Publication
private Set<Publication> publications = new HashSet<Publication>();

@OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Publication> getPublications() {
return publications;
}

public void setPublications(Set<Publication> publications) {
this.publications = publications;
}
// other methods
}

出版实体

@Entity
@Table(name = "Publication")
public class Publication implements Serializable {
public Publication() {}

public Publication(String publicationTitle, String publicationType,
String publicationPlace, Date publicationDate) {

this.publicationTitle = publicationTitle;
this.publicationType = publicationType;
this.publicationPlace = publicationPlace;
this.publicationDate = publicationDate;
}

// create connectivity with table Student
private Student student;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "StudentId")
public Student getStudent() {
return this.student;
}

public void setStudent(Student student) {
this.student = student;
}

}

GroupStudent实体

@Entity
@Table(name = "GroupStudent")

public class GroupStudent implements Serializable {
public GroupStudent() {}

public GroupStudent(String groupStudentNumber) {
this.groupStudentNumber = groupStudentNumber;
}

// create connectivity with table Student
private Set<Student> students = new HashSet<Student>();

@OneToMany(mappedBy = "groupStudent", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Student> getStudents() {
return this.students;
}

public void setStudents(Set<Student> students) {
this.students = students;
}
}

Controller

@RequestMapping(value = "/deletePublication.html", method = RequestMethod.GET)
public ModelAndView deletePublication(@RequestParam("studentId") Long studentId) {
....
ps.deletePublication(ps.selectPublicationsById(2L));
....
return modelandview;
}

最佳答案

当您尝试获取已存在于 hibernate 上下文中的实体时,会发生此错误。您不能同时拥有两个附加实体。在您的 Controller 中,您首先调用 ps.selectPublicationsById(2L),这可能会导致错误。

尝试用 HQL 或 native SQL 删除替换删除逻辑。

String hql = "delete from Publication where Id= :id";
session.createQuery(hql).setString("id", id).executeUpdate();

关于java - hibernate 。删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451946/

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