Hibernate使用oracle生成无效的sql

时间:2013-05-29 14:06:24

标签: java oracle hibernate jpa

我正在使用带有Oracle11g的hibernate 4.2.1。在NamedQuery的转换过程中,显然hibernate会生成无效的SQL语句。 NamedQuery看起来像这样:

SELECT retenues FROM EnsembleRetenuesPrestation retenues
WHERE EXISTS (SELECT calcul FROM CalculRetenueGlobale calcul
              WHERE calcul.id.payementPrestation.id = :payementId
              AND calcul.id.ensembleRetenuesPrestation.id = retenues.id)

CalculRetenueGlobale是一个连接表(带有一些额外的字段。所以它是一个真实的实体。)用于EnsembleRetenuesPrestation和PayementPrestation。它使用嵌入式密钥作为主键,包含两个实体CalculRetenueGlobale和PayementPrestation。

生成的SQL如下:

select
    ensemblere0_.ERP_ID_ENSEMBLE_RET_PREST as ERP1_24_,
    ensemblere0_.ERP_AAAAMM_ARRERAGES as ERP2_24_,
    ensemblere0_.ERP_CODE_CALCUL_SIMUL as ERP3_24_,
    ensemblere0_.ERP_ID_EXCEDENT_RETENUE as ERP19_24_,
    ensemblere0_.ERP_FLAG_5E_TRANCHE_CESSION as ERP4_24_,
    ensemblere0_.ERP_FLAG_ARRERAGES as ERP5_24_,
    ensemblere0_.ERP_MONTANT_APRES_RETENUES as ERP6_24_,
    ensemblere0_.ERP_MONTANT_AVANT_RETENUES as ERP7_24_,
    ensemblere0_.ERP_MONTANT_CESSIBLE as ERP8_24_,
    ensemblere0_.ERP_MONTANT_INCESS_INSAISISS as ERP9_24_,
    ensemblere0_.ERP_MONTANT_SAISISSABLE as ERP10_24_,
    ensemblere0_.ERP_RETENUE_ACOMPTE as ERP11_24_,
    ensemblere0_.ERP_RETENUE_ACOMPTE_FIXE as ERP12_24_,
    ensemblere0_.ERP_RETENUE_CONJOINT_TIERS as ERP13_24_,
    ensemblere0_.ERP_RETENUE_FAILLITE as ERP14_24_,
    ensemblere0_.ERP_RETENUE_SURENDETTEMENT as ERP15_24_,
    ensemblere0_.ERP_SOM_RET_ALIM as ERP16_24_,
    ensemblere0_.ERP_SOM_RET_CESSION as ERP17_24_,
    ensemblere0_.ERP_SOM_RET_SAISIES as ERP18_24_ 
from
    CCRSC.ENSEMBLE_RET_PREST ensemblere0_ 
where
    exists (
        select
            (calculrete1_.CAL_ID_CREANCE,
            calculrete1_.CAL_ID_ENSEMBLE_RET_PREST,
            calculrete1_.CAL_ID_PAIEMENT)
        from
            CCRSC.CALC_RET_GLOBALE calculrete1_ 
        where
            calculrete1_.CAL_ID_PAIEMENT=1
            and calculrete1_.CAL_ID_ENSEMBLE_RET_PREST=ensemblere0_.ERP_ID_ENSEMBLE_RET_PREST
    )

结果是ORA-00907:缺少右括号。如果我删除了这部分中的两个括号,我可以毫无问题地执行查询(例如在SQLDeveloper中):

select
    (calculrete1_.CAL_ID_CREANCE,
     calculrete1_.CAL_ID_ENSEMBLE_RET_PREST,
     calculrete1_.CAL_ID_PAIEMENT)

我只能找到一个旧问题(https://hibernate.atlassian.net/browse/HHH-2409),但在这种情况下,他们会责怪“AS”关键字,在这种情况下似乎不是问题。

有没有人遇到过类似的问题?这可能是一个映射问题吗?

提前致谢,

亚历

1 个答案:

答案 0 :(得分:3)

将字段列表放在括号中会对即使是微不足道的查询产生错误,因此Oracle不会错误地解析查询。

select (1,2,3) FROM DUAL

为旧版本的Hibernate建议的解决方法是返回EXISTS()中的字面值而不是字段。 (https://hibernate.atlassian.net/browse/HHH-2845

SELECT retenues FROM EnsembleRetenuesPrestation retenues
WHERE EXISTS (SELECT 1 FROM CalculRetenueGlobale calcul
              WHERE calcul.id.payementPrestation.id = :payementId
              AND calcul.id.ensembleRetenuesPrestation.id = retenues.id)

https://hibernate.atlassian.net/browse/HHH-5998

中也提到了这个问题