Answer the question
In order to leave comments, you need to log in
How to set up a Many To Many relationship within the same entity in Spring Jpa?
Hello.
I suspect that the question is a little stupid, but still.
I am making a server using Spring 4.2.4 and Spring Data Jpa, the model is something like a social network, there is an entity (let's call it "User" conditionally), you need to store data on who this user subscribed to, and, accordingly, who is subscribed to this user . That is, there is a Many To Many connection, but there is one problem - the same entity is located on both sides of it.
I'm thinking of coding it like this:
@ManyToMany
@JoinTable(name="following_follower",
joinColumns = @JoinColumn(name="follower_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name="following_id", referencedColumnName="id"))
private Set<User> following;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "following")
private Set<User> followers;
Answer the question
In order to leave comments, you need to log in
I will not tell you how it is implemented in Java. But such a database model exists for sure.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question