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

问题描述

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



由于在db中生成的表全都是相同的,所以我发现的唯一区别是每一侧的双向联合会有一个引用另一个,而单向不是。



这是一个单向关联

  public class User {
private int id;
私人字符串名称;
@ManyToOne
@JoinColumn(
name =groupId)
私人群组;
}

public class Group {
private int id;
私人字符串名称;
}

双向关联

  public class User {
private int id;
私人字符串名称;
@ManyToOne
@JoinColumn(
name =groupId)
私人群组;
}
public class Group {
private int id;
私人字符串名称;
@OneToMany(mappedBy =group)
private List< User>用户;
}

区别在于组是​​否拥有用户的引用。



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

解决方案

主要区别在于双向关系提供了双向导航访问,以便您可以访问其他没有明确的疑问。此外,它允许您在两个方向上应用级联选项。



请注意,导航访问并不总是很好,特别是对于一对多和多非常多的关系。想象一下 Group ,其中包含数千个用户 s:




  • 您如何访问它们?有这么多的 User s,你通常需要应用一些过滤和/或分页,所以你需要执行一个查询(除非你使用,这对我来说看起来像一个黑客)。在这种情况下,一些开发人员可能倾向于在内存中应用过滤,这显然不利于性能。请注意,建立这样的关系可以鼓励这类开发人员在不考虑性能的前提下使用它。

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


所以,我不能同意来自。您需要仔细设计双向关系,考虑用例(您是否需要双向导航访问?)以及可能的性能影响。 strong>





What is the difference between Unidirectional and Bidirectional associations?

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.

This is a Unidirectional association

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

The Bidirectional association

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.

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:

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

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

See also:

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

08-05 23:38