Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Integer versus int  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi team,

Just seeking clarification on one of the finer points of ORM (Hibernate specifically).

I have a domain entity class named 'Vessel' to represent ships.  Ships, just like automobiles, have a registration plate type unique identifier called the 'IMO' (usually painted on the stern).  Therefore, my Vessel class includes the following field declaration.



It is not a required field, because sometimes the user doesn't yet know the IMO when creating a new vessel.  But if the IMO is entered, then it must be unique.

The problem is that when this field is left blank, an IMO of '0' is recorded.  Then next time a vessel is created without an IMO, the database (entirely expectedly) protests that a vessel with imo=0 already exists - it is not unique.

I was about to change the IMO field type from int to Integer, but just thought I'd check that this is the correct approach.  As perhaps this problem may be solved with an annotation that prevents int from defaulting to zero, which is normal for this primitive variable type.

Also, should I switch to Integer, does Hibernate need to modify the database table column type?

Thanks in advance,

Chris.
 
Saloon Keeper
Posts: 2470
317
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you will most likely not be performing any mathematical operations on the identifier after it has been inserted into the database, maybe a String/varchar would be a better data type.  

You may however want to validate the IMO number by calculating and comparing the check digit if you cannot trust it to be valid.
 
Rancher
Posts: 3983
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the column can be null then it would need to be Integer, a change which only involves the Java side as the column type will be the same on the server.

However, Ron is quite correct in that the IMO is not really a number.
Indeed, by representing it as a String you can ensure it is 7 digits long and, should the number start with a 0, the formatting won't be an issue.  If the column is an Integer then '0123456' would end up in the database as '123456' which would involve some work to get it to display correctly, whereas if it were a String then you wouldn't need to worry about that.
 
Bartender
Posts: 20557
120
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But just to answer the literal question. No, converting the Entity class from using int to Integer has no effect on the database schema and (especially since auto-boxing was added to Java) relatively little effect on application code.

And since you can annotate an Integer as @NotNull, this would indeed be effective.

Although I would consider Ron's advice on the true type of this particular item.
 
Christopher Dodunski
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ron, Dave and Tim,

I took your advice and changed the int field to String rather than Integer.  I was probably originally a little too swayed by performance considerations, particularly given the 'imo' column is likely to be indexed.  Still, vessel imo and mmsi numbers are not numbers in the true sense of the word, rather labels that just happen to comprise numerical characters.  So your advice to use String/varchar makes good sense.

Thanks & regards,

Chris.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!