3

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();
}
0

3 Answers 3

3

You are missing End date parameter :

query.setParameter(CREATED_DATE, startDate);
query.setParameter(END_DATE, endDate);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @user7294900. Its really helpful.
3

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();

2 Comments

second parameter is endDate
Thank you @YCF_L. I just start read the online documents to learn more. You guys are rock
2

The parameters must match the string on the query

Try this:

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("startDate",startDate);
    query.setParameter("endDate",endDate);
    return query.list();
}

--

pscar13

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.