Error on HQL subselect

83 Views Asked by At

I'm running this query with a subselect inside WHERE clause

    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

But i receive this error:

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

When i remove the subselect the query works normally.

I cannot discover what is wrong with this part:

(
    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

There are 1 best solutions below

0
On BEST ANSWER

I see that you are getting a count of ParticipanteProtocoloEntity. But it looks malformed to me:

(UPPER(pp.nmParticipante) LIKE :nmParticipante AND :nmParticipante IS NOT NULL) > 0

looks like {boolean statement} > 0 to me. Im guessing you wanted the count(*) to be greater than zero:

 (
 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