How to execute normal SQL query in Hibernate without HQL?

巧了我就是萌 提交于 2019-12-04 05:48:55

问题


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.


回答1:


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




回答2:


With JPA you can user entityManager.createNativeQuery(...) with Hibernate there's session.createSQLQuery(...)



来源:https://stackoverflow.com/questions/24124351/how-to-execute-normal-sql-query-in-hibernate-without-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!