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 mapping 3 entities to 1 join table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate mapping 3 entities to 1 join table" Watch "hibernate mapping 3 entities to 1 join table" New topic
Author

hibernate mapping 3 entities to 1 join table

bramez bramezito
Greenhorn

Joined: Feb 18, 2011
Posts: 2
Hi all,
I have 3 entities, let's say students, subjects and teachers in 3 different tables with 2columns each (id and name).

The logic is:
1-. Every student can have many subjects
2-. Every subject for a particular student can only have 1 teacher
3-. Every teacher can have many subjects.

I am new at hibernate and I have read lots of documentation about associations between 2 entities, but how can i handle relations with 3 entities? Does anyone know how to handle this problem?

I'm using hibernate anotations, btw.

Thanks.

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

If you want to have a jointable between 3 then I think you will need to create a 4th entity that maps to the jointable that has One-To-Many joins to the other 3 tables.
However you may want to look over your schema as I think you will end up with lots of duplicate data in the join table which will make it difficult if say 2 teachers were to swap classes.

Sean


I love this place!
bramez bramezito
Greenhorn

Joined: Feb 18, 2011
Posts: 2
Thanks for your reply sean.

My question now is how can I improve my schema so that there is not duplicated data in the join table?

And how can i make OneToMany joins to the other 3 tables?

bramez.

Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Maybe this might help you a bit -

Subjects can be a domain table. You can add in another entity called a course. Students can enroll for many courses. Now lets say you have Course 1780 and 1781 that are both Physics courses but they are being taught by different teachers. A student can anytime register to anyone of the courses.

Use the @JoinColumn annotatio to join to other tables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate mapping 3 entities to 1 join table