2010-09-30 55 views
1

我想下面的HQL翻譯成標準的符號:如何在休眠條件中使用CURRENT_DATE函數?

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

只需使用UGLY Java日曆!

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));