使用hibernate返回查询的计数值

时间:2011-05-01 13:14:57

标签: hibernate

这是我的mysql查询:

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

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

1 个答案:

答案 0 :(得分: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();

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

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