| Author |
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. AddressLookup Landmark ServiceRequest 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. -Brian
|
 |
Shivani Chandna
Ranch Hand
Joined: Sep 18, 2004
Posts: 371
|
|
**<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?. Regards, Shivani
|
/** Code speaks louder than words */
|
 |
 |
|
|
subject: Inheritance Mapping and Relationships
|
|
|