Using wrapper Integer class or int primitive in hibernate mapping

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.

Leave a Comment