Search code examples
hibernatejpahqlnamed-query

Hibernate HQL exception with multiple INNER JOINs


I have 3 tables representing 3 entities like this :

Service class :

Service
----------
- Long id (primary key)
- Collection<commandeLine> commandeLines (many to many)
- Date billingDate;

CommandLine class :

CommandLine
--------------
- Long id (primary key)
- Command (many to one)
- Collection<Service> services (many to many)

Command class :

Command
-------------
- Long id (primary key)
- Date date
- Collection<CommandLine> commandLines (one to many)

Now I have a named query on Service.java, that will return all services that have a billing date > to the date of at least one of the commands they are attached to. So (I guess) I have to use 2 inner joins to use CommandLine class that has an actual link to the Command class.

What I did with NamedQueries is :

@NamedQuery(name = "ServiceDateQuery", query = "SELECT s FROM Service s "
        + "INNER JOIN Command c WITH c.date > s.billingDate "
        + "INNER JOIN s.commandLines cl WITH cl.command = c.id") })

I get an error :

org.hibernate.hql.ast.QuerySyntaxException: Path expected for join!

I guess it doesn't like the fact that I don't link to Command from another entity. But what can I do? if I put s.CommandLines first, I won't be able to perform the query either.

Any help please?


Solution

  • not WITH, but WHERE and at the end of the query, join in jpa are resolved automatically from the class model:

    SELECT s FROM Service s INNER JOIN s.comandelines cl INNER JOIN cl.comand c
    WHERE c.date > s.billingDate