如何在休眠标准中使用CURRENT_DATE函数?

时间:2010-09-30 13:54:43

标签: java hibernate orm hql criteria

我想将以下HQL转换为Criteria表示法:

from Deal
where CURRENT_DATE between startDate and endDate

我尝试使用Restrictions.between但它无法识别current_date

Criteria c = session().createCriteria(Deal.class)
   .add(Restrictions.between("CURRENT_DATE", "startDate", "endDate");

1 个答案:

答案 0 :(得分:1)

只需使用UGLY java Calendar!

Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);

Date currentDate = c.getTime();

Criteria criteria = session.createCriteria(Deal.class)
.add(Restrictions.gt("startDate", currentDate))     
.add(Restrictions.lt("endDate", currentDate));