如何使用条件查询从表中选择多个列。
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")));
答案 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));