Rishabh
Rishabh

Reputation: 3852

Execute a query using Hibernate-Java

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??

Upvotes: 0

Views: 454

Answers (2)

Nayan Wadekar
Nayan Wadekar

Reputation: 11622

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).

Upvotes: 1

user3489875
user3489875

Reputation: 457

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

Upvotes: 2

Related Questions