加入条件与组

时间:2012-03-27 23:40:12

标签: hibernate hibernate-criteria

如何编写后续SQL

的标准
SELECT
m.match_type, 
count(m.match_type)
cat.description, 
loc.oid

FROM
USER_MATCH m, 
RECORD rec, 
CATEGORY cat, 
LOCATION loc

WHERE
m.record_oid = rec.oid and 
rec.category_oid = cat.oid and 
rec.location_oid = loc.oid
group by m.match_type, cat.description, loc.oid

我已尝试过以下但我得到了 这里不允许使用子查询表达式错误。

Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Match.Class);
criteria.createAlias("record", "record");
criteria.createAlias("category", "category");

ProjectionList projList = Projections.projectionList();        
projList.add(Projections.groupProperty("record.categoryId"));  
projList.add(Projections.groupProperty("record.locationId")); 
criteria.setProjection(projList);

提前感谢:)

1 个答案:

答案 0 :(得分:0)

这会对你有所帮助

Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Match.Class);
criteria.createAlias("record", "record");
criteria.createAlias("category", "category");
criteria.createAlias("location", "location");

ProjectionList projList = Projections.projectionList();  
projList.add(Projections.groupProperty("match_type"));      
projList.add(Projections.count("match_type");
projList.add(Projections.groupProperty("record.categoryId"));  
projList.add(Projections.groupProperty("record.locationId")); 
criteria.setProjection(projList);

注意您不需要在hibernate中包含连接条件,因为它会被CreateALias自动处理。