You can use multiselect
function for this.
CriteriaBuilder cb=session.getCriteriaBuilder();
CriteriaQuery<Object[]> cquery=cb.createQuery(Object[].class);
Root<Car> root=cquery.from(User.class);
cquery.multiselect(root.get("id"),root.get("Name"));
Query<Object[]> q=session.createQuery(cquery);
List<Object[]> list=q.getResultList();
System.out.println("id Name");
for (Object[] objects : list) {
System.out.println(objects[0]+" "+objects[1]);
}
This is supported by hibernate 5. createCriteria
is deprecated in further version of hibernate. So you can use criteria builder
instead.