5

this is my mysql query:

SELECT count(*) 
FROM bw_jobs 
WHERE year(job_date)=year(curdate()) AND month(job_date)=month(curdate());

How to use this in hibernate to get count value?

1
  • hi i got it . Tha answer is SessionFactory factory = HibernateUtil.getSessionFactory(); session = factory.openSession(); SQLQuery sqlQuery = session.createSQLQuery("SELECT count(*) as noofjobs FROM bw_jobs where year(job_date)=year(curdate()) and month(job_date)=month(curdate());"); sqlQuery.addScalar("noofjobs",Hibernate.INTEGER); Integer i=(Integer)sqlQuery.uniqueResult(); Commented May 1, 2011 at 13:20

1 Answer 1

18
String hql = "select count(job.id) from Job job"
             + " where year(job.jobDate) = year(current_date())"
             + " and month(job.jobDate) = month(current_date())";
Query query = session.createQuery(hql);
return ((Number) query.uniqueResult()).intValue();

(assuming the bw_jobs table is mapped by the Job entity, and the job_date column is mapped to the jobDate property)

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-expressions for the list of functions supported by Hibernate.

Sign up to request clarification or add additional context in comments.

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.