0
select * from tableA where trunc(order_date) > sysdate -7;

Something like

Criteria criteria = getSession().createCriteria(TableA.class).
add(Restrictions.gt("orderDate", afterSubtracting7DaysFromToday));
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
kshah
  • 11
  • 2
  • 1
    `sysdate` is a proprietary feature of Oracle and hence, will not be available in Hibernate. Executing a native query will be one of the options. – Tiny Dec 27 '15 at 14:31
  • Thanks, but not just sysdate, but trunc function, how do i get it in the code? Sysdate anyways I handled using Date object. – kshah Dec 30 '15 at 14:35
  • As stated earlier, proprietary features are not supported by ORM frameworks though in case of functions, there are some common supported functions such as those group functions like `max()`, `min()`, `sum()`, `count()` and other common functions like `trim()`, `between()` etc which `trunc()` is not part of. – Tiny Dec 30 '15 at 14:51
  • Not sure about Hibernate criteria but in JPA criteria, you could use [`CriteriaBuilder#function(String name, Class type, Expression>... args)`](http://docs.oracle.com/javaee/7/api/javax/persistence/criteria/CriteriaBuilder.html#function-java.lang.String-java.lang.Class-javax.persistence.criteria.Expression...-) in order to create an expression to execute a native database function. You will find the equivalent to this in JPQL. – Tiny Dec 30 '15 at 14:53
  • Thank you so much for your time and guidance. – kshah Dec 30 '15 at 16:10

1 Answers1

0

Use sqlRestriction to execute sql database functions. Something like this:

Criteria criteria = getSession().createCriteria(TableA.class).
.add(Restrictions.sqlRestriction("trunc(order_date) > sysdate -7"));
CCC
  • 850
  • 7
  • 12