使用查询的休眠返回计数值 [英] Using hibernate return count value of a query

查看:64
本文介绍了使用查询的休眠返回计数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的mysql查询:

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();

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

(assuming the bw_jobs table is mapped by the Job entity, and the job_date column is mapped to the jobDate property)

请参见 http://docs .jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-expressions 以获得Hibernate支持的功能列表.

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-expressions for the list of functions supported by Hibernate.

这篇关于使用查询的休眠返回计数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆