Можно сохранить одного и того же родителя для 2 детей:
Я хочу отключить эту возможность.
домены:
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@OneToOne
Parent parent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@OneToOne(mappedBy="parent")
private Child child;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@PreRemove
private void preRemove() {
child.setParent(null);
}
}
Как изменить этот код, чтобы отключить возможность иметь одного и того же родителя для 2 детей?Должен ли я создать FK или что-нибудь?Как это сделать элегантно с JPA?