-->
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.  [ 2 posts ] 
Author Message
 Post subject: Assign null for a property with data type int
PostPosted: Tue Oct 14, 2008 11:44 am 
Newbie

Joined: Thu Jul 24, 2008 2:59 pm
Posts: 5
Hibernate version:3



I have a property called Count . I want to save anything that i get from the customer. So we are saving the properties in the database by having 2 fields called original Count and Count.


Original Count contains the data that the customer sent in the form of a string.

Count contains the data converted into integer format.

Mapping documents:
<property name="Cnt" type="int" column="CNT"/>

<property name="OrigCnt" type="String" column="ORIG_CNT"/>


java source . setters and getters:
Code:

int cnt;
String origCnt;

public int getCnt() {
      return Cnt;
   }

   public void setCnt(int Cnt) {
      this.Cnt = Cnt;
   }

   public String getOrigCnt() {
      return origCnt;
   }

   public void setOrigCnt(String origCnt) {
      this.origCnt = origCnt;
      try{
         setCnt(Integer.parseInt(origCnt));
      } catch (NumberFormatException nfe){
         
      }
   }



By doing this the Cnt field is always set as 0. i want to set it as null in the database since its a nullable field.

I know that if i change the cnt data type to Integer it will solve my problem but that will have an effect in n number of places in the existing code. Can we do something in the hibernate mapping?

I have the same scenario for amount field. i am using BigDecimal as the datatype for the amount so everything should work.

Any suggestions?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 9:40 am 
Newbie

Joined: Thu Sep 18, 2008 10:27 am
Posts: 4
Hi,

This is something that I have used for foreign key columns which are int type and can be null.

public void setCntRef(Integer Cnt) {
if (Cnt != null) {
this.Cnt = Cnt.intValue();
} else {
this.Cnt = 0;
}
}

public Integer getCntRef() {
return this.Cnt > 0 ? new Integer(this.Cnt) : null;
}


In your mapping file map this pseudo property cntRef to the database column.

See if that works. Also note that if you use JDK1.5 then you can make use of autoboxing and need not create the Integer object explicitly or call the .intValue().


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