Another Hibernate question... :P
Using Hibernate's Annotations framework, I have a User
entity. Each User
can have a collection of friends: a Collection of other User
s. However, I have not been able to figure out how to create a Many-to-Many association within the User
class consisting of a list of User
s (using a user-friends intermediate table).
Here's the User class and its annotations:
@Entity
@Table(name="tbl_users")
public class User {
@Id
@GeneratedValue
@Column(name="uid")
private Integer uid;
...
@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE},
targetEntity=org.beans.User.class
)
@JoinTable(
name="tbl_friends",
joinColumns=@JoinColumn(name="personId"),
inverseJoinColumns=@JoinColumn(name="friendId")
)
private List<User> friends;
}
The user-friend mapping table has only two columns, both of which are foreign keys to the uid
column of the tbl_users
table. The two columns are personId
(which should map to the current user), and friendId
(which specifies the id of the current user's friend).
The problem is, the "friends" field keeps coming out null, even though I've pre-populated the friends table such that all the users in the system are friends with all the other users. I've even tried switching the relationship to @OneToMany
, and it still comes out null (though the Hibernate debug output shows a SELECT * FROM tbl_friends WHERE personId = ? AND friendId = ?
query, but nothing else).
Any ideas as to how to populate this list? Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…