What is the difference between primitive and wrapper classes in JPA (Hibernate) column mappings?

For example, theres an integer column in the database table. Then, in the java model, it can appear as a primitive int and Integer . My question is, what is the difference between int and Integer in this case? And does performance concern? Thanks!

+6
java jpa primitive wrapper
source share
2 answers

I try to avoid using primitives. This is especially true for the Id attribute. This allows you to detect an unset value by testing for null . If you use Java 5 or higher, automatic boxing eliminates pain (and does not apply to performance). But also for other attributes. As @skaffman pointed out, primitives are not suitable for null columns, and I prefer the code to be as flexible as possible.

+6
source share

You already mentioned the difference - Integer can be null , int cannot. Therefore, if the database column is NULL, you should use Integer .

As for performance, I would not worry about that. Modern VMs are very good at this.

+3
source share

All Articles