-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to insert null into database when int value is 0
PostPosted: Tue Sep 20, 2005 11:01 pm 
Newbie

Joined: Tue Sep 20, 2005 10:40 pm
Posts: 3
Hibernate version: 3.x

Name and version of the database you are using: Oracle 9i

I have a non-id property value stored in my Java object as int. I want to instruct Hibernate to insert null into the mapped database column when the value of the property is 0. Is there an easy way?

Thanks much,
Ted


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 12:50 am 
Newbie

Joined: Sun Sep 11, 2005 11:36 pm
Posts: 6
Hi
u can probably define in the .hbm.xml file the property not-null ="false"
for ex.
<property name="rawLocation" length="128" not-null="false"/>
and then manage the insersion from the java code by not entering anything when the value is zero.
is this help u.

Dont forget to rate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:12 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
Try to define the property with type="integer" and don't set this property when the value is 0.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 10:45 am 
Newbie

Joined: Tue Sep 20, 2005 10:40 pm
Posts: 3
I'm sorry, maybe I wasn't clear. Since the property is stored as an int, it is 0 by default. I want to continue to store the value as an int, but I want Hibernate to recognize that the value of 0 means that the property hasn't been set (to a non 0 value). Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 10:49 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
use a usertype. there are lots of examples on the hibernate website.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 11:06 am 
Newbie

Joined: Tue Sep 20, 2005 10:40 pm
Posts: 3
Yes, thank you, I know I could use a User Type. I just wondered if there was an easier way. It seems like it would be a common enough occurrence that there would be a 'null-for-zero' attribute that I was missing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 11:14 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
Quote:
It seems like it would be a common enough occurrence that there would be a 'null-for-zero' attribute that I was missing.


not that i'm aware of, but i could be wrong. usertypes are simple enough anyway... maybe there's already a 0 -> null one on the wiki. if not, you should post yours.


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