HQL日期查询问题
如果我期待一个 Date 对象,比如说 2011-03-05
,并且我想使用 HQL 来提取 2011-03 中的所有内容,忽略 day/05
之类的东西 whereyear(somecolumn) =year(datepassedin ) 和月份(somecolumn) = 月份(datepassedin)
可以在 HQL 中执行吗?
在 SQL 中似乎很容易做到,但在 HQL 中则不然
if I am expecting a Date object say for 2011-03-05
and I want to use HQL to pull everything in 2011-03, ignoring the day/05
something like where year(somecolumn) = year(datepassedin) and month(somecolumn) = month(datepassedin)
is it possible to do in HQL?
It seems to be pertty easy to do in SQL, but not in HQL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么 HQL 有年份和月份 表达式 你可以给它提供日期对象就好了。因此,只需像往常一样使用 Query 对象即可,没有什么花哨或复杂的。
Well HQL has year and month expressions and you can feed it date objects just fine. So, just use a Query object as usual, nothing fancy or complicated.
我在 SQL Server 中尝试了同一查询的两个不同版本,类似于
whereyear(somecolumn)
比括号查询慢得多(几乎一个数量级)。您必须创建两个日期,一个位于范围的开头,一个位于范围的末尾。假设您有一个字符串:
在某处创建一个方便的常量来解析该值:
然后:
在您的查询中,您将拥有
where somecolumn >= :fromValue 和 somecolumn < :toValue
。该代码是用 Scala 编写的,但它使用标准 Java 库。
I tried two different versions of this same query and in SQL Server, something like what you have
where year(somecolumn)
was significantly slower (by almost an order of magnitude) than a bracketing query.You have to create two dates, one at the beginning of the range, and one at the end. Say that you have a string:
Somewhere create a convenient const for parsing the value:
And then:
In your query, you'll have
where somecolumn >= :fromValue and somecolumn < :toValue
.This code is written in Scala, but it uses standard Java libraries.