File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Inheritance Mapping and Relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Inheritance Mapping and Relationships" Watch "Inheritance Mapping and Relationships" New topic

Inheritance Mapping and Relationships

Brian Smith
Ranch Hand

Joined: May 20, 2005
Posts: 63
I am a little confused when it comes to mapping inheritance in JPA (Hibernate Annotations)

I have an entity (AddressLookup) which has a subclass (Landmark). The parent entity is related to another entity (ServiceRequest) OneToMany.

I am using the Table per subclass (JOINED) mapping pattern.




Now when writing a query how do I refer to Landmarks via ServiceRequests?

In order for the following to be valid, should I map Landmark to AddressLookup OneToOne?

Landmark landmark = serviceRequest.getAddressLookup.getLandmark();

It is not a problem going the other way since Landmark inherits getServiceRequestCollection from AddressLookup but ServiceRequest knows nothing of the subclass Landmark.

Thanks for any help.

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
**<Newbie here>

Hello Brian,

ServiceRequest can continue to keep the AddressLookup as the reference point.

Once we get all the AddressLookups linked to ServiceRequest we can keep a method inside AddressLookup which returns the complete

We can keep a method inside the AddressLookup that gets all the Landmarks or any other child classes which may be linked to ServiceRequest. The only issue is the signature of the method :

public AddressLookup getAddressLookup(ServiceRequest sr)
//fire query where you retrieve address lookup and the associated extra
information within any child class.
// i.e get landmarks linked through join between AddressLookup and
Landmark (Join being defined through the Id)
// This way if tomorrow some ServiceRequest are linked to Landmark
and others linked to NewlyDefinedLandmarks(Another table extending
AddressLookup) there would be no impact on code.

But how can ServiceRequest access the properties specific to Landmark or NewlyDefinedLandmark ?. Casting?.


/** Code speaks louder than words */
I agree. Here's the link:
subject: Inheritance Mapping and Relationships
It's not a secret anymore!