简体   繁体   English

如何将此SQL查询转换为Hibernate的HQL?

[英]How to translate this SQL query into Hibernate's HQL?

Having used Hibernate before but never to this extent, I have struggled while trying to translate the following query into a valid Hibernate SQL query. 以前曾经使用过Hibernate,但从未使用过Hibernate,在尝试将以下查询转换为有效的Hibernate SQL查询时,我一直很努力。

SELECT * FROM
(SELECT * FROM scripts WHERE (script.type LIKE 'DATA'))
AS A JOIN
(SELECT * FROM req_executions  
WHERE (req_executions.fk_database NOT IN 
(SELECT executions.fk_database FROM executions
WHERE executions.fk_script = req_executions.fk_script))
OR ((req_executions.fk_database = 12 AND req_executions.fk_database != 8) 
AND req_execution.fk_database NOT IN (SELECT executions.fk_database FROM execution
WHERE executions.fk_script = req_executions.fk_script)))
AS B
ON (A.id = B.fk_script)

The result of this query is a filtered table of scripts that are pending for execution. 该查询的结果是已过滤的待执行脚本的表。 I have tried things like: 我已经尝试过类似的事情:

"SELECT req FROM (SELECT s FROM Script s  
WHERE( s.filename LIKE :filename AND s.type = :scriptType AND s.ticketNumber LIKE :ticketNumber ) 
JOIN (SELECT re FROM RequiredExecution re 
WHERE (re.key.databaseId NOT IN (SELECT e.database FROM Execution e WHERE e.script.id = re.key.scriptId))
OR ((re.key.databaseId = :forAllId AND :dbId != :syncId)
AND re.key.databaseId NOT IN
(SELECT e.database FROM Execution e WHERE e.script.id = re.key.scriptId)))) WHERE (s.id = re.key.scriptId)"

and using "AS" but I am still far from a solution. 并使用“ AS”,但距离解决方案还差得远。 Any tips? 有小费吗?

I thought you had to select FROM an entity and not from another SELECT ... I don't know if it is possible, seems tough anyway. 我以为您必须从一个实体中进行选择,而不是从另一个SELECT中进行选择……我不知道是否可行,无论如何似乎都很困难。

You might consider sticking with native SQL query calling session.createSQLQuery("the native SQL") . 您可能考虑坚持使用本机SQL查询调用session.createSQLQuery("the native SQL") and .addEntity(YourEntityClass.class) to auto map results and .setParameter( , 0 based numbered, while the query uses ? placeholders instead of :named_parameter. .addEntity(YourEntityClass.class)自动映射结果,并为.setParameter( ,从0开始的数字编号),而查询使用?占位符而不是:named_pa​​rameter。

In the end I was forced to use a nativeQuery instead, because "...HQL subqueries can occur only in the select or where clauses." 最后,我不得不使用nativeQuery,因为“ ... HQL子查询只能在select或where子句中发生。” as stated in 14.13. 14.13所述。 Subqueries . 子查询

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

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