我正在寻找如何解决我的Branch对象中的递归和双向一对一关系的方法:
@Entity
@Table("BRANCH")
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@OneToOne
@JoinColumn(name = "id")
private Branch parent;
@OneToOne(mappedBy = "parent")
@JoinColumn(name = "parent_id")
private Branch child;
//Getters and Setters
}
这是可接受的模式吗?
答案 0 :(得分:0)
带有自引用的双向@OneToOne映射的正确方法:
@Entity
@Table("BRANCH")
public class Branch {
@OneToOne
@JoinColumn(name = "parent_id")
private Branch parent;
@OneToOne(mappedBy = "parent")
private Branch child;
您不需要两个外键,单个 关系的拥有方中的外键就足够了。在 反向OneToOne的JPA必须使用mappingBy属性。