• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicate rows in ManyToMany mapping in Hibernate

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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                |










 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic