单向和双向 JPA 和 Hibernate 关联有什么区别? [英] What is the difference between Unidirectional and Bidirectional JPA and Hibernate associations?

查看:31
本文介绍了单向和双向 JPA 和 Hibernate 关联有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单向和双向关联有什么区别?

What is the difference between Unidirectional and Bidirectional associations?

由于db中生成的表都是一样的,所以我发现唯一的区别是双向关联的每一侧都会有一个对另一侧的引用,而单向没有.

Since the table generated in the db are all the same,so the only difference I found is that each side of the bidiretional assocations will have a refer to the other,and the unidirectional not.

这是一个单向关联

public class User {
    private int     id;
    private String  name;
    @ManyToOne
    @JoinColumn(
            name = "groupId")
    private Group   group;
}

public class Group {
    private int     id;
    private String  name;
}

双向关联

public class User {
    private int     id;
    private String  name;
    @ManyToOne
    @JoinColumn(
            name = "groupId")
    private Group   group;
}
public class Group {
    private int         id;
    private String      name;
    @OneToMany(mappedBy="group")
    private List<User>  users;
}

区别在于组是​​否持有用户的引用.

The difference is whether the group holds a reference of the user.

所以我想知道这是否是唯一的区别?推荐哪个?

So I wonder if this is the only difference? which is recommended?

推荐答案

主要区别在于双向关系提供了双向导航访问,因此您无需显式查询即可访问另一侧.它还允许您将级联选项应用于两个方向.

The main differenece is that bidirectional relationship provides navigational access in both directions, so that you can access the other side without explicit queries. Also it allows you to apply cascading options to both directions.

请注意,导航访问并不总是好的,尤其是对于一对多"访问.和多对多"关系.想象一个包含数千个 UserGroup:

Note that navigational access is not always good, especially for "one-to-very-many" and "many-to-very-many" relationships. Imagine a Group that contains thousands of Users:

  • 您将如何访问它们?有这么多User,您通常需要应用一些过滤和/或分页,以便您无论如何都需要执行查询(除非您使用集合过滤,对我来说这看起来像是一个黑客).在这种情况下,一些开发人员可能倾向于在内存中应用过滤,这显然对性能不利.请注意,拥有这种关系可以鼓励此类开发人员在不考虑性能影响的情况下使用它.

  • How would you access them? With so many Users, you usually need to apply some filtering and/or pagination, so that you need to execute a query anyway (unless you use collection filtering, which looks like a hack for me). Some developers may tend to apply filtering in memory in such cases, which is obviously not good for performance. Note that having such a relationship can encourage this kind of developers to use it without considering performance implications.

您如何将新的 User 添加到 Group?幸运的是,Hibernate 在持久化时会查看关系的拥有方,因此您只能设置 User.group.但是,如果要保持内存中的对象一致,还需要将User 添加到Group.users.但是它会让 Hibernate 从数据库中获取 Group.users 的所有元素!

How would you add new Users to the Group? Fortunately, Hibernate looks at the owning side of relationship when persisting it, so you can only set User.group. However, if you want to keep objects in memory consistent, you also need to add User to Group.users. But it would make Hibernate to fetch all elements of Group.users from the database!

所以,我不能同意 最佳做法.您需要仔细设计双向关系,考虑用例(您是否需要双向导航访问?)和可能的性能影响.

So, I can't agree with the recommendation from the Best Practices. You need to design bidirectional relationships carefully, considering use cases (do you need navigational access in both directions?) and possible performance implications.

另见:

这篇关于单向和双向 JPA 和 Hibernate 关联有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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