nagkumar wrote:
Hi,
I have a user object, with name, loginid, password, zip and e-mail. When I like to validate the password, I would like User object to be retrived given the login id. However instead of getting the values of all fields of user object, I only want loginid and password of the user object to be selected rather than all other fields.
Is there a way to build such query.. i.e partially filled java objects.. for optimisation purpose.
Regards,
Nagendra
Hi,
You can use projection hql to achieve what you need. So, for instance, instead of executing:
“from User user where user.loginid=:loginid”
which will return a (possibly) single User object, you can use projection by executing:
“select user.loginid, user.password from User user where user.loginid=:loginid”
This will return a list (unique in your case for unique login ids) of object arrays just like in normal jdbc and you can use every array by casting to the appropriate class.
A more elegant and object oriented way, however, of manipulating the result set is to use dynamic instantiation. This is done by defining a transient and not mapped class encapsulating the fields you need. So, in your case you would need to create a PartialUser class holding loginid and password (with a related constructor also defined ) and use it like : “select new PartialUser(user.loginid, user.password) from User user where user.loginid=:loginid”.
Hope that helps,
mosa