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

落花浮王杯 提交于 2020-03-18 12:07:56

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2565352/whats-difference-between-primitive-and-wrapper-class-in-jpa-hibernate-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!