title: Hibernate Many-to-Many , merge existing row
body: <p>I tried a sample project about Hibernate Many-to-Many relationship downloaded from
  <a href="http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html" rel="nofollow noreferrer">http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html</a>
  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.</p>
  
  <p><a href="https://stackoverflow.com/questions/7926053/hibernate-many-to-many-duplicates-same-record">Hibernate Many-to-Many, duplicates same record</a></p>
  
  <p>So now my code like this:</p>
  
  <pre><code>    Set&lt;Course&gt; courses = new HashSet&lt;Course&gt;();
      courses.add(new Course("Science"));
      DB db = new DB();
      Student eswar= db.getStudentFromId(1);
      eswar.setCourses(courses);
      session.saveOrUpdate(eswar);
  </code></pre>
  
  <p>And the same Student Eswar is there.</p>
  
  <pre><code>    +------------+--------------+
      | STUDENT_ID | STUDENT_NAME |
      +------------+--------------+
      |          1 | Eswar        |
      |          2 | Joe          |
      +------------+--------------+
  </code></pre>
  
  <p>But the student_course table just updated with new value to the COURSE_ID, but not added a new course.</p>
  
  <pre><code>    +------------+-----------+
      | STUDENT_ID | COURSE_ID |
      +------------+-----------+
      |          1 |         7 | //it was 6 last time
      +------------+-----------+
  </code></pre>
  
  <p>I really needed to see this as this (same student can do several courses):</p>
  
  <pre><code>    +------------+-----------+
      | STUDENT_ID | COURSE_ID |
      +------------+-----------+
      |          1 |         6 |
      |          1 |         7 |
      |          2 |         7 |
      +------------+-----------+
  </code></pre>
  
  <p>Student.Java</p>
  
  <pre><code>@Entity
  @Table(name = "STUDENT")
  public class Student {
  
  private long studentId;
  private String studentName;
  private Set&lt;Course&gt; courses = new HashSet&lt;Course&gt;(0);
  
  public Student() {
  }
  
  public Student(String studentName) {
      this.studentName = studentName;
  }
  
  public Student(String studentName, Set&lt;Course&gt; 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&lt;Course&gt; getCourses() {
      return this.courses;
  }
  
  public void setCourses(Set&lt;Course&gt; courses) {
      this.courses = courses;
  }
  }
  </code></pre>
  
  <p>Course.java</p>
  
  <pre><code>@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;
  }
  
  }
  </code></pre>
  
  <p>I really appreciate your help.</p>