使用 fetch join 或 EAGER 解决 hibernate multiple-bag-fetch-issue?

发布于 2024-12-20 03:34:05 字数 204 浏览 2 评论 0原文

我有一个 A 类,它与 B 类和 B 类具有一对多关系。 C. 我已将 fetchType 设置为 EAGER,这会导致 JPA 中出现多包提取问题。现在我知道有两种方法可以解决这个问题。要么我保留获取类型 Eager &将它们从 List 更改为 Set,这在我的情况下是可以的,或者将 fetchType 更改为“lazy”并使用联接查询来 EAGER 获取关系。推荐的方法是什么?

I have a Class A which has one-to-many relationship with class B & C. I had set the fetchType to EAGER which causes the multiple bag fetch problem in JPA. Now there are two ways to solve the problem which I know of. Either I keep the fetch type Eager & change them from List to Set which is OK in my case, or change fetchType to "lazy" and use join query to EAGER fetch the relationships. What is the recommended approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

像你 2024-12-27 03:34:05

“JPA 中的多个包获取问题”

请注意,它们不是“JPA 中的多个包获取问题”,这是 Hibernate 的一个特定问题。其他 JPA 提供商不存在此问题,并且 JPA 完全允许您拥有多个 EAGER OneToMany 关系。

"multiple bag fetch problem in JPA"

Note that their is no "multiple bag fetch problem in JPA", this is a specific issue with Hibernate. Other JPA providers do not have this issue, and JPA fully allows you to have multiple EAGER OneToMany relationships.

小兔几 2024-12-27 03:34:05

我更喜欢获取集合(尽管如果您使用休眠,查询中也存在单次获取的限制),因为变化是您有时希望列出所有 A 对象而不想要 B 和 B 对象。 C. 就我个人而言,我主要使用列表,因为我想将集合直接显示到 jsf 数据表中(无法处理集合),并且我被迫通过在后端手动读取集合来初始化集合,而不是在集合大于时获取它们一个单一的集合。

I'd prefer to fetch the collection (though the limit of a single fetch exists in queries aswell if you use hibernate), since changes are that you sometimes wish to list all A objects without wanting B & C. Personally I mostly use Lists since I want to show the collections directly into jsf datatables (which can't handle sets), and I'm forced to initialize collections by manually reading them in the backend rather than fetching them if it's more than a single collection.

爱你不解释 2024-12-27 03:34:05

在急切的集合获取中,Hibernate 默认情况下“交叉连接”您的根表和集合表。这可能会导致返回重复的根元素,这可能是您不希望的。三种可能的解决方案:

  1. 不要急切获取;
  2. 使用 FetchMode.SELECT 强制选择获取(仍然渴望但没有连接获取);或
  3. 将结果散列到集合中。建议设置方法,前提是您绝对需要始终附加所述集合。即使有额外的哈希步骤,连接获取的性能也比选择获取更好,这几乎不需要任何成本。

On eager collection fetch, Hibernate "cross-joins" your root and collection tables by default. This may lead to duplicate root elements being returned, which you may not want. Three possible solutions:

  1. don't eager fetch;
  2. force a select fetch (still eager but without the join fetch) using FetchMode.SELECT; or
  3. hash the results into a Set. Set approach recommended, provided you absolutely need always to have said collection attached. Peformance of a join fetch is better than select fetch even with the extra hashing step, which costs practically nothing.
甜尕妞 2024-12-27 03:34:05

考虑将集合映射为列表而不是包。只需添加 @org.hibernate.annotations.IndexColumn 即可完成此操作,或者从 JPA 2.0 开始,更喜欢 @javax.persistence.OrderColumn

Consider mapping the collection as a list rather than bag. This is done by just adding the @org.hibernate.annotations.IndexColumn or, since JPA 2.0, prefer @javax.persistence.OrderColumn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文