JPA查询MONTH/YEAR函数 [英] JPA Query MONTH/YEAR functions
问题描述
如何像SQL查询一样使用MONTH
函数编写JPA查询?
How can I write a JPA query using MONTH
function just like sql query?
@NamedQuery(name="querybymonth", query="select t from table1 t where MONTH(c_Date) = 5")
当我使用上述模式进行查询时,出现错误:unexpected token - MONTH
.
When I use the above pattern for query, I get an error: unexpected token - MONTH
.
推荐答案
如果使用的是EclipseLink(2.1),则可以使用FUNC()函数来调用JPA JPQL规范中未定义的任何数据库函数.
If you are using EclipseLink (2.1) you can use the FUNC() function to call any database function that is not defined in the JPA JPQL spec.
即 FUNC('MONTH',c_Date)
i.e. FUNC('MONTH', c_Date)
在JPA 2.1(EclipseLink 2.5)中, FUNCTION 语法已成为一部分规范(并替代了EclipseLink的FUNC).
In JPA 2.1 (EclipseLink 2.5) the FUNCTION syntax becomes part of the specification (and replaces the EclipseLink-specific FUNC).
如果使用的是TopLink Essentials,则无法在JPQL中执行此操作,但是可以为其定义TopLink Expression查询(类似于JPA 2.0标准),或使用本机SQL.
If you are using TopLink Essentials, you cannot do this in JPQL, but you can define a TopLink Expression query for it (similar to JPA 2.0 criteria), or use native SQL.
此外,如果您正在使用任何JPA 2.0提供程序并使用Criteria查询,那么可以使用function()API来定义它.
Also if you are using any JPA 2.0 provider and using a Criteria query there is a function() API that can be used to define this.
这篇关于JPA查询MONTH/YEAR函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!