• 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

Understanding many to many mapping in hibernate.

 
Ranch Hand
Posts: 182
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am referring to the example at - http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/
Employees can attend many Meetings and Meetings can be attended by many Employees.

Let then owning class be Employee. The many to many mapping code inside Employee will be -



I looked at several other examples and documentation, but I am still not sure how this works. I don't think I fully understand what joinColumns and inverseJoinColumns do.

joinColumns={@JoinColumn(name="EMPLOYEE_ID")} - Does this line only tell hibernate that Employee id of employee joins employee id of the mapping table ?

inverseJoinColumns={@JoinColumn(name="MEETING_ID")}) ? Does this line only tell hibernate to join Employee id to the Meeting_Id column in the link table ?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand how it works in a typical database? It can't use a basic foreign key because that only works for one to many relationships. Instead you have a Employee table, a Meeting table, and a special Employee-Meeting linking table. The linking table only exists to relate employees to meetings, and its only fields (probably) are the employee id and meeting id. So, if I'm an employee with ID 1, and I went to a meeting on how to produce more widgets with a gadget (meeting ID102), and another on how to reduce belly fat with meditation (meeting ID 703), then the linking table is going to have:




That's really all there is to it. You have to tell Hibernate the name of the linking table, the name of the field that keys into the "owning" table of the relationship, and the name of the field that keys into the "subordinate" table. Don't let the joinColumn and inverseJoinColumn names confuse you. Coming up with good names for things isn't easy, and I'm sure the folks that created Hibernate did their best!
 
Ali Gordon
Ranch Hand
Posts: 182
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much greg. I thought there was more to these things than just simple PK, FK relationships. Look at the confusing explanation of Hibernate made easy -



Of course, the first annotation @JoinColumn only tells hibernate how to save the PK of the Employee class, not how to find out the name of the column than maps the Employee
to its associated Meeting. So, to help hibernate process the inverse, Meeting to Employee relationship, we add a second @JoinColumn, but associate it with the inverseJoinColumns of
@JoinTable annotation :



I'd like to rephrase it as -

To join an Employee to the join table, we put a the join column in joinColumns. To join Employee to a Meeting, we put a join column in
inverseJoinColumns. The inverse is a misnomer. There is no inversion going on.



 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, the inversion is really just a way of looking at it. Because the Employee is arbitrarily designated here as the owning table, you have:

Employee ⇒ Employee-Meeting ⇐ Meeting

... so the "inverse" is the left facing arrow. You could designate Meeting as the owning table, and then you'd have to flip the join column and inverse join column in the Hibernate config. It wouldn't make any difference to the database schema. Now, I say that with about 95% confidence. Hopefully someone will come along and either confirm or refute that.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic