Is it possible to execute big SQL query in Hibernate?

how to execute sql query in hibernate

  • 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

  • Answer:

    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: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html

John at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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.

Riccardo Cossu

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.