File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - How to do associations with join tables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - How to do associations with join tables?" Watch "Hibernate - How to do associations with join tables?" New topic
Author

Hibernate - How to do associations with join tables?

Marcin Mogiela
Greenhorn

Joined: Aug 30, 2007
Posts: 14
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


SCJP 5.0, SCWCD 1.4
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Aug 30, 2007
Posts: 14
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

Joined: Mar 02, 2007
Posts: 4
Did anyone found out any solution for this situation?
[ June 22, 2008: Message edited by: Pradeepta Bhattacharya ]

Pradeepta Bhattacharya,
Marcin Mogiela
Greenhorn

Joined: Aug 30, 2007
Posts: 14
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

Joined: Nov 03, 2001
Posts: 626

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

Joined: May 18, 2010
Posts: 1
*********************************************************
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate - How to do associations with join tables?