HQL子选择出错

时间:2017-09-01 18:12:40

标签: java hibernate jpa named-query

我在WHERE子句

中使用子选择运行此查询
    select  prot.id as id,
            prot.nrProtocolo as nrProtocolo,
            prot.nrAno as nrAno,
            prot.cdSituacaoProtocolo as cdSituacaoProtocolo,
            prot.palavraChave as palavraChave,
            prot.dsObs as dsObs,
            prot.dataCriacao as dataCriacao,
            partAtual as participanteAtual,
            assunto.id as assunto_id,
            assunto.nmAssunto as assunto_nmAssunto,
            tema.id as assunto_tema_id,
            tema.nmTema as assunto_tema_nome
    from Evento evt
    inner join evt.protocolo prot
    left outer join prot.assunto assunto
    left outer join assunto.tema tema
    inner join prot.participanteAtual partAtual
    where (
            (prot.participanteSubscritor.id = :participanteId and :participanteId is not null) or
            (upper(prot.nmSubscritor) like :nmParticipante and :nmParticipante is not null ) or
            (prot.participanteEmissor.id = :participanteId and :participanteId is not null) or
            (upper(prot.nmEmissor) like :nmParticipante and :nmParticipante is not null ) or
            (
                select count(*) from ParticipanteProtocoloEntity pp where pp.protocolo.id = prot.id and
                ( 
                    (pp.participante.id = :participanteId and :participanteId is not null) or
                    (upper(pp.nmParticipante) like :nmParticipante and :nmParticipante is not null) > 0
                )
            )
          )
          and trunc(prot.dataCriacao) >= trunc(:periodoInicial) and trunc(prot.dataCriacao) <= trunc(:periodoFinal)
          and prot.cdSituacaoProtocolo <> 4
          and prot.cdSituacaoProtocolo <> 8
          and (prot.snExcluido is null or prot.snExcluido != 'S')
    order by prot.dataCriacao desc, prot.nrProtocolo asc

但是我收到了这个错误:

Error in named query: 
Protocolo.recuperaListaProtocoloPorEncaminhadoParticipanteTrans: 
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: query

当我删除子选择时,查询正常工作。

我无法发现这部分有什么问题:

(
    select count(*) from ParticipanteProtocoloEntity pp where pp.protocolo.id = prot.id and
                ( 
                    (pp.participante.id = :participanteId and :participanteId is not null) or
                    (upper(pp.nmParticipante) like :nmParticipante and :nmParticipante is not null) > 0
                )
)

1 个答案:

答案 0 :(得分:0)

我看到你的计数为ParticipanteProtocoloEntity。但它对我来说看起来很不对劲:

(UPPER(pp.nmParticipante) LIKE :nmParticipante AND :nmParticipante IS NOT NULL) > 0
对我来说,

看起来像{boolean statement} > 0。我想你希望count(*)大于零:

 (
 SELECT
     COUNT(*)
 FROM
     ParticipanteProtocoloEntity pp
 WHERE
     pp.protocolo.id = prot.id
 AND ((
             pp.participante.id = :participanteId
         AND :participanteId IS NOT NULL)
     OR  (
             UPPER(pp.nmParticipante) LIKE :nmParticipante
         AND :nmParticipante IS NOT NULL ))) > 0