I'm really new with Hibernate. I want a
List<User> using hibernate criteria, but only with fields User id and name filled up. Is that possible? Something like the query shown below:
SELECT user.id, user.name FROM user
This is exactly what projections are for. Here is an example:
In fact, if you look at the documentation for "lazy property fetching" they specifically say:
"A different (better?) way to avoid unnecessary column reads, at least for read-only transactions is to use the projection features of HQL or Criteria queries. This avoids the need for buildtime bytecode processing and is certainly a preferred solution."
By the way, there is a related question that you may also be interested in: http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections
I'm really late answering this, but, you can add a custom result transformer to the Query object like below.
Typically you don't want to partially load the properties of an object. But if you must, see this:
For plain reporting-like behaviour you could use entity queries: