Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria: NOW() < date + 1 day

I have a table in a MySQL DB with a date(DATETIME) column on it. How do I express it in java Hibernate criteria if let's say I would like to query for records where NOW() < ('date' + 1 day) ?

like image 236
Charlie Kee Avatar asked Mar 20 '13 16:03

Charlie Kee


1 Answers

You could turn it the other way around and compare 'date' >= (NOW - 1 day).

Assuming you've got a mapped MyTable class with the date property:

Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1); 
Criteria criteria = session.createCriteria(MyTable.class);
criteria.add(Restrictions.ge("date", c.getTime());
List results = criteria.list();
like image 106
Xavi López Avatar answered Oct 20 '22 00:10

Xavi López