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

2

7

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!

Unquote answered 2/4, 2010 at 6:36 Comment(1)
of course, I know that it will occur an exception when primitive int meets null value in database.Hewitt
S
7

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.

Sharma answered 2/4, 2010 at 8:35 Comment(0)
G
3

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.

Golightly answered 2/4, 2010 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.