Tharaka Deshan
Tharaka Deshan

Reputation: 1396

Hibernate Many-to-Many , merge existing row

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.

Upvotes: 0

Views: 3096

Answers (1)

Zohaib
Zohaib

Reputation: 7116

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

Upvotes: 1

Related Questions