Criteria查询在hibernate中选择多个列?

时间:2014-02-27 10:53:02

标签: spring hibernate criteria hibernate-criteria

如何使用条件查询从表中选择多个列。

Select testid,marks from user where id='uid' and name='uname'; 

我只能获得testid但是我同时需要testid标记也可以修改以下查询以获得testid和标记。

Session ses = sessionFactory.getCurentSession();
Criteria c = ses.createCriteria(user.class);
c.add(Restrictions.eq("id", uid));
c.add(Restrictions.eq("name", uname));
c.setProjection(Pojections.distinct(Projections.property("testid")));

1 个答案:

答案 0 :(得分:1)

您可以尝试向ProjectionList提供Projections.distinct

Session ses = sessionFactory.getCurentSession();
Criteria c = ses.createCriteria(user.class);
c.add(Restrictions.eq("id", uid));
c.add(Restrictions.eq("name", uname));

ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("testid"));
pl.add(Projections.property("marks"));

c.setProjection(Projections.distinct(pl));