2014-02-27 283 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));