开发者

JPA 2 query giving strange behavior on OR clause

开发者 https://www.devze.com 2023-02-11 14:35 出处:网络
I have a JPA 2 query that is driving me nuts.A SurveyQuestion may have an Organization.Here\'s the mapping within SurveyQuestion

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号