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

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

问题描述

另一个Hibernate问题......:P

Another Hibernate question... :P

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

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 (它指定了当前用户的朋友)。

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 =?AND friendId =?查询,但没有别的。)

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相当令人困惑,因为你通常建模的方式不同于Hibernate办法。你的问题是你错过了另一个系列。

@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天全站免登陆