Hibernate递归多对多关联同一个实体 [英] Hibernate recursive many-to-many association with the same entity

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

问题描述

另一个 Hibernate 问题...:P

Another Hibernate question... :P

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

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

这是 User 类及其注释:

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;
}

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

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

问题是,即使我已经预先填充了朋友表,系统中的所有用户都是所有其他用户的朋友,但朋友"字段仍然为空.我什至尝试将关系切换到 @OneToMany,但它仍然为 null(尽管 Hibernate 调试输出显示 SELECT * FROM tbl_friends WHERE personId = ? ANDfriendId = ?代码>查询,但没有别的).

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 相当令人困惑,因为您通常对其建模的方式与休眠"方式不同.您的问题是您缺少另一个集合.

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

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

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

friends"和friendOf"集合可能匹配也可能不匹配(取决于您的友谊"是否总是相互的),当然,您不必在 API 中以这种方式公开它们,但这就是在 Hibernate 中映射它的方法.

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.

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

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