-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Mapping User-Friends relation in Social Networks
PostPosted: Thu Nov 27, 2008 10:01 pm 
Newbie

Joined: Wed Jul 02, 2008 8:44 pm
Posts: 14
Location: Zurich, Switzerland
It's quite some time that I'm trying to figure out this problem and from googling around many people have similar problems.

I'm trying to model a User in a Social Network, and what is more basic to a social network than to map a friendship relation?
Every user in the system should have a list of it's friends and I thought that this might be an incredibly easy task (just use a ManyToMany relation, right?). So I went on to try the following:

Code:
@Entity
@Table(name="users")
public class User {
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="userid")
   protected Long id = null;
   @ManyToMany
   protected List<User> friends = null
}


The problem now is that it tells me I use ManyToMany wrongly by having no clear distinction between friend and befriended. So far so good, I get the error, but how can I do what I want?

Any idea? I've reached the end of my wisdom.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 8:41 pm 
Newbie

Joined: Wed Jul 02, 2008 8:44 pm
Posts: 14
Location: Zurich, Switzerland
I'm still having this problem. Really no one knows how to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 5:42 pm 
Newbie

Joined: Tue Jan 16, 2007 3:44 pm
Posts: 10
Snyke wrote:
I'm still having this problem. Really no one knows how to do this?

You need a joint table: user_friend.

Code:
   @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}
    )
    @JoinTable(
        name="USER_FRIEND",
        joinColumns=@JoinColumn(name="USER_ID"),
        inverseJoinColumns=@JoinColumn(name="FRIEND_ID")
    )
    public List getFriends() {
        return friends;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 25, 2008 9:21 am 
Newbie

Joined: Wed Jul 02, 2008 8:44 pm
Posts: 14
Location: Zurich, Switzerland
That surely is a solution, and for now I've adopted it.
The downside of course is that I have 2 entries for every "friendship" which is quite redundant.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.