Psyconn
Psyconn

Reputation: 127

How to use the CURRENT_DATE function in a hibernate criterion?

I want to translate the following HQL into Criteria notation:

from Deal
where CURRENT_DATE between startDate and endDate

I tried using Restrictions.between but it doesn't recognize current_date

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

Upvotes: 1

Views: 3173

Answers (1)

javagruffian
javagruffian

Reputation: 627

Just use the 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)); 

Upvotes: 1

Related Questions