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.
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.