是否可以在JPA @Query语句中具有条件来确定时间戳是否匹配当前日期?
我有一个现有的JPA @Query,我需要在其中仅在当前日期匹配时间戳列的位置返回行中添加加法条件。基本上想知道是否有可能的事情是可能的:
@Query("SELECT .. " +
"WHERE .. " +
"AND isCurrentDay(c.completedTs))"
否则可以通过本机查询来完成我需要的东西。
编辑:我的解决方案最终是使用本机查询,例如:
@Query(value = "SELECT ... " +
"WHERE ... " +
"AND CONVERT (date, c.completedTs) = CONVERT (date, GETDATE()))",
nativeQuery = true)
I have an existing JPA @Query that I need to add an addition condition to the WHERE such that it'll only return rows where the current date matches that of a timestamp column. Basically wondering if something along the lines of this is possible:
@Query("SELECT .. " +
"WHERE .. " +
"AND isCurrentDay(c.completedTs))"
Otherwise can accomplish what I need with a native query.
Edit: My solution ended up being to use a Native Query like:
@Query(value = "SELECT ... " +
"WHERE ... " +
"AND CONVERT (date, c.completedTs) = CONVERT (date, GETDATE()))",
nativeQuery = true)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用本机查询使用curdate SQL语言说明您正在写的语言会更安全。例如,对于MySQL + JPA,它看起来像这样:
I'll be it would be safer to do it through native queries using the
CURDATE
sql statement of the language you are writing in. For example, for MySQL + JPA it would look something like this:JPQL暴露了很少的功能以关联当前日期/时间:
从文档中:
参考: javadoc
可以通过以下方式使用这些直接访问当前日期,
请注意,afaik,这些是香草函数,不支持表达式,即,您不能执行Current_Date-1。
JPQL Exposes few functions to associate CURRENT date/time :
From the docs :
ref : javadoc
These can be used in the following way to directly access current date
Please note , AFAIK , these are vanilla functions and do not support expressions , i.e , you cannot do CURRENT_DATE-1.