2012-08-31 73 views
2

我在DB2中使用hibernate并存在以下问题。表/实体Customer具有字段first_name,last_name,birthdayjava.util.Date)。我需要写一个查询,它将检索所有在未来四天有生日的客户。问题在于,对于某些客户来说,出生年份不详,因此它在数据库中设置为9999,因此我不能仅对where子句(因为9999年)进行简单检查。休眠日期操作

+0

如何将9999转换为一个日期吗? – Vikdor

+2

对于未知值,通常最好使用NULL而不是虚拟。另外,你是用HQL还是Criteria API来查询数据库? – vstoyanov

+0

@Vikdor:有两种情况。首先是整个日期是已知的,所以保存到数据库。其他情况是只知道出生日期和月份。例如,一个客户于10月12日出生,所以它将被保存为10.12.9999到db,我不能改变这个逻辑。 –

回答

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) 
+0

我认为这是我正在寻找的东西,但如果当前月份是十二月份,月份的结果(current_date()+ 4)是什么? –

+0

取决于日期如果日期28.12.2012然后1. –

+0

我不确定有关DB2的SQL语法可能需要添加'+4天'或'+1天'。 –