LynAs LynAs - 1 year ago 112
Java Question

Hibernate get all transaction of current year

Suppose I have a class TransactionDetails. This table holds transaction of last 10 year. Now How can i get all TransactionDetails of current year

I am using criteria as following

Criteria criteria = session.getCurrentSession().createCriteria(TransactionDetails.class);
criteria.add(Restrictions.eq("entryDate", thisYear));
return (List<TransactionDetails>) criteria.list();

I know I can achieve this by detecting beginning of year and end of year and the do a query with between operator. But I am looking for a way do this in one line. e.g. like in sql we use

How can this be done??

Answer Source

Try Restrictions.between("entryDate", loDate, highDate)


You could also probably use a (vendor specific) sql restriction.

eg: Restrictions.sqlRestriction("to_char(entry_date, 'YYYY') = ?", "2015", StringType.INSTANCE)

Note: This will likely perform worse than between

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download