Retrieve single field rather than whole pojo in hibernate

In HQL, you can simply ask for the one field:

String employeeName = session.createQuery("select empMaster.name from EmployeeMaster empMaster where empMaster.id = :id").setInteger("id",10).uniqueResult();

You need property projection http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-projection


This is working for me

String orderNo = (String) getCurrentSession().createQuery("select orderNumber from Asset where id = :id").setParameter("id", assetId).uniqueResult();

setInteger doesn't work for me then I used setParameter.

Tags:

Hibernate