Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hibernate Pattern for Null Integer Field

 
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.
 
Sheriff
Posts: 10445
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 ]
 
Author
Posts: 3445
  • 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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic