2

This JPQL query:

select p from Post p where p.status = 'public' or p.author.name = 'John'

Translates to a join between post and author, so it misses all records that are public (status = 'public') that has no author (author = null).

Any way to convert this to a left join, without adding an explicit left join clause to the "from" part?

I can't do an explicit left join on the "from" part like this:

select p from Post p left join p.author a where p.status = 'public' or a.name = 'John'

Because the code that generates the p.author.name = 'John' clause has no way to add a new relation to the "from" part.

So, this part is static, I can only append clauses to it:

select p from Post p where p.status = 'public'
Thiago Negri
  • 5,221
  • 2
  • 28
  • 39
  • you mean like explicitly saying "LEFT OUTER JOIN p.author" in the FROM part of the query? Any JPQL documentation would show how to do that. – Neil Stockton Jun 03 '16 at 06:47
  • I'm looking for a way to do a left join without saying it explicitly in the from part of the query. I've edited the question to clarify. – Thiago Negri Jun 03 '16 at 11:55
  • 1
    you cannot. That is what the FROM clause is for. Putting stuff in the WHERE will get what the JPA provider gives you .. – Neil Stockton Jun 03 '16 at 11:56
  • you might look at building your query using CriteriaBuilder rather then a JPQL string, as it is easier to build onto dynamically. – Chris Jun 03 '16 at 16:02
  • I can't use CriteriaBuilder, I'm typing this clause into `@AdditionalCriteria` form Eclipselink, it only accepts JPQL. – Thiago Negri Jun 04 '16 at 20:10

1 Answers1

0

This cannot be done in JPQL, and though you could use a subquery to avoid the join in the inner query, I don't know how it would work within @AdditionalCriteria.

If you need to add an outer join in additional criteria, you will need to use native EclipseLink API, setting an outerjoin expression through a descriptor customizer. This blog gives an example, while EclipseLink's expression framework does outer joins using the getAllowingNull as described in the documentation here

Chris
  • 20,138
  • 2
  • 29
  • 43