I have a model objects relationship such as this:
@Table(name="cables")
class Cable {
@Id
private Long id;
@Column(name="dstport_id")
private Port dstPort;
@Column(name="srcport_id")
private Port srcPort;
}
@Table(name="ports")
class Port {
@Id
private Long id;
private Cable cable; // Here's the mapping that should point to cables.dstport_id or cables.srcport_id whatever is present
}
In this relationship the mapping is a One-to-One mapping through Cable’s dstport_id OR srcport_id columns. A cable could be connected to none, one or two (exclusively different) ports. A Port could be connected to none or only one cable and to it’s either endpoint. So, is there a way in Hibernate to map such a relationship inside the Port entity (there’s no tricks to map it inside the Cable entity)?
I don’t believe it is possible to define the relationship from Port to Cable with only one association in Port.
In my view, your best option would be to define 2 optional, one-to-one associations to Cable in Port (
dstPortCableandsrcPortCable, say) using the ‘mappedBy’ joining method, and then define a method in Port, thus: