• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate Identifier and primitive types

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Hibernate documentation, they recommend using a nullable Wrapper type for your identifier field. Why would you want to do this? I can't think of a reason to have a null identifier.

Also, I've seen mentioned in a couple places that you should use the nullable Wrapper types instead of primitives for your fields. Is there a particular reason for this, and if so, what is it?

Thanks.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bai Shen wrote:In the Hibernate documentation, they recommend using a nullable Wrapper type for your identifier field. Why would you want to do this? I can't think of a reason to have a null identifier.

Also, I've seen mentioned in a couple places that you should use the nullable Wrapper types instead of primitives for your fields. Is there a particular reason for this, and if so, what is it?




There are probably some very good reasons for their recommendations. However, I have not experienced any problems using the primitive int as a primary key. I just configure the value of zero to indicate an unsaved record.

Hibernate will throw an exception if it tries to load a null value into a primitive object field. Therefore, if your database field is nullable then you must use a Wrapper type in your object. If the database field is non-nullable then you can use a primitive. Just be aware that a primitive will always put a value, which ensures the database does not throw an exception. An automative value of zero is not a problem is the database field holds just a plain value, such as amount, weight, length, etc. However, if the database field is a foreign key then an automatic value of zero will result in either an referential integrity exception or, ever worst, a possible mis-association between records if for some strange reason you have a master record with a pirmary key value of zero.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bai Shen wrote:In the Hibernate documentation, they recommend using a nullable Wrapper type for your identifier field. Why would you want to do this? I can't think of a reason to have a null identifier.

Also, I've seen mentioned in a couple places that you should use the nullable Wrapper types instead of primitives for your fields. Is there a particular reason for this, and if so, what is it?

Thanks.



From a database point of view its invalid to have a null identifier. I suspect this is a Hibernate implementation restriction, my guess being this might misbehave with saveOrUpdate type methods. For example, if you have an existing entity with a PK value of 0 and you use a primitive type for your identifier field its default value will be zero which increases the possibility that the update check behind this method will resolve to true (and you'll get an update when you wanted an insert).
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic