休眠与同一个实体的多对多关联 [英] Hibernate many-to-many association with the same entity

查看:159
本文介绍了休眠与同一个实体的多对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个Hibernate问题...:P



使用Hibernate的Annotations框架,我有一个 User 实体。每个用户可以有一个朋友集合:其他用户 s的集合。但是,我一直无法弄清楚如何在 User 类中创建多对多关联,其中包含 User s(使用用户朋友中间表)。

以下是User类及其注释:

  @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)

私人列表< User>朋友;

$ / code>

用户朋友映射表只有两列,两列都是外部键到 tbl_users 表的 uid 列。这两列是 personId (它应映射到当前用户), friendId (它指定当前用户的朋友)。



问题是,即使我已经预先填充了朋友表,所以所有用户在系统中与所有其他用户是朋友。我甚至尝试将关系切换为 @OneToMany ,它仍然为空(尽管Hibernate调试输出显示了一个 SELECT * FROM tbl_friends WHERE personId =?和friendId =?查询,但没有别的)。



有关如何填充此列表的任何想法?谢谢!

办法。您的问题是您错过了另一个收藏。



想想这样 - 如果您将作者/书映射为多对多,您需要Book上的作者集合和Author上的books集合。在这种情况下,您的用户实体代表关系的两端;所以你需要我的朋友和朋友集合:

  @ManyToMany 
@JoinTable(name = tbl_friends,
joinColumns = @ JoinColumn(name =personId),
inverseJoinColumns = @ JoinColumn(name =friendId)

private List< User>朋友;
$ b @ManyToMany
@JoinTable(name =tbl_friends,
joinColumns = @ JoinColumn(name =friendId),
inverseJoinColumns = @ JoinColumn(name = personId)

私人列表<用户> friendOf;

您仍然可以使用相同的关联表,但请注意join / inverseJon列在集合上交换。

friends和friendOf收藏可能会或可能不会匹配(取决于您的友谊总是相互的),而且您不必暴露它们这种方式当然是在你的API中,但这是在Hibernate中映射它的方式。


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 Users. 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 Users (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!

解决方案

@ManyToMany to self is rather confusing because the way you'd normally model this differs from the "Hibernate" way. Your problem is you're missing another collection.

Think of it this way - if you're mapping "author" / "book" as many-to-many, you need "authors" collection on Book and "books" collection on Author. In this case, your "User" entity represents both ends of a relationship; so you need "my friends" and "friend of" collections:

@ManyToMany
@JoinTable(name="tbl_friends",
 joinColumns=@JoinColumn(name="personId"),
 inverseJoinColumns=@JoinColumn(name="friendId")
)
private List<User> friends;

@ManyToMany
@JoinTable(name="tbl_friends",
 joinColumns=@JoinColumn(name="friendId"),
 inverseJoinColumns=@JoinColumn(name="personId")
)
private List<User> friendOf;

You can still use the same association table, but note that join / inverseJon columns are swapped on collections.

The "friends" and "friendOf" collections may or may not match (depending on whether your "friendship" is always mutual) and you don't have to expose them this way in your API, of course, but that's the way to map it in Hibernate.

这篇关于休眠与同一个实体的多对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆