aspose file tools*
The moose likes Object Relational Mapping and the fly likes Inheritance/one table for each class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Inheritance/one table for each class" Watch "Inheritance/one table for each class" New topic
Author

Inheritance/one table for each class

Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
hi,
on his book "Spring Persistence with Hibernate" Ahmad Reza Seddighi discusses alternatives to mapping inheritance hierarchy in Hibernate (pages 102-103). On the example we have Person, Student and Teacher entities. Student and Teacher extend Person. On the example Ahmad is demonstrating "One table for each concrete class" solution for the mapping inheritance problem. Now Ahmad rises question where Address entity is added to schema. This is excat quote:

If Address has a many-to-one relationship with Person (more than one person may have the same address), then the ADDRESS table should provide a foreign key reference to all PERSON, STUDENT, and TEACHER tables, in order to establish database-level relationship, but this is not possible.


I'm confused why is that not possible. Why it would be impossible to have, lets-say "AddessID", column/foreign key for PERSON,STUDENT and TEACHER tables?

any help greatly appreaciated
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

With the example they suggest the multiplicity of the association is reversed. An address_id field on person for example means one address can be used by zero or more persons, and a person can have one address (or zero if the fk is nullable) . A person_id on address means a person can have many addresses, though an address itself can only be used by one person.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
hi and thanks for answer, but I'm still puzzled

On the example I'm referring to it is as You stated: "one address can be used by zero or more persons", so wouldn't that mean that PERSON table should have foreign key pointing to ADDRESS ?

lets say we have 1 row in ADDRESS:
ID;street; city
A1;1600 Pennsylvania Avenue; Washington

and then 2 rows in PERSON:
ID;name; ADDRESS_ID (fk)
01;Barack Obama; A1
02;Michelle Obama;A1

here we have 2 persons in one address, not vice versa

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I think you are misunderstanding what is meant by many-to-one. Cardinality is described as the relationship from one entity to another, so many-to-one is many addresses to one person. That is not possible with an address id defining the relationship on the person table. However (like I said) the reverse is.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance/one table for each class