-->
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.  [ 8 posts ] 
Author Message
 Post subject: SetValue
PostPosted: Thu Jul 10, 2008 8:36 am 
Newbie

Joined: Thu Jul 10, 2008 8:29 am
Posts: 5
I have this query :

ISQLQuery query = session.CreateSQLQuery("select * from tbl where someColumn=:someValue");

And I need to do the next :

query.SetInt32("someValue", null);

but I have Exception.

How can I set null to my parameter?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 9:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can't ... if your column can contain NULL values and you map it to a value type, you have to use a nullable (Int32?).
Actually with most (if not all) databases "column = null" wouldn't work. If you want to compare with null, you have to use "column is null".

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 10:36 am 
Newbie

Joined: Thu Jul 10, 2008 8:29 am
Posts: 5
Ok, I know this and I use Nullable type.
but it doesn't helps me.
For ex:
int? someValue;
someValue = null;

and then I cannot use :
query.SetInt32("someValue", someValue);
Because the second parametr has type int, so
I can use only this -

query.SetInt32("someValue", someValue.Value);

and this is not resolve of my problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 10:51 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Ok, I know this and I use Nullable type.


Sorry, wasn't clear from your post. Try

query.SetParameter("someValue", someValue);

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 5:04 am 
Newbie

Joined: Thu Jul 10, 2008 8:29 am
Posts: 5
thanks,
I'll try to use SetParameter.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 9:00 am 
Newbie

Joined: Thu Jul 10, 2008 8:29 am
Posts: 5
SetParameter doesn't work with Nullable types.
Who knows other variants?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 10:00 am 
Newbie

Joined: Thu Jul 10, 2008 8:29 am
Posts: 5
Thanks all.

I've done following:

int? val = ...;

if (val.HasValue
query.SetInt32("someValue", val.Value);
else
query.SetString("someValue", null);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 10:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Are you sure that you get the expected sql statement ? Does hibernate translate the null value to "somevalue is null" ? Afaik "somevalue = null" won't return anything ... maybe that depends on the DB, at least SQL server returns nothing at all ?

_________________
--Wolfgang


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