Gersh
Gersh

Reputation: 25

How to execute normal SQL query in Hibernate without HQL?

I have a pretty complex Join query to select few items from DB, and it doesn't involve any Update required back to this table. Which is why, I don't want to use the HQL (Hibernate Query Language, instead I want to execute as a simple SQL query.

Is that possible to execute a Normal SQL - Join query which involves 3 different tables in hibernate? I use Java - Struts framework.

If you say it is not possible then I have to stick to HQL and I would post here the query for which I would need your help in writing down HQL based class(tables) creations and HQL based Query string.

Also, if you could please give me an example query with couple or 3 table Joins and how to set the Parameters for the SQL.

Upvotes: 0

Views: 8790

Answers (2)

Nicola Baiocco
Nicola Baiocco

Reputation: 156

  • With JPA you can use entityManager.createNativeQuery(...)
  • With Hibernate there's session.createSQLQuery(...)

Upvotes: 1

Nirav Prajapati
Nirav Prajapati

Reputation: 3005

from below line of code you can use any Query with hibernate

its call Native SQL

session.createSQLQuery("SELECT * FROM table as a join table1 as b  on a.id = b.id ").list();

for more help go here

Upvotes: 2

Related Questions