HQL 子选择错误

Posted

技术标签:

【中文标题】HQL 子选择错误【英文标题】:Error on HQL subselect 【发布时间】:2017-09-01 18:12:39 【问题描述】:

我在 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】:

我看到你得到了ParticipanteProtocoloEntity 的计数。但它在我看来格式不正确:

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

在我看来像boolean statement &gt; 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

【讨论】:

以上是关于HQL 子选择错误的主要内容,如果未能解决你的问题,请参考以下文章

hibernate hql case when 子查询报java.lang.NullPointerException错误

HQL:选择语句以及使用“case when then”给出意外的令牌错误

Hibernate HQL:当且仅当所有子实体都具有相同值的属性时,如何选择父实体?

HQL选择组内具有最大值的实体的ID

休眠 HQL 连接错误

HQL - 删除并出现 JOIN 错误