Hibernate子查询

时间:2017-08-02 11:09:52

标签: java hibernate hql nhibernate-criteria detachedcriteria

我遇到需要转换查询的情况: -

select hostname, avg(cpu_utilization_percentage) from cpu_utilization where timestamp In (select distinct(timestamp) from report.cpu_utilization order by timestamp desc limit 6) group by hostname

现在,我想使用hibernate获取这些数据,所以我使用了子查询: -

//对于内部查询

DetachedCriteria subquery = DetachedCriteria.forClass(CpuUtilizationDTO.class);
subquery.setProjection(Projections.distinct(Projections.property("timeStamp"))).addOrder(Order.desc("timeStamp"));
subquery.getExecutableCriteria(session).setMaxResults(6);

//对于外部查询

Criteria query = session.createCriteria(CpuUtilizationDTO.class);
ProjectionList list = Projections.projectionList();
        list.add(Projections.groupProperty("hostName"));
        list.add(Projections.avg("cpuUtilizationpercentage"));
        query.setProjection(list);
List<Object[]> obj= (List<Object[]>)hibernateTemplate.findByCriteria(query);ction(list);

//现在将子查询添加到主查询中我正在使用

query.add(Subqueries.propertyIn("timeStamp", subquery));

但每次我获得整个数据的平均值。任何人都可以帮忙,我错过了什么地方?

0 个答案:

没有答案