【问题标题】:Extract Year / Month from a TimeStamp variable in JPQL从 JPQL 中的时间戳变量中提取年/月
【发布时间】:2019-10-08 22:17:29
【问题描述】:

在 JSF+JPA 应用程序中,需要按年和月搜索名为“createdAt”的时间戳属性。我使用了 Extract(Year, createdAt),但它给出了一个错误。

持久性提供程序是 EclipseLink 2.6(这是 Netbeans 中的当前默认值)。数据库是 MySQL。

这是一个简化的查询

select c from Client c where EXTRACT(YEAR,c.createdAt)=:ey and  EXTRACT(MONTH,c.createdAt)=:eq 

错误是

Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select count(distinct c)    from ClientEncounterComponentItem i join i.itemClient c  where i.retired=:f  and i.item=:v1  and i.integerNumberValue>:d1 and EXTRACT(YEAR,c.createdAt)=:ey and  EXTRACT(MONTH,c.createdAt)=:eq  and i.itemClient.person.province=:area    ]. 
[154, 166] The right expression is not a valid expression.
[167, 263] The query contains a malformed ending.

【问题讨论】:

  • 简化您的查询 - 是 Extract 还是其他子句的问题?
  • 简化查询
  • 为什么不使用 "select c from Client c where EXTRACT( YEAR, c.createdAt ) = :ey" 并显示新的异常消息和堆栈跟踪?根据(wiki.eclipse.org/EclipseLink/UserGuide/JPA/…),您也可以尝试“ EXTRACT( YEAR FROM c.createdAt ) = :ey”

标签: jpa eclipselink


【解决方案1】:

https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Functions 的文档显示查询的正确格式应该是:

select c from Client c where EXTRACT(YEAR FROM c.createdAt)=:ey and  EXTRACT(MONTH FROM c.createdAt)=:eq

EclipseLink 单元测试验证了在支持该格式的数据库上使用 Hermes 解析器时该格式是否有效。

【讨论】:

  • 什么是 Hermes 解析器? MySQL 支持吗?谷歌搜索,但失败。
  • Hermes 是 EclipseLink 用于 JPQL 处理的解析器,以帮助它生成表达式,然后生成 SQL。
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2018-06-20
  • 2021-02-18
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多