小编典典

使用Hibernate和批注进行自我引用的多对多关系

java

我的大脑开始为此烦恼,这很简单:

@ManyToMany(mappedBy = "following", cascade = CascadeType.ALL)
private Set<User> followers = new HashSet<User>();

@ManyToMany(mappedBy = "followers", cascade = CascadeType.ALL)
private Set<User> following = new HashSet<User>();

阅读 211

收藏
2020-11-16

共1个答案

小编典典

就像是:

@ManyToMany(mappedBy = "following", cascade = CascadeType.ALL)
@JoinTable(name="UserRel", 
                joinColumns={@JoinColumn(name="ParentId")}, 
                inverseJoinColumns={@JoinColumn(name="UserId")})
private Set<User> followers = new HashSet<User>();

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="UserRel", 
                joinColumns={@JoinColumn(name="UserId")}, 
                inverseJoinColumns={@JoinColumn(name="ParentId")})
private Set<User> following = new HashSet<User>();
2020-11-16