• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Pattern for Null Integer Field

 
Dionisius Purba
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I was wondering what's the recommended way/patterns for this situation in Hibernate:
1. I have one (or more) field with type is integer (or double or any numeric types), let's say age.
2. that field is nullable.
3. one solution: use primitive (i.e. int) instead of the wrapper class (i.e. Integer). primitive int always have value, thus I can not use this without any workaround.
3b. one of the workaround for primitive is assigning with a constant value (e.g. -9999) when the real value is null. But the consequences are there are effort for changing the value in retrieval and saving, not to mention that it is not elegant solution.
4. another solution: use wrapper class (Integer). But some says that using Integer will be slower than using primitive. And also when I need math operation, I need to call the .intValue() method.

I think that this is a common issue, but I can not find the pattern for this issue yet.

Any recommendations are much appreciated.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. another solution: use wrapper class (Integer). But some says that using Integer will be slower than using primitive.


Thats wrong. Using Integer is not going to slow down your application.

And also when I need math operation, I need to call the .intValue() method.


Which version of Java are you using? Java 5 has introduced autoboxing, which no longer requires you to convert Integer to primitive int using those APIs.

Any recommendations are much appreciated.

If your field is nullable, then going for Integer object, instead of primitive type, would be the right thing.
[ June 05, 2007: Message edited by: Jaikiran Pai ]
 
arulk pillai
Author
Ranch Hand
Posts: 3393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preference is using wrapper class as opposed to primitive variables. Some resons are as follows


--For boolean primitive you can only have true or false but wrapper Boolean can have true, false and null. The main frame systems can have a boolean data as null. So if you use primitive, what will you map the null data value to.

-- For currencies use BigDecimal.

-- Integer also gives you ability to have null values.

So IMHO wrapper classes give you better flexibility and the performance hit will be very minimal if there is one.
 
Dionisius Purba
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran and Arulk, thanks a lot for your advise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic