This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

int vs Integer for JPA entity's Id field?  RSS feed

 
Ranch Hand
Posts: 627
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the best choice for storing Ids in the persistence layer(JPA) if the database Id type is integer?

I prefer int over Integer :
1. int has some performance and memory benefits over Integer
2. since Ids are always non-nullable, there is no other reason to use Integer over int.

Please correct me if I missed something.
 
Ranch Hand
Posts: 73
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

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...
 
Marshal
Posts: 67165
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
I prefer int over Integer :
1. int has some performance and memory benefits over Integer
2. since Ids are always non-nullable, there is no other reason to use Integer over int.

Please correct me if I missed something.


You missed something.

Firstly, (1) is a micro-optimization. Unless you have hard numbers to show a demonstrable benefit, it's irrelevant.

Secondly, how are you going to insert a new entity, if the id cannot be null?
 
Puspender Tanwar
Ranch Hand
Posts: 627
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Secondly, how are you going to insert a new entity, if the id cannot be null?


I didn't get your words, Why would I require null to insert a  new entity?
I kept the id datatype as int and used Spring Data Jpa for persistence with Hibernate as implementation.


 
Rancher
Posts: 3767
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What id does that new User have?
Get the test to create another User and see what id that one has.
Check that they are different.
 
Puspender Tanwar
Ranch Hand
Posts: 627
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IDs are getting generated in sequence starting from one. Yes on each new insertion, the IDs are different.
 
Dave Tolls
Rancher
Posts: 3767
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had forgotten that it treats 0 as NULL (or effectively) which I feel is dodgy ground.
After all, zero is a perfectly valid id.

I can imagine some confusion if this strategy was applied to a pre-existing database where this was the case.
 
Puspender Tanwar
Ranch Hand
Posts: 627
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 19996
95
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Puspender Tanwar
Ranch Hand
Posts: 627
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!