Thursday, September 01, 2016

How could I be so stupid using hibernate criteria with timestamp?

I needed to make a query to find all records valid today for columns with effectiveFrom and effectiveTo range
I spent a couple of hours trying to understand why

            criteria.add(Restrictions.ge("effectiveFrom",currentDate.getTime()));
            criteria.add(Restrictions.le("effectiveTo",currentDate.getTime()));

was giving no results.

After many trials and attempts with other criteria tricks, I remembered that hibernate is not comparting if  currentDate.getTime is greater than effectiveFrom, but if effectiveFrom is greater than currentDate.getTime
So correct sintax for it was the exact opposite

            criteria.add(Restrictions.le("effectiveFrom",currentDate.getTime()));
            criteria.add(Restrictions.ge("effectiveTo",currentDate.getTime()));

This one worked as it should 

No comments: