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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With