如何在JPQL中调用mysql date_format函数

时间:2014-05-31 04:41:55

标签: mysql java-ee jpa eclipselink

我正在使用eclipseLink 2.4.1JPA 2.0.1和其他支持工具开发网络服务。我创建了一个具有日期类型列的表。我想获得特定日期的记录。我正在使用mysql的date_format并收到以下错误:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing[select o from Clients o  where FUNCTION('date_format',{o.effectiveStartDate},{d,'dd-mm-yyyy'})='27-05-2014']
[90, 111] The JDBC escape format does not start with either 'd', 't' or 'ts'.
[110, 110] The JDBC escape format is missing the close quote.

即使我试图直接调用date_format:

Select o from Clients o  where date_format(o.effectiveStartDate '%d-%m-%Y'})='27-05-2014'

然后我得到语法错误解析。 invalid token [(]

我也试过了namedQuery,但没有成功。

如何调用这样的函数JPQL。

1 个答案:

答案 0 :(得分:0)

“{o.effectiveStartDate}”试图做什么?删除大括号,或者它会尝试将其解释为JDBC转义语法日期(它不是)。

JPA 2.1'FUNCTION'语法为

FUNCTION("date_format", o.effectiveStartDate, "the_date_format")

其中“the_date_format”匹配MySQL根据http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format接受第二个arg的内容

这当然要求您使用符合JPA2.1的实现;你的实现可能有一些其他供应商特定的FUNC或其他不可移植的,我没有时间,当有一个标准变体应该做你需要的