#Friendship relationship JPA
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Are you using relational db or NoSQL
That’s a many to many relation, many users can have many users (friends)
You’ll need a join table in sql to achieve that, or throw on the @ManyToMany annotation on your relation
You said a user has friends
On your first msg
But If that what u want then yes u could use manytomany
your friends DB would look something like this
CREATE TABLE Relationship (
uniqueId INT PRIMARY KEY,
UserID1 INT,
UserID2 INT,
RelationshipType (Friends,Blocked,etc)
);
in this
As friendship is bi-directional like if A is friends of B than B is friend of A (obviously)
so to tackle this we can either have 2 rows showing same relationship in table like
userID1 UserID2
A B
B A
( which is not ideal as this would increase no of rows)
so we can have just 1 row like
userID1 UserID2
A B
and in appliction you can have a Relationship entity and have a RelationshipRepo with findByUserID1OrUserID2 method
and in UserClass
have
list<Relationship> without mentioning any relationship
fetch list of friends on run time and set them in response body before returning in response.
This would increase 1 DB call but its easier to handle incase you want to implement more functionalities in future like finding mutual friends or friends-of-friends.
Hope this helps
Friend
id suggest to rename to "User"
it makes more sense to say User has Friends
rather than Account has Friends
id still go with oneToMany here
are you sure that you want multiple friends to belong to multiple users ?
yeah
@Entity
public class User {
// some fields...
@ManyToMany
@JoinTable(name = "user_friends", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "friend_id"))
private Set<User> friends;
@ManyToMany(mappedBy = "friends")
private Set<User> friendOf;
// constructor, getters, setters, etc..
}
i think this should work
you said that Friend is actually a User
which means User is a friend with User
you dnt have to
you can follow this example
it would create another table
user_friends with columns user_id and friend_id
not entity ofc
the dao pattern can help or service or dunno
👍
the thread can wait for 12 hours of non activity
Set is a collection
like List
it has some features, like it doesn't accept ~~redundant ~~ duplicated objects
User has friends
User is a friend of friends
it's a biderectional relationship
you can get the user's friends
and from a friend, you can know the user
i know it can be confusing but that's it
kinda
i don't think so!
ah yeah
i mean a user and a friend
@valid quail wouldn't it be easier to manage this solution instead of managing such complex relationship?
Or am I missing something?
i see it easier this way
this is how you do in your service class
public void addFriend(User user, User friend) {
user.getFriends().add(friend);
friend.getFriendOf().add(user);
userRepository.saveAll(List.of(user, friend));
}
ofc !
This way we are creating 2 records for 1 relationship. After a point it'll create more redundant rows to represent the same relationship.
hmm maybe not
here we comes to some specification
here is a good situation in which we may need to create an extension
from User
i think no
it will update the existing entities
I am confused a bit. lets say we have a 2 users A and B and they add each other as friends. how will it look in DB ?
the table user_friends will add 1 row
while fetching list of friends of A and list of friends of B. How will it work? as in manyToMany relation we need to mention joinColumn but here we have either or condition like
if user_friends has 2 columns
user1 and user2
which column will we mention in joinColumn as for each friend-relation we will only have 1 record like user1 =A and user2=B? as we can't mention only "user1" or "user2" in joinColumn I think
all this is theoretical, you have to try it out
i did an example like this in the past and it worked
manyToMany ideally work but in this use case it wont due to the nature of relationship we have between user_friends and user entity. Again I may be wrong here.
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍