Hibernate日期操作

时间:2012-08-31 16:03:50

标签: java database hibernate db2 hql

我在DB2中使用hibernate并遇到以下问题。表/实体Customer包含字段first_namelast_namebirthdayjava.util.Date)。我需要写一个查询,它将检索所有在接下来的四天有生日的客户。问题是,对于一些客户来说,出生年份是未知的,因此在数据库中设置为9999,因此我不能只对where子句进行简单的检查(因为9999年)。

1 个答案:

答案 0 :(得分:1)

使用简单的hql查询

from Customer as user where month(user.birthday) = month(current_date()+4 days) and day(user.birthday) = day(current_date()+4 days)
union all
from Customer as user where month(user.birthday) = month(current_date()+3 days) and day(user.birthday) = day(current_date()+3 days)
union all
from Customer as user where month(user.birthday) = month(current_date()+2 days) and day(user.birthday) = day(current_date()+2 days)
union all
from Customer as user where month(user.birthday) = month(current_date()+1 day) and day(user.birthday) = day(current_date()+1 day)