休眠。删除对象

时间:2013-12-08 09:42:25

标签: java spring hibernate

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

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

控制器

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

1 个答案:

答案 0 :(得分:1)

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

尝试用HQL或本机SQL删除替换delete logicc。

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