• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate mapping 3 entities to 1 join table

 
bramez bramezito
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
bramez bramezito
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic