Vaibhav Katwate Vaibhav Katwate - 1 month ago 6
Java Question

Can any one help me in writing criteria query of hibernate for this query

This is my query:

select id from role_sub_module_mapping where role_id='165';


I have tried this but don't get the desired output.

Criteria cr = super.getSession().createCriteria(RoleSubModule.class);
cr.add(Restrictions.eq("roleId", '165'));

Answer

Your Criteria query

cr.add(Restrictions.eq("roleId", '165'));

is almost correct. It now returns a list of RoleSubModule instances with the roleId 165.

In order to get the Id instead, you need to use a projection onto the Id property:

cr.setProjection(Projections.property("id");