JPA - 加入同桌

时间:2014-11-03 07:55:42

标签: java hibernate jpa

我收到了这个请求:

TypedQuery<ParamGenerauxExternes> q = entityMgr
            .createQuery("SELECT p FROM ParamGenerauxExternes p "
                    + "WHERE EXISTS "
                    + "(SELECT q FROM ParamGenerauxExternes q "
                    + "WHERE q.key.origine = :pOrigine "
                    + "AND q.key.typeParam LIKE :pTypeParametreBis "
                    + "AND p.key.sousType LIKE CONCAT('%',q.libelleParam) "
                    + "AND q.actif = 'Y') "
                    + "ORDER BY p.libelleParam", ParamGenerauxExternes.class)
            //.setParameter("pTypeParametre", "REL_TO_HOUSEHOLD")
            .setParameter("pOrigine", pOrigineGrc)
            .setParameter("pTypeParametreBis", "GRC_HOUSEHOLD_TYPE_P%");

但是它不起作用,我得到了这个错误信息,告诉假设缺失:

GRAVE: EJB Exception: : javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ORA-00907: missing right parenthesis

JPA生成的SQL在第二次选择后有一个括号:

select
    paramgener0_.CODE_PARAM as CODE1_2823_,
    paramgener0_.ORIGINE as ORIGINE2823_,
    paramgener0_.SOUS_TYPE as SOUS3_2823_,
    paramgener0_.TYPE_PARAM as TYPE4_2823_,
    paramgener0_.ACTIF as ACTIF2823_,
    paramgener0_.LIBELLE_PARAM as LIBELLE6_2823_ 
from
    FOA_PARAM_GEN_EXTERNE paramgener0_ 
where
    exists (
        select
            (paramgener1_.CODE_PARAM,
            paramgener1_.ORIGINE,
            paramgener1_.SOUS_TYPE,
            paramgener1_.TYPE_PARAM) 
        from
            FOA_PARAM_GEN_EXTERNE paramgener1_ 
        where
            paramgener1_.ORIGINE=? 
            and (
                paramgener1_.TYPE_PARAM like ?
            ) 
            and (
                paramgener0_.SOUS_TYPE like '%'||paramgener1_.LIBELLE_PARAM
            ) 
            and paramgener1_.ACTIF='Y'
    ) 
order by
    paramgener0_.LIBELLE_PARAM

我不知道为什么hibernate在第二个select之后添加一个括号?如果你知道该怎么做......

1 个答案:

答案 0 :(得分:0)

该查询在Oracle上失败,但传递给Postgres。如果您使用的是Oracle数据库,请尝试将Oracle SQL方言设置为JPA提供程序 Hibernate SQL Dialects