The Criteria API makes it easy to use restrictions in your queries to selectively retrieve objects; for instance, your application could retrieve only products with a price over $30. You may add these restrictions to a Criteria object with the add() method. The add() method takes an org. hibernate.
Restrictions with CriteriaCriteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.
The Criteria interface makes it easy to selectively fetch the data on the basis of conditions in the select query. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method.
I think you could do that with Projections, something like
Criteria.forClass(bob.class.getName())
.add(Restrictions.gt("id", 10))
.setProjection(Projections.property("id"))
);
Similarly you can also:
Criteria criteria = session.createCriteria(bob.class);
criteria.add(Expression.gt("id", 10));
criteria.setProjection(Projections.property("id"));
criteria.addOrder(Order.asc("id"));
return criteria.list();
or setProjection(Projections.id())
http://www.devarticles.com/c/a/Java/Hibernate-Criteria-Queries-in-Depth/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With