Monday, 25 April 2016

How to select multiple columns using Criteria API ?


Set Multiple projections
ProjectionList p1 = Projections.projectionList();
p1.add(<projection1>);
p1.add(<Projection2>);
c.setProjection(p1);


Example : Select names and address who are from Agra having age more than 60 in order of Name



Crietria c = session.createCriteria(Person.class);
c.add( Restrictions.like("city","AGRA") );
c.add( Restrictions.gt("age",60) );
c.addOrder( Order.asc("name") );

ProjectionList p1 = Projections.projectionList();
p1.add(Projections.property("name"));
p1.add(Projections.property("address"));
c.setProjection(p1);


List result = c.list();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.