-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many To Many relationship on same class
PostPosted: Tue Mar 23, 2010 7:40 pm 
Newbie

Joined: Tue Mar 23, 2010 7:25 pm
Posts: 2
Hi, I guess it's probably a common issue, but even after searching the forum, I don't find the answer

I have an entity User, which contains 2 List<User>
One list is Friends, the other Family.

Obviously this is a many to many relationship (one guy may have several friends or family, and one of his friend can have other User friend)

I can not manage to do the correct mapping
I have now

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "user")
@JoinTable(name = "FRIENDS")
public List<User> getFriends() {
return charms;
}

How should I do the mapping, knowing I want it to be BIDIRECTIONAL, because then I can find the Friends of a User, but I also want to be able to know of which Users I am Friend

Thanks for any help!

Here is my full class:
Quote:
@Entity
@Table(name = "USERS")
public class User implements Serializable {

private Long id;
private String username;
private List<User> Friends = new ArrayList<User>();
private List<User> Family = new ArrayList<User>();

@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name = "USERNAME", nullable = false)
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "user")
@JoinTable(name = "FRIENDS")
public List<User> getFriends() {
return friends;
}

public void setFriends(List<User> friends) {
this.friends = friends;
}

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "user")
@JoinTable(name = "FAMILY")
public List<User> getFamily() {
return family;
}

public void setFamily(List<User> family) {
this.family = family;
}

}


Top
 Profile  
 
 Post subject: Re: Many To Many relationship on same class
PostPosted: Wed Mar 24, 2010 4:44 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
If you want to have it bidirectional then you must declare the ManyToMany twice (one time with mappedBy, one time without).
Try with following (to make it more compact, I annotate directly the properties here)

Code:
@Entity
@Table(name = "USERS")
public class User implements Serializable {

@Id @GeneratedValue @Column(name = "ID", nullable = false)
private Long id;

...

@ManyToMany(mappedBy = "friendOf")
@JoinTable(name = "FRIENDS")
private Set<User> myfriends = new HashSet<User>();

@ManyToMany()
@JoinTable(name = "FRIENDS")
private Set<User> friendOf = new HashSet<User>();



@ManyToMany(mappedBy = "partOfFamily")
@JoinTable(name = "FAMILY")
private Set<User> myfamily = new HashSet<User>();


@ManyToMany()
@JoinTable(name = "FAMILY")
private Set<User> partOfFamily = new HashSet<User>();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.