在没有时间戳的HQL中比较日期 [英] Date Compare in HQL without timestamp

查看:166
本文介绍了在没有时间戳的HQL中比较日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须比较hibernate hql查询中的两个日期。
我在java bean中使用了java.util.Date,并且在mysql数据库中使用timestamp作为数据类型。

  select t from Task t where t.modifiedDate> t.endDate; 

以上查询将时间与日期进行比较。我应该怎么做才能在没有时间的情况下在上面的查询中比较日期。

解决方案

查看可用日期函数的Hibernate文档



http: //docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html



在14.10节注意这一行

(...),分钟(...),小时(...),日(...),小时(...)月(...)和年(...)

所以这应该工作

  ... where year(t.endDate)>年(t.startDate)
和月(t.endDate)>月(t.startDate)
和日(t.endDate)> day(t.startDate)

解决方案报告满足这个问题:

  ... where DATE(t.endDate)> (t.startDate)


I have to compare two dates in hibernate hql query. I am using java.util.Date in my java bean and using timestamp as datatype in mysql database.

select t from Task t where t.modifiedDate > t.endDate;

Above query compares time with date. What should i do to compare date in above query without time.

解决方案

See the Hibernate documentations for available date functions

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

In section 14.10 notice this line

second(...), minute(...), hour(...), day(...), month(...), and year(...)

So this should work

... where year(t.endDate) > year(t.startDate) 
    and month(t.endDate) > month(t.startDate)
    and day(t.endDate) > day(t.startDate)

The solution reported as satisfying the question is

 ... where DATE(t.endDate) > (t.startDate)

这篇关于在没有时间戳的HQL中比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆