Hibernate - Criteria之后的Criteria之后的setProjection

时间:2010-07-16 15:51:50

标签: hibernate spring group-by criteria projection

为什么这不可能?

Criteria crit1 =  sess.createCriteria(Criteria1Class.class);
Criteria crit2 = crit1.createCriteria("criteria2Class");

crit2.setProjection(Projections.groupProperty("criteria2Property"));
List<String> l2 = crit2.list();

如果我使用这种结构,我会收到错误:

could not resolve property: criteria2Property of: com.foo.bar.models.Criteria1Class

offcourse criteria2Property在Criteria1Class中不存在,因为它存在于Criteria2Class

那么为什么groupProperty @manyToOne关联从Criteria1ClassCriteria2Class无法实现{{1}}?

1 个答案:

答案 0 :(得分:2)

自己解决了:

Criteria crit1 =  sess.createCriteria(Criteria1Class.class)
                  .createAlias("criteria2Class","criteria2ClassAlias")
                  .setProjection(Projections.groupProperty("criteria2ClassAlias.criteria2Property"));

crit1.list();