• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Ranch Hand
Posts: 640
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: 91
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: 67256
170
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: 640
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.


 
Master Rancher
Posts: 4017
47
  • 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: 640
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
Master Rancher
Posts: 4017
47
  • 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: 640
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.
 
Saloon Keeper
Posts: 20635
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 640
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.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!