Hibernate多对多,合并现有行
我尝试了一个关于 Hibernate 多对多关系的示例项目,下载自 http:// www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html 然后,当我要向现有学生添加课程时,我遇到了重复同一个学生的问题,但从此处提出的上一个问题解决了这个问题。
现在我的代码就像这样:
Set<Course> courses = new HashSet<Course>();
courses.add(new Course("Science"));
DB db = new DB();
Student eswar= db.getStudentFromId(1);
eswar.setCourses(courses);
session.saveOrUpdate(eswar);
同一个学生埃斯瓦尔也在那里。
+------------+--------------+
| STUDENT_ID | STUDENT_NAME |
+------------+--------------+
| 1 | Eswar |
| 2 | Joe |
+------------+--------------+
但是student_course表只是用COURSE_ID的新值更新了,但没有添加新课程。
+------------+-----------+
| STUDENT_ID | COURSE_ID |
+------------+-----------+
| 1 | 7 | //it was 6 last time
+------------+-----------+
我真的需要看到这个(同一个学生可以做几门课程):
+------------+-----------+
| STUDENT_ID | COURSE_ID |
+------------+-----------+
| 1 | 6 |
| 1 | 7 |
| 2 | 7 |
+------------+-----------+
Student.Java
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>(0);
public Student() {
}
public Student(String studentName) {
this.studentName = studentName;
}
public Student(String studentName, Set<Course> courses) {
this.studentName = studentName;
this.courses = courses;
}
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_COURSE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") })
public Set<Course> getCourses() {
return this.courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course.java
@Entity
@Table(name="COURSE")
public class Course {
private long courseId;
private String courseName;
public Course() {
}
public Course(String courseName) {
this.courseName = courseName;
}
@Id
@GeneratedValue
@Column(name="COURSE_ID")
public long getCourseId() {
return this.courseId;
}
public void setCourseId(long courseId) {
this.courseId = courseId;
}
@Column(name="COURSE_NAME", nullable=false)
public String getCourseName() {
return this.courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
我真的很感谢你的帮助。
I tried a sample project about Hibernate Many-to-Many relationship downloaded from
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html
Then I had a problem of duplicating same student when I going to add a course to a existing student , but solved it from a previous question put in here.
Hibernate Many-to-Many, duplicates same record
So now my code like this:
Set<Course> courses = new HashSet<Course>();
courses.add(new Course("Science"));
DB db = new DB();
Student eswar= db.getStudentFromId(1);
eswar.setCourses(courses);
session.saveOrUpdate(eswar);
And the same Student Eswar is there.
+------------+--------------+
| STUDENT_ID | STUDENT_NAME |
+------------+--------------+
| 1 | Eswar |
| 2 | Joe |
+------------+--------------+
But the student_course table just updated with new value to the COURSE_ID, but not added a new course.
+------------+-----------+
| STUDENT_ID | COURSE_ID |
+------------+-----------+
| 1 | 7 | //it was 6 last time
+------------+-----------+
I really needed to see this as this (same student can do several courses):
+------------+-----------+
| STUDENT_ID | COURSE_ID |
+------------+-----------+
| 1 | 6 |
| 1 | 7 |
| 2 | 7 |
+------------+-----------+
Student.Java
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>(0);
public Student() {
}
public Student(String studentName) {
this.studentName = studentName;
}
public Student(String studentName, Set<Course> courses) {
this.studentName = studentName;
this.courses = courses;
}
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_COURSE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") })
public Set<Course> getCourses() {
return this.courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course.java
@Entity
@Table(name="COURSE")
public class Course {
private long courseId;
private String courseName;
public Course() {
}
public Course(String courseName) {
this.courseName = courseName;
}
@Id
@GeneratedValue
@Column(name="COURSE_ID")
public long getCourseId() {
return this.courseId;
}
public void setCourseId(long courseId) {
this.courseId = courseId;
}
@Column(name="COURSE_NAME", nullable=false)
public String getCourseName() {
return this.courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
I really appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你只需要在你的代码中执行以下操作,我希望它能开始工作
DB db = 新的 DB();
目前你正在做的是从数据库中获取学生,并将其课程设置为新集合,这个新集合不包含旧的旧课程......所以当你调用SaveOrUpdate()方法时,同一个学生得到使用新的 SET 进行更新,但是这个新的 SET 不包含旧的条目....
you just need to do following in your code, and I hope it will start working
DB db = new DB();
currently what u are doing is get student from database, and set its courses to the new Set, this new set does not contains the old old course........so when u callSaveOrUpdate() method, the same student gets updated with the new SET, but this new SET does not contain the old entry....