开发者

Using hibernate return count value of a query

开发者 https://www.devze.com 2023-03-02 03:01 出处:网络
this is my mysql query: SELECT count(*) FROM bw_jobs WHERE year(job_date)=year(curdate()) AND month(job_date)=month(curdate());

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 thi开发者_JAVA技巧s 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号