3

i have to select the record from table users_roles and i am using hibernate. but i don't want to to make the separate class in java for that , so

i am thinking of using plain sql . can i do it or i have to make the java class for that . because i i just to extract the role like

select role from user_roles where email = [email protected]

any eaxmple of that would be fine

2 Answers 2

10

Within Hibernate, you have to use the SQL-like langauge HQL (Hibernate Query Language). This would look something like:

From user_roles where email = [email protected]

And retrieve the whole mapped entity class for you.

edit: an alternative would be this: Using native queries in Hibernate

Sign up to request clarification or add additional context in comments.

3 Comments

the thing is i don't have any entity class for that . i only have database table for that . how can i do that
if i use native query to use select then how to execute that query . i mean query.executeupdate or executequery
0

You can either use native queries as posted by user538603 or create an entity for that table; the second way would probably be better in the mid/long term, because you would fully exploit Hibernate (criteria and everything...). I would suggest native queries only if you mostly need to write queries you can't do in HQL (like certain types of unions, specific database functions and some more use cases), but in that case I would suggest you to use www.mybatis.org, which is more suited for massive use of native sql queries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.