使用 fetch join 或 EAGER 解决 hibernate multiple-bag-fetch-issue?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“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.
我更喜欢获取集合(尽管如果您使用休眠,查询中也存在单次获取的限制),因为变化是您有时希望列出所有 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.
在急切的集合获取中,Hibernate 默认情况下“交叉连接”您的根表和集合表。这可能会导致返回重复的根元素,这可能是您不希望的。三种可能的解决方案:
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:
考虑将集合映射为列表而不是包。只需添加
@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