0

I have a query

select userid from userinfo where DATEDIFF(ExpiryDate,curdate())<100;

how can this query be executed using hibernate??

I have tried this

List<String> usernames = (List<String>)(getSession().createQuery("select userid from userinfo where DATEDIFF(ExpiryDate,CURRENT_DATE)<100;").list());
     for (Iterator iterator = usernames.iterator(); iterator.hasNext();){
      String username= (String) iterator.next();
      System.out.println(username);
     }

But this does'nt return any result is there any other way to do this??

4
  • change DATEDIFF(ExpiryDate,CURRENT_DATE) to DATEDIFF(ExpiryDate,now()) and see if this works Commented Apr 9, 2014 at 10:57
  • @AbhikChakraborty:no, that isn't working Commented Apr 9, 2014 at 11:23
  • are u able to get the query and then try it directly on mysql..since if the query is getting sent as select userid from userinfo where DATEDIFF(ExpiryDate,now()) < 100 ; it should work. Commented Apr 9, 2014 at 11:28
  • @AbhikChakraborty: in mysql it is working fine, but here it is not returning any result Commented Apr 9, 2014 at 12:21

2 Answers 2

2

If it's in the native SQL dialect of your database, then you have to use this:

List<String> usernames = (List<String>)(getSession().createSQLQuery("yourNativeSqlQuery").list());
Sign up to request clarification or add additional context in comments.

1 Comment

first of all thanks as this does the job , but something native HQL would have been awesome. thanks once again
1

DATEDIFF is MySQL specific keyword, not valid in HQL.

SELECT u.userid FROM userinfo u WHERE u.expiryDate BETWEEN CURRENT_DATE() AND :end_date

Try executing above query & set parameter end_date as (current_date+100).

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.