Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - How to do associations with join tables?

 
Marcin Mogiela
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There is a "student" table and a "course" table (many to many relationship)
There is also a "grade" that I wanted to join with the "student_course" join table. (many one)

How can I do it in Hibernate? I am confused because "student_course" join table will be generated by Hibernate through @ManyToMany annotation


Thanks,
Marcin
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread discussed a very similar issue:

JavaRanch Thread on Adding Properties to Joined Many to Many Table Example

Not to ruin your enjoyment of reading through that long thread, I think an example of a program that adds a property to a class representing the join table of a many-to-many relationship in Hibernate3 can be found here:

hibernate annotations composite mapping

Post back, and let us know if this puts you in the right direction.

-Cameron McKenzie
 
Marcin Mogiela
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron, it's a pleasure talking to you

The example from http://java-aap.blogspot.com/2006/04/hibernate-annotations-composite.html is a little too advanced for me.
I have only two days experience with hibernate and have read half and a little tutorial from your site http://www.hiberbook.com/HiberBookWeb/learn.jsp

All I understood from the example was that if a join table has its own properties a new POJO should be created


Now for the magic:
I've added 2 extra getters and setters for Product and Item to make it easier to get or set a product or item on your primary key. I added to extra columns to make hibernate think there is a Product mapped to this table (and an Item) but hibernate never updates or inserts this property (becuase it doensn't exists in our db.) But when hibernate does the getProduct() method it will retrieve the Product from our composite foreign key and all will work fine!


I don't understand this magic. Right now it seems to me like a hack

Thanks
[ May 19, 2008: Message edited by: Marcin Mogiela ]
 
Pradeepta Bhattacharya
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did anyone found out any solution for this situation?
[ June 22, 2008: Message edited by: Pradeepta Bhattacharya ]
 
Marcin Mogiela
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeepta,

I did something like below, though it may not be the optimal solution.

If anybody had better solution for this problem or found any flaw in the problem itself I would highly appreciate any suggestions.

First of all I introduced the join class Enrollment.



From Enrollment.hbm.xml


From Course.hbm.xml


From Student.hbm.xml


From Grade.hbm.xml
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marcin Mogiela:


All I understood from the example was that if a join table has its own properties a new POJO should be created


Cameron,
what if we don't have extra properties in join table. Do we still need extra POJO for join table? I have a working example without have new POJO for join table though I have not created compound primary key for join table.

So I have threet ables Group, Receiver and receiver_group.
While I have two classes Group and Reciver but when I insert the data the join table also gets updated.
 
Bauyrzhan Matyganov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*********************************************************
how to write a setter and getter methods for Student and Course fields in Enrolment class?





Marcin Mogiela wrote:Hi Pradeepta,

I did something like below, though it may not be the optimal solution.

If anybody had better solution for this problem or found any flaw in the problem itself I would highly appreciate any suggestions.

First of all I introduced the join class Enrollment.



From Enrollment.hbm.xml


From Course.hbm.xml


From Student.hbm.xml


From Grade.hbm.xml
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic