I have two tables: 'publication' and 'publication_translations'.
In hibernate I use an annotation to map the translations to the publication object with a @ManyToOne.
In the DAO layer I use a query to get all the publications like this:
from publication p where application_id = :applicationId
Now I want to fetch join the translations into the application object like:
from publication p left outer join fetch p.translations pt where application_id = :applicationId
This results in a SQL query like this:
select
publicatio0_.id as id25_0_,
translatio1_.id as id26_1_,
publicatio0_.application_id as applica17_25_0_,
publicatio0_.url as url25_0_,
translatio1_.language as language26_1_,
translatio1_.name as name26_1_,
translatio1_.publication_id as publicat4_26_1_,
translatio1_.id as id0__
from
publication publicatio0_
left outer join
publication_translation translatio1_
on publicatio0_.id=translatio1_.id
where
and application_id=180;
When I run this query directly in MySQL it returns rows as I expect. But somehow the List returned by the session.list() stays empty.
It seems like Hibernate throws away the rows when the translation is not existent. (as you would expect with an inner join, but not a left outer join..
Why does this happen? Am I missing something?
Btw. the reason i'm doing this is because I want to add a where or with to the hql clause stating a specific language, so you can fetch the translations for a specific language. But I simplified the case to illustrate the problem. The end result will be something like:
from publication p join fetch p.translations pt where (pt.language = :language or pt.language is null) and application_id = :applicationId
Publication.java:
@OneToMany(mappedBy = "publication")
private List<PublicationTranslation> translations;
PublicationTranslation.java
@ManyToOne
private Publication publication;
Edit: added the annotations on the classes: Edit2: changed mistake in the annotation on Publication.java