FETCH JOIN 最大深度?

发布于 2025-01-07 14:37:29 字数 480 浏览 1 评论 0原文

W 试图获取超过三个级别的连接:

JOIN FETCH entity1.collection1.collection2  // two OneToMany relations

但是得到:

org.hibernate.HibernateException: Errors in named queries: [...]

是因为它太深了,还是因为无法以这种方式获取集合的集合?我的最大获取深度是 3,如果这是相关的。

同时,我可以从另一侧开始执行三重 JOIN FETCH:

JOIN FETCH entity3.entity2.entity1  // two ManyToOne relations

不知何故,我在 JPA 规范或 Hibernate 文档中找不到任何会限制此子句深度的内容。

W was trying to fetch join over three levels:

JOIN FETCH entity1.collection1.collection2  // two OneToMany relations

but got:

org.hibernate.HibernateException: Errors in named queries: [...]

Is it because it was too deep, or because a collection of collections cannot be fetched this way? My max fetch depth is 3, if this is relevant.

I can, at the same time, do a triple JOIN FETCH starting from the other side:

JOIN FETCH entity3.entity2.entity1  // two ManyToOne relations

Somehow I cannot find anything in JPA specification, or in Hibernate docs, that would limit the depth of this clause.

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

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

发布评论

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

评论(1

贵在坚持 2025-01-14 14:37:30

collection1 的类型为CollectionCollection 没有 collection2 字段。这就是我对此类查询的推理方式。

您必须在集合上创建显式联接:

select e from Entity1 e
left join fetch e.collection1 as c1
left join fetch c1.collection2 as c2

请注意,这将产生笛卡尔积,因此可能返回大量行。另请注意,只有当两个集合中至少有一个是集合时才有可能。如果它们都是包,Hibernate 在执行查询时会抛出异常。

collection1 is of type Collection. And a Collection doesn't have a collection2 field. That's how I reason about those kind of queries.

You must the create an explicit join over the collection:

select e from Entity1 e
left join fetch e.collection1 as c1
left join fetch c1.collection2 as c2

Note that this will produce a cartesian product, and thus petentially returns a huge number of rows. Also note that it will only be possible if one of the two collections at least is a set. If they're both bags, Hibernate will throw an exception when executing the query.

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