gpt4 book ai didi

mysql - 如何在JPQL中调用mysql date_format函数

转载 作者:行者123 更新时间:2023-11-30 00:12:03 24 4
gpt4 key购买 nike

我正在使用 eclipseLink 2.4.1JPA 2.0.1 和其他支持工具开发 Web 服务。我创建了一个表,其中有一列日期类型。我想获取特定日期的记录。我正在使用 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'

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

我也尝试过使用namedQuery,但没有成功。

如何调用JPQL这样的函数。

最佳答案

“{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

这当然要求您使用符合 JPA2.1 的实现;您的实现可能有一些其他特定于供应商的 FUNC 或其他不可移植的功能,我没有时间考虑是否有一个标准变体可以满足您的需求

关于mysql - 如何在JPQL中调用mysql date_format函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com