使用子查询中的限制将sql转换为hql

时间:2013-10-02 19:37:15

标签: hibernate hql

我有以下sql,但目前无法转换此。 我刚停在subselect的极限。任何人都可以帮助我吗?

select( select p2.numnfe from xml_itens as p2
where p2.cod_prod_cli is null
and p2.ean_prod = p1.ean_prod
and p2.cnpj_for = p1.cnpj_for
and p2.cod_prod_for = p1.cod_prod_for
and p2.cfop != 5929 and 6929
LIMIT 1
) as num_nfe,
(SELECT descricao_prod from xml_itens as p2
where p2.cod_prod_cli is null
and p2.ean_prod = p1.ean_prod
and p2.cnpj_for = p1.cnpj_for
and p2.cod_prod_for = p1.cod_prod_for
and p2.cfop != 5929 and 6929
LIMIT 1
) as descricao_prod, 
cnpj_for, ean_prod, cod_prod_for, cod_prod_cli
from xml_itens as p1 
where cod_prod_cli is null
and p1.cfop != 5929 and 6929 
group by cnpj_for, ean_prod, cod_prod_for;

我设法做到了,但是及时发现只从数据库中返回一条记录。

 String sql = "select ("
            + " select p2.numnfe from XmlItens p2 where p2.codProdCli is null and p2.eanProd = p1.eanProd and p2.cnpjFor = p1.cnpjFor and p2.cfop != 5929 and p2.cfop != 6929) as numnfe, "
            + " p1.cnpjFor, p1.eanProd, p1.codProdFor from XmlItens p1 "
            + " where p1.codProdCli is null "
            + " group by p1.cnpjFor, p1.eanProd, p1.codProdFor";

由于

2 个答案:

答案 0 :(得分:1)

我找不到任何替代LIMIT sql的东西。因此,我使用函数createsqlquery() hibernate而不是转换查询,而是工作。

答案 1 :(得分:0)

无论hibernate团队在这一变化背后的推理如何,我都无法开始告诉你它是多么令人沮丧。

话虽如此,请使用EXIST关键字获取创意。在这个例子中,我们正在寻找限制1.选择TOP 1等.EXIST可能是你唯一的非原生解决方案。