• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoid setting default values as 0 for fields of type long

 
Amit Goda
Ranch Hand
Posts: 37
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone.

How to avoid sending a default 0 value for a field of type long ?

The problem is, i am sending an object to the web service which in turn will make an insert call to store the values of object. In the object some fields data type is long. When passing the Object via client classes.
In our DB there are some fields which will not except 0 values.

So when we don't set any values for a field that has type long, java by default will set 0 for those fields and which will result in an exception related to database.

SO can anyone suggest how do i avoid sending 0 to DB when particular field of long type is not set ?


Any help will be appreciated !
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid the default values (0L for a long, not 0), you would have to ensure all fields are initialised to “real” values in every constructor.
[There are other ways to do it.]
 
Amit Goda
Ranch Hand
Posts: 37
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

Thank you for your reply. Actually i am not passing 0 for those fields. In real scenario, i am not setting those fields ( using the setter methods ).

To be more precise, there i have a class called Rating and there are two fields called currentScaleValue and goalScaleValue both of them are of type long.

Now initially user will set only one value i.e currentScaleValue and user won't set the other field. Now when the object of Rating class is passed with value for currentScaleValue only to the DB by default the other field goalScaleValue is set to 0 (as by default a field of type long in java will have value 0).
When an insert call is made to DB to store the values, since the goalScaleValue is by default 0 ( which cannot be inserted into table since 0 value are not acceptable) i am getting an exception saying that cannot set 0 for goalScaleValue column.

I cannot change the column properties in DB since that is created by other and we cannot modify it.

So how do i avoid passing 0 to DB in my java layer when i am passing the Object ?

PS : I am using Apache Axis for invoking the web service using client classes.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make it a Long so that the default value is null.
Or you need to change your web service code so that it replaces the 0 with something the database will accept.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure of your requirement but it sounds to me like you need to use 'Long' rather than 'long' and initialize the value to null. That way you can set the database column value to NULL (assuming it NULL is allowed). If you can't use NULL then you could maybe use the maximum value of a long a a flag to indicate that it is not yet defined.

:-) I'm so slow !!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your database won’t accept 0, you will have to initialise those fields to other values than 0. Maybe 1L?
Or use richard Tookey’s suggestion.
 
Amit Goda
Ranch Hand
Posts: 37
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Richard and Joanne for your suggestions.

Richard your suggestion seems to be good. But the thing is the project i am working is almost done and now i came across this error while testing.
Also as i said in my earlier post i am using web services (which are developed by others and not me) and when i generate client classes using those web services using apache axis(eclipse built in) the request and response handlers created have by default fields with data type long. So i just take the parameters set them and send a request to web service through client classes. And at this point when i don't set any particular fields with data type long they go as 0 in request.

Hope i am clear about the functionality.

Can i handle at java level or axis level ?
 
Amit Goda
Ranch Hand
Posts: 37
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess we cannot change the default rule applied by java so we will need to handle at DB level.


Cheers!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic