Hibernate多对多,合并现有行

发布于 2024-12-12 13:35:17 字数 3371 浏览 0 评论 0原文

我尝试了一个关于 Hibernate 多对多关系的示例项目,下载自 http:// www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html 然后,当我要向现有学生添加课程时,我遇到了重复同一个学生的问题,但从此处提出的上一个问题解决了这个问题。

Hibernate多对多,重复相同的记录

现在我的代码就像这样:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-12-19 13:35:17

你只需要在你的代码中执行以下操作,我希望它能开始工作
DB db = 新的 DB();

    Student eswar= db.getStudentFromId(1);
    Set<Courses> c = eswar.getCourses();
    c.add(new Course("Science"));
    eswar.setCourses(c);

    session.saveOrUpdate(eswar);

目前你正在做的是从数据库中获取学生,并将其课程设置为新集合,这个新集合不包含旧的旧课程......所以当你调用SaveOrUpdate()方法时,同一个学生得到使用新的 SET 进行更新,但是这个新的 SET 不包含旧的条目....

you just need to do following in your code, and I hope it will start working
DB db = new DB();

    Student eswar= db.getStudentFromId(1);
    Set<Courses> c = eswar.getCourses();
    c.add(new Course("Science"));
    eswar.setCourses(c);

    session.saveOrUpdate(eswar);

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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文