#Friendship relationship JPA

1 messages · Page 1 of 1 (latest)

white fossilBOT
#

<@&987246584574140416> please have a look, thanks.

#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

frigid hatch
#

Are you using relational db or NoSQL

tulip sequoia
#

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

valid quail
#

It's a OneToMany

#

In user you add a Set of Friend

valid quail
#

You said a user has friends

#

On your first msg

#

But If that what u want then yes u could use manytomany

frigid hatch
#

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

valid quail
#

Friend

valid quail
#

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

frigid hatch
#

Or am I missing something?

valid quail
#

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 !

frigid hatch
valid quail
#

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

valid quail
#

it will update the existing entities

frigid hatch
# valid quail i think no

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 ?

valid quail
#

if u have specification in Friend, that doesn't go in User

#

then yes

valid quail
frigid hatch
#

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

valid quail
#

i did an example like this in the past and it worked

frigid hatch
#

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.

white fossilBOT
#

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 👍