我的大脑开始为此烦恼,这很简单:
@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>();
就像是:
@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>();