-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: HQL query for "x is null or x.y = value"
PostPosted: Wed Feb 11, 2009 12:43 pm 
Newbie

Joined: Mon Dec 29, 2008 9:48 am
Posts: 6
Hi.
I'm trying to write an hql query that does something like this:
"from Data as data where (data.info is null or data.info.startDate > :timestamp)"

I am working with HSQLDB.
The problem is that the result ignores the cases in which data.info IS null.

I've noticed that the HQL is translated to SQL like this:
Code:
select
        data0_.DATA_ID as DATA1_32_,
        data0_.INFO_ID as INFO3_32_,
        data0_.name as name32_
    from
        DATA data0_,
        INFO info1_
    where
        data0_.INFO_ID=info1_.INFO_ID
        and (
            data0_.INFO_ID is null
            or info1_.startDate>?
        )


And it seems that the "data0_.INFO_ID=info1_.INFO_ID" part causes the problem.
In contrast, when I use the query
"from Data as data where (data.info is null)"
I get the following sql:
Code:
select
        data0_.DATA_ID as DATA1_32_,
        data0_.INFO_ID as INFO3_32_,
        data0_.name as name32_
    from
        DATA data0_
    where
        data0_.INFO_ID is null

Which does retrieve the null objects.

Sadly, I cannot use some patch like creating an empty "info" class for every "data" to avoid the problem.

Is there any way to write a single query in which the where clause is in the form "x is null or x.y = value" (or some workaround for the problem)?


Regards,
Oz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 2:26 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to specify an explicit (left) join. Something like the following should work:

Code:
from Data as data left join data.info as info
where (info is null or info.startDate > :timestamp)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 5:12 am 
Newbie

Joined: Mon Dec 29, 2008 9:48 am
Posts: 6
Thanks a lot nordborg, that solved the problem.

One small change though, I added a 'fetch' so that the query returns a list of Data objects (as before) and not a list of multiple objects (Data and Info):
"from Data as data left join fetch data.info as info where (info is null or info.startDate > :timestamp)"

Regards,
Oz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.