Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between primitive and wrapper class in JPA (Hibernate) column mappings?

For instance, there’s an integer column in a database table. Then in java model, it can be mapped both as primitive int and Integer. My question is what's difference between the int and Integer in this case? And performance concern? Thanks!

like image 224
卢声远 Shengyuan Lu Avatar asked Apr 02 '10 06:04

卢声远 Shengyuan Lu


2 Answers

I tend to avoid using primitives. This is especially true for the Id attribute. This makes it possible to detect a not yet set value by testing for null. If using Java 5 or above, auto-boxing takes away the pain (and is not a performance concern). But also for other attributes. As pointed out by @skaffman, primitives are not suitable for nullable columns and I prefer the code to be as flexible as possible.

like image 177
Pascal Thivent Avatar answered Oct 02 '22 16:10

Pascal Thivent


You've already mentioned the difference - Integer can be null, int can't. So if your database column is nullable, then you should use Integer.

As for performance, I wouldn't worry about it. Modern VMs are very good at that sort of thing.

like image 40
skaffman Avatar answered Oct 02 '22 15:10

skaffman