JPQL / HQL 获取连接语法与 EclipseLink 和 EclipseLink 兼容休眠

发布于 2024-09-14 11:35:49 字数 956 浏览 6 评论 0原文

我希望能够在 EclipseLink 和 EclipseLink 之间交换我的 JPA 实现。通过简单的属性更改即可休眠。我可以做到这一点,但导致我出现问题的是命名查询验证。使用 EclipseLink 我必须像这样编写 fetch joins:

SELECT id 
FROM IndexDefinition id 
JOIN FETCH id.index 
JOIN id.index i
JOIN FETCH i.indexVersions

但是当使用 Hibernate 验证此查询时,我得到以下异常:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list 

因此我可以将查询更改为 HQL 友好,如下所示:

SELECT id 
FROM IndexDefinition id 
JOIN FETCH id.index i
JOIN FETCH i.indexVersions

但这会导致 EclipseLink 中出现以下异常:

Exception Description Syntax error parsing the query [IndexDefinition.getForIndex] SELECT id FROM IndexDefinition id JOIN FETCH id.index i, syntax error at [id].
Internal Exception MismatchedTokenException(78!=-1)

我知道我可以在我的 DAO 级别使用查询提示来执行获取连接,但是有没有一种方法可以使用特定的获取连接语法来满足两个 JPA 实现的需要?

I would like to be able to swap my JPA implementation between EclipseLink & Hibernate with a simple property change. I can do this ok but what is causing me problems is the named query validation. Using EclipseLink I have to write fetch joins like so:

SELECT id 
FROM IndexDefinition id 
JOIN FETCH id.index 
JOIN id.index i
JOIN FETCH i.indexVersions

But when validating this query using Hibernate I get the following exception:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list 

So I can change the query to be HQL friendly like so:

SELECT id 
FROM IndexDefinition id 
JOIN FETCH id.index i
JOIN FETCH i.indexVersions

But this then causes the following exception in EclipseLink:

Exception Description Syntax error parsing the query [IndexDefinition.getForIndex] SELECT id FROM IndexDefinition id JOIN FETCH id.index i, syntax error at [id].
Internal Exception MismatchedTokenException(78!=-1)

I know I can use query hints at my DAO level to perform a fetch join but is there a way of pleasing both JPA implementations with a specific fetch join syntax?

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

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

发布评论

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

评论(1

煮茶煮酒煮时光 2024-09-21 11:35:49

好吧,JPA 1.0 似乎不支持 嵌套联接。来自 JPA 1.0 规范:

4.4.5.3 获取连接

FETCH JOIN 可以获取
作为副作用的关联
执行查询。 FETCH JOIN 是
指定实体及其
相关实体。

获取连接的语法是

fetch_join ::= [ 左[外部] | INNER ] JOIN FETCH join_association_path_expression

引用的关联
FETCH JOIN 子句的右侧
必须是属于以下组织的协会
作为结果返回的实体
的查询。 不允许
指定一个识别变量
权利引用的实体
FETCH JOIN 子句的一侧,以及
因此隐含地引用了
获取的实体无法出现
查询中的其他位置

以下查询返回一组
部门。作为副作用,
相关员工
部门也被检索,甚至
尽管他们不属于
显式查询结果。执着的
员工的领域或财产
急切获取的内容已完全
已初始化。的初始化
的关系属性
检索到的员工是
由元数据确定
员工实体类。

<前><代码>选择d
FROM 部门 d LEFT JOIN FETCH d.employees
其中 d.deptno = 1

获取连接具有相同的连接
语义作为相应的内在
或外连接,除了相关的
右侧指定的对象
连接操作的一侧不是
在查询结果中返回或者
否则在查询中引用。
因此,例如,如果部门 1
有5名员工,以上查询
返回五个引用
部门 1 实体。

因此,虽然某些 JPA 提供程序可能支持嵌套联接,但其行为并未标准化。

参考

  • JPA 1.0 规范
    • 第 4.4.5.3 节“获取连接”
  • JPA Wikibook

Well, it seems that nested joins are not supported by the JPA 1.0. From the JPA 1.0 specification:

4.4.5.3 Fetch Joins

A FETCH JOIN enables the fetching of
an association as a side effect of the
execution of a query. A FETCH JOIN is
specified over an entity and its
related entities.

The syntax for a fetch join is

fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH join_association_path_expression

The association referenced by the
right side of the FETCH JOIN clause
must be an association that belongs to
an entity that is returned as a result
of the query. It is not permitted to
specify an identification variable for
the entities referenced by the right
side of the FETCH JOIN clause, and
hence references to the implicitly
fetched entities cannot appear
elsewhere in the query
.

The following query returns a set of
departments. As a side effect, the
associated employees for those
departments are also retrieved, even
though they are not part of the
explicit query result. The persistent
fields or properties of the employees
that are eagerly fetched are fully
initialized. The initialization of the
relationship properties of the
employees that are retrieved is
determined by the metadata for the
Employee entity class.

SELECT d
FROM Department d LEFT JOIN FETCH d.employees
WHERE d.deptno = 1

A fetch join has the same join
semantics as the corresponding inner
or outer join, except that the related
objects specified on the right-hand
side of the join operation are not
returned in the query result or
otherwise referenced in the query.
Hence, for example, if department 1
has five employees, the above query
returns five references to the
department 1 entity.

So, while nested joins might be supported by some JPA providers, the behavior is not standardized.

References

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