Reputation: 979
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:
The arguments in favor of using wrapper objects:
Upvotes: 51
Views: 28193
Reputation: 118
If you are using a database like mongo db, OR the data has meaning when its null go with Wrapper,
Tip: If you are doing calculations just create an extraNullChecked getter and use that to avoid null checks all over the codebase.
If the Data point has to have a value all the time and can't be null, use primitive.
Upvotes: 0
Reputation: 723
I'm gonna go out on a limb and disagree with all of the other answers here. If the field is nullable in the database, use a wrapper. If it can't be null, use a primitive. Now you get the performance benefits, AND it's much easier to distinguish between fields that can and can't be null in your code.
Upvotes: 0
Reputation: 9245
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:
Upvotes: 16
Reputation: 61705
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.
Upvotes: 48
Reputation: 1500675
The Hibernate documentation (just the first version I happened to find) states:
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.
So the "expert's voice" suggests using Integer
/ Long
... but it's not described why this is the case.
I wonder whether it's so that an object which hasn't been persisted yet can be created without an identifier (i.e. with a property value of null
), distinguishing it from persisted entities.
Upvotes: 7