Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

hibernate mapping 3 entities to 1 join table  RSS feed

 
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.

 
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.

 
security forum advocate
Ranch Hand
Posts: 216
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!