如何将SQL转换为具有联合和内部联接的HQL?

时间:2019-05-06 07:13:20

标签: java sql hql

我希望将此普通SQL查询转换为HQL 。我有一个想法,如何将SQL查询转换为HQL查询,但是我的查询包含 UNION ,然后是内部联接。因此,为我进行转换变得很棘手。

select count(distinct T.id_veicolo) from 
((select distinct C.id_veicolo from (
(select distinct id_veicolo from movement
where id_sede_uscita = 23
and annullato <> 'true' 
and (((inizio >= '2019-01-05' and inizio <= '2019-01-06') and fine >= '2019-01-06') 
or (inizio <= '2019-01-05' and (fine <= '2019-01-06' and fine >= '2019-01-05')) 
or (inizio <= '2019-01-05' and fine >= '2019-01-06') or (inizio >= '2019-01-05' and fine <= '2019-01-06'))) 
UNION 
(select id_veicolo from freeVehicle where id_sede = 23 and inizio <= '2019-01-05' and fine >= '2019-01-06') ) as C) as D 
inner join (select id from parco_veicoli where targa = 'XXX') as R on D.id_veicolo = R.id) as T

问题点

如果此查询中仅存在 UNION ,那么我可以将该查询转换为2个子查询,但是我的问题是在 UNION 之后,我们再次使用内部联接。这是我的主要问题所在。

我的要求

我希望从HQL查询获得的结果与从此SQL查询获得的结果相同。我不想使用SQL查询,因为 SQL查询不支持缓存

1 个答案:

答案 0 :(得分:1)

如果您需要与内部联接进行严格联合,那么最好使用本机sql。结果集可以转换为java类中的POJO对象,但是您将利用本机查询执行的速度,并且只需单击一个db