什么是单向和双向协会之间的区别? [英] What is the difference between Unidirectional and Bidirectional associations?

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

问题描述

什么是单向和双向协会之间有什么区别?

What is the difference between Unidirectional and Bidirectional associations?

由于在db生成的表都是一样的,因此,我发现的唯一的区别在于bidiretional关联设定的每一侧会有一个参考,另外,和单向不

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?

推荐答案

主要differenece是双向的关系提供了两个方向导航式访问,这样就可以没有明确的查询访问​​的另一面。此外,它允许你申请级联选项的两个方向。

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.

需要注意的是导航式访问并不总是好的,尤其是对一到非常多和多到非常多的关系。想象一下,一个,其中包含数千个用户的S组

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:


  • 你会如何访问它们?有了这么多的用户 S,你通常需要应用一些过滤和/或分页,让你总有需要执行一个查询(除非你使用的收集过滤,它看起来像我的一个黑客)。一些开发商可能倾向于在内存中,应用过滤在这种情况下,这显然不利于性能。需要注意的是有这样的关系可以鼓励这样的开发人员使用它没有考虑性能问题。

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

你会如何添加新的用户 S到?幸运的是,休眠着眼于关系的拥有方坚持,当它,所以你只能设置 User.group 。但是,如果你想保持在内存中的对象一致,还需要用户添加到 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.

参见:

  • Deterring "ToMany" Relationships in JPA models
  • Hibernate mapped collections performance problems

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

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