I didn't check against the JPA Spec itself, but the JEE7 tutorial (chapter 37)
does not seem to mention anything specific against your rationale.
Personally, for all Integer Id fields in tables, I have always seen that the
entity maps the Id field to an Integer. In some cases, that may be the rule
for the creation of entities via CRUD generation functionality...
Puspender Tanwar wrote:This question came to my mind, after reading the Effective Java by Joshua Bloch. Where author has clearly advised to avoid wrapper if primitive can be used, since wrapper require more space.
That comes under premature optimization. Realize, also, that the amount of space in the database is unaffected by which way you define the ORM field. And in general, you shouldn't be keeping enough instances of data in RAM at any given time for the amount of storage used to make a difference.
Dave anticipated my original thought, which was that ints always have a value, but JPA can and does create un-initialized objects internally, where the difference between "missing" (null) and default value (zero) might be important.
I'd like to say that I've actually had experiences that led me to avoid using int in ID fields, but I can't recall anything concrete.
And yes, the wrapper classes are a horrible kludge, but they're the only way to do object-oriented operations on primitives. Although OOP purists would probably say that it's the primitives themselves that are the real kludge.
An IDE is no substitute for an Intelligent Developer.
Tim Holloway wrote:I'd like to say that I've actually had experiences that led me to avoid using int in ID fields, but I can't recall anything concrete.
I found a real world issue.
Suppose I have an entity 'Question' having 'views' as a column which couldn't be null(of course)
If I persist the above JPA entity without setting the views property, ideally JPA should stop me to persist the entity but views being a primitive value, the default '0' value will be available and persisted to the DB.
This issue doesn't comes up with Integer, because default value would be null(if not provided externally). And JPA will force us to provide a valid integer value for views property, as the column in not-nullable.