I have some query regarding hibernate,
Table : Employee_Master
Id Number Name Varchar Salary long
POJO: EmployeeMaster.java
public class EmployeeMaster {
private int id ;
private String name;
private long salary;
//... all field s getter/ setter methods
}
Now I want to get only name from such id.
SQL query like like:
select name from employee_master where id = 10;
But how can we achieve in hibernate the above same thing?
session.createQuery("from EmployeeMaster empMaster where empMaster.id = 10");
This solution I know but it will return whole pojo list. But I want only that field name so what should I do ?
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
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