JPA 查询 MONTH/YEAR 函数 [英] JPA Query MONTH/YEAR functions

查看:65
本文介绍了JPA 查询 MONTH/YEAR 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 MONTH 函数编写 JPA 查询,就像 sql 查询一样?

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 表达式查询(类似于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆