从 JPQL 中的时间戳变量中提取年/月

Extract Year / Month from a TimeStamp variable in JPQL

在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.

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 解析器时此格式是否有效。