Vaibhav Katwate Vaibhav Katwate - 1 year ago 94
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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download