简体   繁体   English

如何在没有 HQL 的情况下在 Hibernate 中执行正常的 SQL 查询?

[英]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.我有一个非常复杂的 Join 查询来从 DB 中选择几个项目,并且它不涉及返回到该表所需的任何更新。 Which is why, I don't want to use the HQL (Hibernate Query Language, instead I want to execute as a simple SQL query.这就是为什么我不想使用 HQL(休眠查询语言,而是想作为简单的 SQL 查询执行。

Is that possible to execute a Normal SQL - Join query which involves 3 different tables in hibernate?是否可以在休眠中执行涉及 3 个不同表的普通 SQL-Join 查询? I use Java - Struts framework.我使用 Java - Struts 框架。

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.如果你说这是不可能的,那么我必须坚持使用 HQL,我会在这里发布查询,我需要你的帮助来写下基于 HQL 的类(表)创建和基于 HQL 的查询字符串。

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.另外,如果您能给我一个带有一对或 3 个表连接的示例查询以及如何设置 SQL 的参数。

from below line of code you can use any Query with hibernate从下面的代码行中,您可以将任何查询与休眠一起使用

its call Native SQL它的调用 Native SQL

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

for more help go here如需更多帮助,请访问此处

  • With JPA you can use entityManager.createNativeQuery(...)使用 JPA,您可以使用 entityManager.createNativeQuery(...)
  • With Hibernate there's session.createSQLQuery(...)使用 Hibernate 有 session.createSQLQuery(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM