Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hibernate parent/child and foreign key relationships mapping

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic