Hibernate多对多,合并现有行

时间:2011-10-28 12:15:38

标签: java hibernate java-ee many-to-many

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

Hibernate Many-to-Many, duplicates same record

现在我的代码就像这样:

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

同样的学生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;
}

}

我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您只需要在代码中执行以下操作,我希望它能够开始工作         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);

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