这是我的mysql查询:
SELECT count(*)
FROM bw_jobs
WHERE year(job_date)=year(curdate()) AND month(job_date)=month(curdate());
如何在休眠中使用它来获取计数值?
答案 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。