多对多,带有休眠和用于自我引用的注释

2022-09-04 03:33:32

想到这里,我的大脑开始受伤,就这么简单吗:

@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>();

答案 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>();

答案 2

推荐