aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate parent/child and foreign key relationships mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate parent/child and foreign key relationships mapping" Watch "Hibernate parent/child and foreign key relationships mapping" New topic
Author

Hibernate parent/child and foreign key relationships mapping

Prince Manchanda
Ranch Hand

Joined: Jun 25, 2001
Posts: 52
I am trying to understand some concept of O/R mapping using Hibernate by taking some pseudo example.

I have two entities Student and Instructor. Both of these have some common properties like first name, last name, address etc.
Also these can have specific properties like student has roll number while instructor has employee code. To model this I have created following classes:


Person class is abstract since I want to use it's specialised children.


I have ommitted the getter/setter and other methods.
I have following mapping files:
Instructor.hbm.xml

Person.hbm.xml

And similarly Student.hbm.xml and Address.hbm.xml.

My database schema is

Field person_id in teacher is having foreign key relationship with table person.

Now my problem is that if i try to load data for Instructor, I am not able to get the records from the Person table. I have tried HibernateTemplate's loadAll(Instructor) method and find(query) method

Am I doing something wrong in modelling the entities either in Object model or data model side. Or is there anything wrong in my hibernate mappings.

Also I am using derby as database and Spring's DAO framework.


Thanks and Regards
P Manchanda
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"P Manchanda",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, I am actually seeing two different concepts in your "mapping" I see a one-to-many in the database between two tables, and in Java I see an inheritance. Both require mapping and bother require different mapping for each part.

I highly recommend, while learning that you split those two totally different concepts with two totally different examples.

I also highly recommend for examples and learning to use the Caveat Emptor sample Hibernate app. Which can be downloaded from www.hibernate.org

Good Luck

Mark
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
Mark,
do you mind to share with us how would you handle the case of P Manchanda

beside, Manchanda, can you specifically point out the relation among object ?

for instance : is student have m:n relation to instructor ?

thanks for guidance

[ April 11, 2008: Message edited by: Nakata kokuyo ]
[ April 11, 2008: Message edited by: Nakata kokuyo ]
Prince Manchanda
Ranch Hand

Joined: Jun 25, 2001
Posts: 52
Thanks for the response guys.
Mark can you elaborate bit more on
Both require mapping and bother require different mapping for each part.


It would be great if you can find some time to post a sample mapping file or do you feel there is a better way to create an object model and data model for this scenerio.

Nakata kokuyo
There can be many to many relationship between student and instructor.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Here is the Hibernate docs on Inheritence mapping

http://www.hibernate.org/hib_docs/v3/reference/en/html/inheritance.html

Here is the Hibernate docs to Association mapping

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html

They are two different chapters.

So in the example of Student/Instructor the parent to child relationship would be mapped via an Association, and the fk id, when related to shared data between the abstract and subclasses would be mapped using the Inheritence mapping techiques.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate parent/child and foreign key relationships mapping