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.
精彩评论