使用hibernate返回查询的计数值

Sur*_*non 4 hibernate

这是我的mysql查询:

SELECT count(*) 
FROM bw_jobs 
WHERE year(job_date)=year(curdate()) AND month(job_date)=month(curdate());
Run Code Online (Sandbox Code Playgroud)

如何在hibernate中使用它来获取计数值?

JB *_*zet 17

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();
Run Code Online (Sandbox Code Playgroud)

(假设bw_jobs表由Job实体映射,job_date列映射到jobDate属性)

有关Hibernate支持的函数列表,请参阅http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-expressions.