How do you select a column using Hibernate?

ThreaT picture ThreaT · May 18, 2012 · Viewed 61.7k times · Source

I would like to select a single column instead of a whole object, using Hibernate. So far I have this:

 List<String> firstname = null;

 firstname = getSession().createCriteria(People.class).list();

My problem is that the above code returns the whole People table as an object instead of just "firstname". I'm not sure how to specify to only return "firstname" instead of the whole object.

Answer

rizzz86 picture rizzz86 · May 18, 2012

You can set the Projection for this like:

.setProjection(Projections.property("firstname"))

With this you can only get the firstname in return.

I have found another link on stack with the same scenario. Hope this will also help How to use hibernate criteria to return only one element of an object instead the entire object?