There is a class Offer
that has optional relationship to class Article
. So that some offers article property holds a null
value.
If i use the following statement, everything works fine. I got all offers, even those that have no article.
SELECT o FROM Offer o
LEFT OUTER JOIN o.article a
LEFT OUTER JOIN o.vendor v
WHERE v.number = '0212' OR a.nummer = '123456'
If i change the statement to:
SELECT o FROM Offer o
LEFT OUTER JOIN o.article a
LEFT OUTER JOIN o.vendor v
WHERE v.number = '0212' OR o.article.nummer = '123456'
I got only these offers having articles different from NULL
. That is because the notation for implicit joins (o.article.nummer
) forces an inner join.
Is there a possibility to force left outer joins to implicit joins (annotation driven or something else)? If there is a chance i could use a short form like this:
SELECT o FROM Offer o
WHERE v.number = '0212' OR o.article.nummer = '123456'
We can also join multiple entities in a single JPQL query:createQuery( "SELECT ph FROM Employee e JOIN e. department d JOIN e. phones ph WHERE d.name IS NOT NULL", Phone. class); List<Phone> resultList = query.
The only way to join two unrelated entities with JPA 2.1 and Hibernate versions older than 5.1, is to create a cross join and reduce the cartesian product in the WHERE statement. This is harder to read and does not support outer joins. Hibernate 5.1 introduced explicit joins on unrelated entities.
A left outer join is a method of combining tables. The result includes unmatched rows from only the table that is specified before the LEFT OUTER JOIN clause. If you are joining two tables and want the result set to include unmatched rows from only one table, use a LEFT OUTER JOIN clause or a RIGHT OUTER JOIN clause.
You can try putting @Fetch(FetchMode.JOIN)
on the Article property. This is a Hibernate annotation, however.
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
//...
@ManyToOne
@Fetch(FetchMode.JOIN)
Article article;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With