aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Identifier and primitive types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Identifier and primitive types" Watch "Hibernate Identifier and primitive types" New topic
Author

Hibernate Identifier and primitive types

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 16, 2003
Posts: 75
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.


Ran
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate Identifier and primitive types
 
Similar Threads
Hibernate's Inheritance strategies
what data type should I use for Oracle Numeric and Date
Exception setting property value with CGLIB
inserting NULL with iBatis
Do not use primitive types in JPA for mapping