Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many to many with Hibernate and annotations for self referencing

My brain is starting to hurt thinking about this, is it as simple as:

@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>();
like image 713
NimChimpsky Avatar asked Dec 04 '12 17:12

NimChimpsky


1 Answers

Something like:

@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>();
like image 108
jenson-button-event Avatar answered Oct 20 '22 16:10

jenson-button-event