Vinod
Vinod

Reputation: 2311

Criteria query to select multiple columns in hibernate?

How can i select multiple columns from a table using criteria query.

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

I am able to get only testid but along with testid I need marks also Can anyone modify the below query to get both testid and marks.

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")));

Upvotes: 5

Views: 5467

Answers (1)

Francisco Spaeth
Francisco Spaeth

Reputation: 23893

You could try to give a ProjectionList to your 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));

Upvotes: 1

Related Questions