24

We're using JPA with hibernate as the provider, we have a query that contains a join with a subquery in the FROM clause, but we get the following error:

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 75 [SELECT sd FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates sd, (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates x WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state GROUP BY x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state = :state AND sd.changeDate = sd2.maxChangeDate]

This is the query:

SELECT sd 
FROM SnapshotDates sd, 
     (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state 
      FROM SnapshotDates x
     WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state
GROUP BY x.viewId, x.state) sd2
WHERE sd.viewId = sd2.viewId 
      AND sd.state = :state 
      AND sd.changeDate = sd2.maxChangeDate

Thank you for helping

gads
  • 436
  • 1
  • 5
  • 10
  • jpa does not support subquery in 'FROM' and "SELECT' clauses, you have to change your query and put subquery in the 'WHERE' clause. – Omid Rostami Aug 11 '18 at 03:31

3 Answers3

28

I did not think HQL could do subqueries in the from clause

https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-subqueries

note the sentence:

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

I imagine you could change it to a native query and execute it that way.

Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
dispake
  • 3,259
  • 2
  • 19
  • 22
4

Your SQL is:

SELECT sd FROM SnapshotDates sd, (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state FROM SnapshotDates x WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state GROUP BY x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state = :state AND sd.changeDate = sd2.maxChangeDate

You can rewrite your sql like

SELECT sd 
FROM SnapshotDates sd, 
WHERE sd.viewId in (:viewIds)
    AND sd.state = :state
    sd.changeDate = (SELECT max(x.changeDate) FROM SnapshotDates x WHERE x.viewId = ds.viewId AND x.state = ds.state)

Find inspired by example

SELECT m FROM Professor m WHERE (SELECT COUNT(e) FROM Professor e WHERE e.manager = m) > 0

http://www.java2s.com/Code/Java/JPA/EJBQLWhereClauseWithSubQuery.htm

My similar example I had SQL

select k.* from kredits k, 
  (select client_id, max(r_date) r_date from kredits k group by client_id) k2 
where k.client_id = k2.client_id 
    AND k.r_date = k2.r_date 
order by k.id

Rewrite it for PQL

select k From Kredit k
where k.rDate = (select MAX(k2.rDate) from Kredit k2 where k2.clientId = k.clientId)
order by k.id

It will be translated to

select kredit0_.id as id28_, kredit0_.client_id as client59_28_ from kredits kredit0_ 
where kredit0_.r_date=(select MAX(kredit1_.r_date) from kredits kredit1_ where kredit1_.client_id=kredit0_.client_id) 
order by kredit0_.id

return same result as SQL.

Use Hebirnate 3.3.1 with MySQL 5.0.24

Joter
  • 316
  • 2
  • 6
  • Please help me in translation of query: `select t.order_state,t.claim_number,t.id from (select * from t_repair_order where claim_number='SERCON201465') t where t.order_state = 'Cancel' and not exists ( select 1 from (select * from t_repair_order where claim_number='SERCON201465') tt where tt.id > t.id and tt.order_state <> t.order_state );` – ankit Dec 20 '19 at 14:16
2

Subqueries (including lateral subqueries) in the from-clause of HQL and Criteria queries are supported from Hibernate 6.1 Final

https://in.relation.to/2022/06/14/orm-61-final/

SANN3
  • 9,459
  • 6
  • 61
  • 97