【问题标题】:How to call SQL Function from Spring @Query Annotation如何从 Spring @Query Annotation 调用 SQL 函数
【发布时间】:2015-01-06 09:43:56
【问题描述】:

这里的语法有什么问题:

@Modifying
@Query(nativeQuery = true, value = "delete from simple_token where simple_token.expiry < NOW()")
int deleteExpiredTokens();

我得到了异常

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's where s.expiry < now()' at line 1

但“按原样”执行的查询将在 MYSQL 客户端中正常执行。

我在这里做错了什么?

【问题讨论】:

    标签: java mysql spring-data


    【解决方案1】:

    您声明的查询和语法错误不同。 where s.expiry &lt; now() 在异常中,但 simple_token.expiry &lt; NOW()@Query 中。看起来你运行了错误的查询。可能是别名不正确

    【讨论】:

    • 感谢您的回答,这是我的愚蠢错误。我感到困惑的一个原因是 IntelliJ 14 抱怨 now() 函数,尽管使用它是完全合法的。过度依赖IDE的案例
    【解决方案2】:

    抱歉,我是个白痴。 sql 'as is' 在 mysql 客户端中不起作用。我将代码更改为:

    @Modifying
    @Query(nativeQuery = true, value = "delete from simple_token where expiry < NOW()")
    int deleteExpiredTokens();
    

    现在可以正常使用了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-31
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多