• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance Mapping and Relationships  RSS feed

Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**<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?.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!