Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Identifier and primitive types

 
Bai Shen
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.
 
Ran Pleasant
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic