This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Hibernate - How to do associations with join tables?  RSS feed

 
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
 
author and cow tipper
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 ]
 
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
 
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.
 
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

 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!