I've faced such an issue and dig tones of material. So, to avoid ugly iteration you can simply tune your hql:
You need to frame your query like this
select entity from Entity as entity where ...
Also check such case, it perfectly works for me:
public List<User> findByRole(String role) {
Query query = sessionFactory.getCurrentSession().createQuery("select user from User user join user.userRoles where role_name=:role_name");
query.setString("role_name", role);
@SuppressWarnings("unchecked")
List<User> users = (List<User>) query.list();
return users;
}
So here we are extracting object from query, not a bunch of fields. Also it's looks much more pretty.