Scenario #1 facebook add friend

Hi ,

I’m using MySql Hibernate , and I have a theory question

scenario : A sends friend request to B => B approve => QUESTION

Question : Now in the databse I need A to be B friends and B to be A friend ,

In code , when A adds B to his friends list ,

A.addfriend(B) and then 

does the databse should also add A to B friend automatically?

or addintilnal additional code is needed for B add A as friend ?


depends on whether you are storing the friendship in two rows or in only one

can you explain what you mean 1 or 2 rows?
Doesn’t your depend , depends on my answer for my question ? :slight_smile: It can be changes regarding the ‘right way’

As it is now , it doesn’t save automatically and the table is :


user_id ------ friend_id
Aid------------- Bid
Aid -------------Xid

storing the relationship in one row means this –

user_id ------ friend_id
Aid------------- Bid

storing the relationship in two rows means this –

user_id ------ friend_id
Aid------------- Bid
Bid------------- Aid

both of those approaches work fine

for only one row, if you want to find somebody’s friends, you have to look for that person in both columns

for two rows, if you want to find somebody’s friends, you would look for that person in only the user_id column

I can’t see how one row helps , because with one row I can only do


which will get me B , but if I do


, it will be empty .

I can’t see cleary how one/two rows help answer , although
the two rows seems what I want and need , for both methods from to work will non empty results.

thanks again for your time

if you want to store two rows for the friendship, then you must do so

and no, to answer your question, the database cannot add the second row automatically

you have to do it with code