This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
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
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.