Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - filter Single Column data using date where clause

Lets say One table ABC having two column i.e. ID & CREATED_DATE

I want to fetch those ID which is created lets say '09-11-2017' and '09-17-2017'

Below SQL query working fine but I want to implement same logic using hibernate.

select ID from ABC where between TO_DATE('09-11-2017', 'MM-DD-YYYY')  AND TO_DATE('09-17-2017', 'MM-DD-YYYY')

My code is not working.

public List getData(final Date startDate, final Date endDate){

    String sqlString = "select ID from ABC where CREATED_DATE between :startDate and :endDate";
    SQLQuery query = getSession().createSQLQuery(sqlString);
    query.setParameter(CREATED_DATE);
    return query.list();
}
like image 539
SKP Avatar asked Dec 18 '25 20:12

SKP


2 Answers

You are missing End date parameter :

query.setParameter(CREATED_DATE, startDate);
query.setParameter(END_DATE, endDate);
like image 157
user7294900 Avatar answered Dec 21 '25 08:12

user7294900


It not work because you don't set the correct parameters :

String sqlString = "select ID from ABC where CREATED_DATE between :startDate and :endDate";
SQLQuery query = getSession().createSQLQuery(sqlString);
query.setParameter("startDate ", start_date);
query.setParameter("endDate", end_date);
return query.list();
like image 27
YCF_L Avatar answered Dec 21 '25 08:12

YCF_L