I am a little confused when it comes to mapping inheritance in JPA
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
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "ADDRESS_LOOKUP")
@DiscriminatorColumn(name="ADDRESS_TYPE",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("ADDRESS")
public class AddressLookup implements Serializable {
@Id
@Column(name = "ADDRESS_LOOKUP_ID", nullable = false)
private Long addressLookupId;
@OneToMany(cascade = CascadeType.ALL, mappedBy="addressLookup")
private Collection<ServiceRequest> serviceRequestCollection;
...
LandmarkCode:
@Entity
@PrimaryKeyJoinColumn(name="LANDMARK_ID")
@Table(name = "LANDMARK")
@DiscriminatorValue("LANDMARK")
public class Landmark extends AddressLookup implements Serializable {
@Column(name = "LANDMARK_ID", nullable = false, insertable=false, updatable=false)
private Long landmarkId
...
ServiceRequest Code:
@Entity
@Table(name = "SERVICE_REQUEST")
public class ServiceRequest implements Serializable {
@Id
@Column(name = "REQUEST_ID", nullable = false)
private Long requestId;
@ManyToOne
@JoinColumn(name="ADDRESS_LOOKUP_ID", referencedColumnName="ADDRESS_LOOKUP_ID")
private AddressLookup addressLookup;
...
How do I refer to the landmarks collections from a ServiceRequest?
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