JPA 2 query giving strange behavior on OR clause
I have a JPA 2 query that is driving me nuts. A SurveyQuestion may have an Organization. Here's the mapping within SurveyQuestion
@ManyToOne( optional=true )
@JoinColumn( name="organization_key" )
private Organization organization;
Organization has a field named key. I create a TypedNamedQuery with the following jpql
SELECT q from SurveyQuestion q
where q.organization IS NULL
or q.organization.key = :organizationKey
with that query I only get the questions that have the given key. If I remove the q.organization.key = :organizationKey from the query, then开发者_开发问答 I get all those that have no organization, but I can't for the life of me get it to return the combination of those that have no organization or have the given organization.
JPA2 with EclipseLink provider.
This is JPA standard.
q.organization.key
resolves to an inner join on q.organization, not to a left join!
You have to do this:
SELECT q
from SurveyQuestion q
left join q.organization o
where o is null OR o.key = :organizationKey
精彩评论