Reputation: 516
I have two classes
class Point {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "point_id")
private Long id;
@Column(name = "name")
private String name;
}
class Link {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "link_id")
private Long id;
@Column(name = "name")
private String name;
@OneToOne
@JoinColumn(name = "fp_id")
private Point firstPoint;
@OneToOne
@JoinColumn(name = "sp_id")
private Point secondPoint;
}
If I remove Link I get constraint error. I want to get the following functionality:
How to configure this relation?
UPDATE Diagram of DB
Upvotes: 0
Views: 236
Reputation: 2417
This doesn't look like a one-to-one association to me, since a point could have multiple incoming links. It looks rather like a VIRTUAL one-to-many on the point side and two many-to-one associations on the link side.
Now, actually mapping the one-to-many is very tricky, since it would be need to be mapped to TWO columns on the child side. You could fix this by having two collections on point, one for each column in link, like incoming and outgoing links, effectively turning the undirected graph into a directed graph, but that would change the logic.
A simple mapping with two many-to-one properties would be easiest to implement. Deleting the links should then be done by the application, right before deleting a point, by using a hql batch delete operation: delete from link where firstPoint = :point or secondPoint = :point.
If you really require hibernate to do the deletion for you, I would suggest to create two collections with cascade=delete.
Upvotes: 1