2014-02-27 359 views
3

如何使用条件查询从表中选择多个列。准则查询选择休眠中的多个列?

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

你可以尝试给一个ProjectionListProjections.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));