Search code examples
javahibernatehql

hql query to get last 1 month records


I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.

Here is Mysql query:

   SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM 
     F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN 
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()

Frankly, I don't have any idea about how to write above query in HQl. Can anyone please help me.


Solution

  • Could you try this, I couldn't test, if you it has a error please inform me;

    String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN  DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()"; 
    query = session.createSQLQuery(hqlQuery);