unexpected token : ( subquery hql

2019-08-10 06:10发布

问题:

Here's my HQL query

FROM com.mysite.ActeurInterne act WHERE act.acteurId IN
(SELECT DISTINCT COALESCE(acteurInterne.acteurInternePrincipalId, acteurInterne.acteurId)
FROM
  (SELECT DISTINCT acteurInterne
  FROM com.mysite.ActeurInterne AS acteurInterne
  JOIN acteurInterne.roleSet.roles                                     AS role
  WHERE acteurInterne.acteurId = acteurInterne.acteurId
  AND acteurInterne.nom LIKE :likenom
  AND (role.dateFermeture IS NULL
  OR role.dateFermeture   >= TRUNC(SYSDATE))
  AND (role.dateOuverture IS NULL
  OR role.dateOuverture   <= TRUNC(SYSDATE))
  AND (role.type           = :type
  OR role.type             = :typeC)
  )
)

I get

 org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 190 

which is the "(" at the begining of the fourth line above.

( SELECT DISTINCT acteurInterne

回答1:

Hibernate documentation states that sub-queries are allowed only in the SELECT or WHERE clause.

Note that HQL subqueries can occur only in the select or where clauses.

But in the example above you have a subquery in the FROM clause of the first subquery.

Have you tried consolidating the 2 sub-queries into one?

FROM com.mysite.ActeurInterne act WHERE act.acteurId IN
(SELECT DISTINCT COALESCE(acteurInterne.acteurInternePrincipalId, acteurInterne.acteurId)
  FROM com.mysite.ActeurInterne AS acteurInterne
  JOIN acteurInterne.roleSet.roles                                     AS role
  WHERE acteurInterne.acteurId = acteurInterne.acteurId
  AND acteurInterne.nom LIKE :likenom
  AND (role.dateFermeture IS NULL
  OR role.dateFermeture   >= TRUNC(SYSDATE))
  AND (role.dateOuverture IS NULL
  OR role.dateOuverture   <= TRUNC(SYSDATE))
  AND (role.type           = :type
  OR role.type             = :typeC)
)