简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM