• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Inheritance/one table for each class

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic