2

Suppose you have EntityA and EntityB. EntityB has an attribute of type EntityA call it "ea".

So you you want to retrieve all EntityAs (related or not with entityB) you could do this way:

select ea, b
from EntityB b
right join b.ea ea

In this way you will retrieve entityA that are related to entityB or entityA that are not related to entityB (ea == null).

In JPA 2.1 right join is not supported. Is there a way to rewrite the query without changing the side of the relation ? (Our business analysts don't want to change it).

1 Answers1

1

For completeness: right join is supported optionally in JPA (at least according to the JoinType documentation), which means, you should check your JPA provider (EclipseLink).

If there is a reverse side of the relationship from EntityA to EntityB, then you can use a LEFT JOIN:

SELECT ea, eb
FROM EntityA ea
LEFT JOIN ea.eb eb

If you do not have the reverse side (and you can't add it), you native queries (basically SQL).

V G
  • 18,822
  • 6
  • 51
  • 89