4

In MySQL it works:

SELECT * FROM carparks a 
LEFT JOIN  (SELECT * FROM locales_carparks) 
c ON a.carpark_id=c.carpark_id

Hot to translate it to JPA:

@Query("SELECT a FROM Carparks a LEFT JOIN("
            +"SELECT b FROM a.locales b"
            +")")

IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 72 [SELECT a FROM database.model.carpark.Carparks a LEFT JOIN(SELECT b FROM a.locales b)]

I've simplified example to show the essence of the problem. Normally I use justSELECT a FROM Carparks a LEFT JOIN a.locales and it works, but in my case I want to use nested SELECT because my query is much more complex

Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51

2 Answers2

5

You could use a simple alternative

create view v_carparks as
    SELECT * FROM carparks a 
    LEFT JOIN  (SELECT * FROM locales_carparks) 
    c ON a.carpark_id=c.carpark_id

And use it for the query

@Query("SELECT a FROM v_carparks")

Especially if the query is complicated, this would be cleaner to have a huge query in a view to hide that complexity.

EDIT :

You can't used a nested query for join. This is written in the HQL documentation like this :

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

This could be explain for the mapping system. Hard to do the mapping with a subqueries result.

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

You can write it like this

@Query("SELECT a FROM Carparks a LEFT JOIN Locales b on a.carpark_id = b.carpark_id")
BOUALI ALI
  • 230
  • 2
  • 14
  • thanks, I normally use SELECT a FROM Carparks a LEFT JOIN a.locales and it works, but in my case I wanted to use nested SELECT because my query is much more complex, I am gonna update question to clarify it – Kamil Nękanowicz Dec 02 '16 at 09:42