-->
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.  [ 4 posts ] 
Author Message
 Post subject: passing nulls
PostPosted: Sat Aug 18, 2007 1:24 am 
Newbie

Joined: Sat Aug 18, 2007 1:12 am
Posts: 2
I have the following JPAQL query running against Postgresql


Code:
select i from Institution i where i.name=:name


if I pass a value for :name then the query behaves as expected. If I pass null, the query returns no results no matter what. This is because the native query generated is
Code:
.... where i.name=null
which is incorrect and returns no values. The correct native sql should be
Code:
.... where i.name is null
. Is there a way to tell hibernate or JPA to handle nulls with the is null language instead of passing a null value to =:value?

Without this functionality, I can't use named queries and have to build them dynamically which diminishes the value of JPA in my opinion. Is there a better solution to this than what I am doing?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 18, 2007 6:27 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Use below HQL query :

select i from Institution i where i.name=:name or i.name is null


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 18, 2007 6:31 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Or use below query :

if (nameParam != null) {
select i from Institution i where i.name=:name
} else {
select i from Institution i where i.name is null
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 18, 2007 8:15 am 
Newbie

Joined: Sat Aug 18, 2007 1:12 am
Posts: 2
thank you.

I already have the second one. The first one won't give me the desired results. I only want the null values if the parameter is in fact null.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.