在DetachedCriteria上结合限制和预测

时间:2011-04-08 16:04:22

标签: hibernate criteria detachedcriteria

我在DetachedCriteria上组合限制和预测时遇到问题。 如果我单独使用限制或预测,程序可以正常工作。 我如何将这两者结合在一起?

我有以下sql:

 select cTopic.topic ,SUM(cTopic.records) as records 
 from Topics cTopic 
 where cTopic.id=? and cTopic.active =? 
 GROUP BY cTopic.topic 
 order by cTopic.orderTopics

以下Java代码可以正常使用而不使用group by(setProjection)

DetachedCriteria criteria = DetachedCriteria.forClass(Coursetopics.class);
criteria.add(Restrictions.eq("active","Y"));
criteria.add(Restrictions.eq("id.courseid",Id)));

List<Coursetopics> sumList =  (List<Coursetopics>) hibernateTemplate
                                      .findByCriteria(criteria);    

如果我在最后一行(List)之前添加以下代码行,则程序无法正常工作。

criteria.setProjection(Projections.projectionList()
                        .add( Projections.sum("records"))
                        .add( Projections.groupProperty("topic") ));

0 个答案:

没有答案
相关问题