-->
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.  [ 6 posts ] 
Author Message
 Post subject: use of setInteger with createQuery
PostPosted: Mon Jun 27, 2005 4:09 pm 
Newbie

Joined: Mon Feb 09, 2004 11:14 am
Posts: 16
I have an attribute that is defined as Integer (we did this because the value could be null).

However, how can I get around the null pointer problem with the following:

Code:
Query query = session.createQuery(hql);         
query.setInteger("glUnit",glParameters.getGlUnit().intValue());


Any ideas...


Top
 Profile  
 
 Post subject: Re: use of setInteger with createQuery
PostPosted: Mon Jun 27, 2005 4:15 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
taa_sarge wrote:
I have an attribute that is defined as Integer (we did this because the value could be null).

However, how can I get around the null pointer problem with the following:

Code:
Query query = session.createQuery(hql);         
query.setInteger("glUnit",glParameters.getGlUnit().intValue());


Any ideas...


That's more of a standard Java question than a Hibernate question isn't it ?

Just check if the value is null before doing the setInteger(). If you need to be able to find objects where the GlUnit is null, you'd have to write a slightly different query anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 4:39 pm 
Newbie

Joined: Mon Feb 09, 2004 11:14 am
Posts: 16
Well yes... I simply wanted to use the same query (with or without a null value).

Was just hoping that there was a more elegant solution.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 4:58 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 5:59 pm
Posts: 34
Here's a somewhat pretty option:

Code:
Query query = session.createQuery(hql);

query.setInteger("glUnit", wrap(glParameters.getGlUnit()));
...
}

private int wrap(Integer value) {

  return value == null ? -1 : value.intValue();
}


Susan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 4:59 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
ExpeditorsSusanA wrote:
Here's a somewhat pretty option:

Code:
Query query = session.createQuery(hql);

query.setInteger("glUnit", wrap(glParameters.getGlUnit()));
...
}

private int wrap(Integer value) {

  return value == null ? -1 : value.intValue();
}


Susan



How does that work if you actually want to retrieve those that have a null value ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 5:45 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 5:59 pm
Posts: 34
Hmm, yeah, you're right... my bad. I wasn't really thinking... Just trying to make it prettier. I would probly make a method to do the addQuery part tho. But it is definitely a Java issue.


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