Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wrapper Integer class or int primitive in hibernate mapping

In the company that I work for we have this major discussion on whether it should be better to use wrapping classes for primitives (java.lang.Integer, java.lang.Long) or whether to use the primitive types directly in the POJOs that map Entities to Tables in Hibernate.

The idea is that we want these values to not be null in the database.

The arguments in favor of using primitives:

  • Handling these values as int means that they can never be null, in this way making it impossible to inadvertently get a null reference on the field.
  • int=32/64 bits of memory. Integer = 16 bytes of memory and is also slower

The arguments in favor of using wrapper objects:

  • We can add a constraint at the database level to always prevent null values from getting there
  • We can end up with misleading data, we can have 0's instead of nulls in the database whenever the user doesn't set a value and buggy data is a tough catch.
  • Objects have more expressive power than primitives. We have null values and also integer values, so we can validate them easier using annotations for example (javax.validation.constraints.NotNull).
like image 536
vanvasquez Avatar asked Sep 21 '11 21:09

vanvasquez


People also ask

Should I use primitive or wrapper?

Verdict. Generally, choose primitive types over wrapper classes unless using a wrapper class is necessary. Primitive Types will never be slower than Wrapper Objects, however Wrapper Objects have the advantage of being able to be null.

What is the difference between wrapper class and primitive?

Wrapper class creates an object and primitive does not create object. Wrapper classes are used with Collections to represent type. Wrappers have methods and can hold memory address/null and primitives hold default values. Primitives are fast compare to wrapper classes as there is no overhead of methods or object.


2 Answers

Use wrappers, make your life simple.

Your data model should dictate this. You should be enforcing nullability in the database anyway.

If they are nullable in the database, then use wrappers. If they are not nullable, and you use wrappers, then you'll get an exception if you try and insert a null into the database.

If your data model doesn't dictate it, then go for a convention, use wrappers all of the time. That way people don't have to think, or decide that a value of 0 means null.

I would also query your assertion that it would less performant. Have you measured this? I mean really measured it? When you're talking to a database, there are a lot more considerations than the difference between 16 bits and 32 bits.

Just use the simple, consistent solution. Use wrappers everywhere, unless somebody gives you a very good reason (with accurate measured statistics) to do otherwise.

like image 192
Matthew Farwell Avatar answered Sep 22 '22 02:09

Matthew Farwell


Thought it should be mentioned:

Hibernate recommendation (section 4.1.2) using non-primitive properties in persistent classes actually refers - as titled - to identifier properties:

4.1.2. Provide an identifier property

Cat has a property called id. This property maps to the primary key column(s) of a database table. The property might have been called anything, and its type might have been any primitive type, any primitive "wrapper" type, java.lang.String or java.util.Date.

...

We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.

Nonetheless, the advantages of primitives aren't strong:

  1. Having an inconsistent non-null value in a property is worse than NullPointerException, as the lurking bug is harder to track: more time will pass since the code is written until a problem is detected and it may show up in a totally different code context than its source.
  2. Regarding performance: before testing the code - it is generally a premature consideration. Safety should come first.
like image 21
yair Avatar answered Sep 23 '22 02:09

yair