Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate rows in ManyToMany mapping in Hibernate

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating many to many mapping as per the example given in this link Example
The example works fine but when I add another Student later with an existing course like Maths it duplicates the course table.



I want that after executing the above code my course and student_course table should look like this....

COURSE
========================
|COURSE_ID|COURSE NAME|
-------------------------------------
|2                 |Computer Science|
--------------------------------------
|3                 |Maths                  |
-------------------------------------

STUDENT_COURSE
========================
|STUDENT_ID|COURSE_ID|
---------------------------------
|1                |2                |
|1                |3                |
|4                |2                |
|4                |3                |
|5                |3                |










 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you created the course object, you did not pass the key id of the existing course. You just set the courseName.

new Course("Maths")

Try set the key id too.

The reason is: when hibernate saw that the course id is null, it generated a new record for it, assuming it is a new course.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic