Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use hibernate criteria to return only one element of an object instead the entire object?

People also ask

Which criteria is used to selectively retrieve objects in hibernate?

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.

How do you put restrictions in criteria query?

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.

What is restriction in hibernate criteria?

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/