[英]Hibernate ManyToMany Multiple Entries
我正在使用休眠 4.3。
我為Student
創建了以下實體。
@Entity
@Table(name="STUDENT")
public class Student {
public Student(){
}
public Student(String name, Set<Course> courses){
this.studentName = name;
this.courses = courses;
}
@Id
@GeneratedValue
@Column(name="STUDENT_ID")
private long studentid;
@Column(name="STUDENT_NAME")
private String studentName;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="STUDENT_COURSE",
joinColumns=@JoinColumn(name="STUDENT_ID"),
inverseJoinColumns=@JoinColumn(name="COURSE_ID")
)
private Set<Course> courses = new HashSet<Course>(0);
//Getter Setter Methods
}
另一個實體是Course
。
@Entity
@Table(name = "COURSE")
public class Course {
public Course(String courseName) {
this.courseName = courseName;
}
@Id
@GeneratedValue
@Column(name = "COURSE_ID")
private long courseID;
@Column(name = "COURSE_NAME")
private String courseName;
@ManyToMany(mappedBy="courses")
private Set<Student> students = new HashSet<Student>(0);
//Getter Setter Methods
// I have added equality and hashcode check below
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Course)) {
return false;
}
Course anotherCourse = (Course) obj;
// return this.courseName == anotherCourse.courseName;
return (this.courseName == null)? anotherCourse.courseName == null : this.courseName.equals(anotherCourse.courseName);
}
@Override
public int hashCode() {
return courseName.hashCode();
}
}
在我的應用程序中,我的代碼如下:
// Configuration and Session creation for Hibernate
Set<Course> courses = new HashSet<Course>();
courses.add(new Course("Maths"));
Student st1 = new Student("ABCD", courses);
session.save(st1);
courses.add(new Course("Physics"));
Student st2 = new Student("EFGH", courses);
session.save(st2);
在上述情況下,它插入了無效數據,因為這兩個課程都適用於兩個學生。 這是不正確的,但在 Java 中對象是相同的,所以這是正確的。 但我希望課程按照上面的定義進行映射。 如何在 Hibernate 端處理這個問題?
我嘗試了另一種選擇:
Set<Course> courses = new HashSet<Course>();
Set<Course> courses1 = new HashSet<Course>();
courses.add(new Course("Maths"));
Student st1 = new Student("ABCD", courses);
session.save(st1);
courses1.add(new Course("Maths"));
courses1.add(new Course("Physics"));
Student st2 = new Student("EFGH", courses1);
session.save(st2);
但是這次它為相同的courseName = "Maths"
創建了兩個不同的課程。 即使我已經創建了一個equals
和hashCode
方法實現。
需要解決方案,如何在 Hibernate 中處理這個問題。
我找到了該問題的解決方案,如下所示:
Course maths = new Course("Maths");
Course physics = new Course("Physics");
Set<Course> courses = new HashSet<Course>();
Set<Course> courses1 = new HashSet<Course>();
courses.add(maths);
Student st1 = new Student("ABCD", courses);
session.save(st1);
courses1.add(maths);
courses1.add(physics);
Student st2 = new Student("EFGH", courses1);
session.save(st2);
在此,我為Course
創建了對象,並在兩個集合中使用了相同的對象。 所以在數據庫中它只為課程Maths
創建了一個行條目。
從而達到目的。 謝謝你。
我相信您的問題來自Course
類中equals()
方法的實現
事實上,當你這樣做時:
return this.courseName == anotherCourse.courseName;
,您將this.courseName
的內存引用與anotherCourse
的courseName
進行比較。
相反,您應該使用 equals() 方法,如下所示:
return this.courseName.equals(anotherCourse.courseName);
,它比較字符串內容而不是引用。
在您的第二次嘗試中,由於您創建了 2 個對象(具有相同的值),並且由於您在Course
的equals()
實現比較了對象的引用(內存地址)而不是它們的內容,Hibernate 認為它們是不同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.