EclipseLink JPQL (Glassfish v3):加入获取语法问题?

发布于 2024-08-22 20:21:10 字数 822 浏览 5 评论 0原文

对于 Hibernate,我习惯于执行如下操作:

select n from NetworkElement n join fetch n.site s where s.active is true

然而,EclipseLink 对此抱怨很多:

原因:异常 [EclipseLink-8024](Eclipse 持久性服务 - 2.0.0.v20091127-r5931):org.eclipse.persistence.exceptions.JPQLException 异常描述:解析查询时出现语法错误 [select n from NetworkElement n join fetch n.site s],第 1 行,第 49 列:[s] 处出现语法错误。

(堆栈上的查询与上面的不同,但结果是相同的)

我尝试了不同的组合,但没有一个起作用:

select n from NetworkElement n join fetch n.site where n.site.active is true
select n from NetworkElement n join fetch n.site as site where site.active is true

我还尝试切换到域模型中的不同实体,怀疑也许我的映射不正确。尽管如此,同样的问题。

难道我只能使用查询提示来实现这一点吗?我不想那样做。

顺便说一下,我使用的是 Netbeans 6.8 和 Glassfish v3 附带的 EcliseLink。

我将不胜感激任何帮助!

罗德里戈

With Hibernate I'm used to do something like the following:

select n from NetworkElement n join fetch n.site s where s.active is true

However, EclipseLink complains a lot about this:


Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [select n from NetworkElement n join fetch n.site s], line 1, column 49: syntax error at [s].

(The query on the stack is different from the one above, but the result is the same)

I've tried different combinations, none of which worked:

select n from NetworkElement n join fetch n.site where n.site.active is true
select n from NetworkElement n join fetch n.site as site where site.active is true

I also tried switching to a different entity in my domain model, suspecting that maybe my mapping is not correct. Still, the same problem.

Could it be that I can only achieve this using a query hint? I don't want to do that.

By the way, I'm using EcliseLink as shipped with Netbeans 6.8 and Glassfish v3.

I'd appreciate any help!

Rodrigo

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

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

发布评论

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

评论(2

╰ゝ天使的微笑 2024-08-29 20:21:10

主要问题是 JPQL 语法不允许别名获取连接,这就是 EclipseLink 使用查询提示来实现此功能的原因。有一个增强请求,要求直接在 JPQL 中添加别名连接获取,如果您希望看到它完成,请投票支持它。 ( https://bugs.eclipse.org/bugs/show_bug.cgi?id =293775)。

——戈登·约克

The main issue is that the JPQL syntax does not allow for aliasing fetch joins and that is why EclipseLink uses query hints for this functionality. There is an Enhancement Request to add aliasing join fetches directly in the JPQL and if you would like to see it completed please vote for it. ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=293775 ).

--Gordon Yorke

聆听风音 2024-08-29 20:21:10

好吧,看起来确实不允许在 JPQL 中为 fetch join 别名。它可以与 Hibernate 一起使用,因为它支持通过 HQL 别名,并且允许您向 JPA 查询对象发出 HQL。

因此,我别无选择,只能切换到带有查询提示的命名查询。我不太喜欢用注释来声明查询,因为实体类太冗长,所以我向持久性单元的 jar 中添加了一个 orm.xml 文件,并执行了以下操作:

<!-- Fetch network elements -->
<named-query name="fetchNetworkElements">
    <query>select n from NetworkElement n</query>
    <lock-mode>NONE</lock-mode>
    <hint name="eclipselink.join-fetch" value="n.site" />
    <hint name="eclipselink.join-fetch" value="n.site.area" />
</named-query>

希望这可以为任何遇到同样问题的人提供一些线索原始 JPQL 的缺点。

Well, it seems that one is not allowed to alias a fetch join in JPQL, indeed.. It works with Hibernate because it supports aliasing through HQL, and you are allowed to issue HQL to a JPA Query object.

Therefore, I had no choice but to switch to named queries with query hints. I don't really like declaring queries with annotations because of the high verbosity on the entity classes, so I added a orm.xml file to the persistence unit's jar, and did the following:

<!-- Fetch network elements -->
<named-query name="fetchNetworkElements">
    <query>select n from NetworkElement n</query>
    <lock-mode>NONE</lock-mode>
    <hint name="eclipselink.join-fetch" value="n.site" />
    <hint name="eclipselink.join-fetch" value="n.site.area" />
</named-query>

Hope this gives some clue to anybody struggling with the same shortcomings of the raw JPQL.

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