File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Android Security Essentials Live Lessons this week in the Android 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

hibernate mapping 3 entities to 1 join table

bramez bramezito

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.


Sean Clark

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.


I love this place!
bramez bramezito

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?


Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 199

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:
subject: hibernate mapping 3 entities to 1 join table
Similar Threads
Designing with Interface or Abstract class
1 to many case : howto ?
using for:each in xsl
creating a database structure.
UML Generalization Problem