• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Long vs. long in hibernate 2

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We are using hibernate 2. We have an old system that uses Long in persistent class fields. Now we have some problems because of the fact that it is an object and not primitive type. Now I can do 2 things.
1. defend in the code against the problem (very easy)
2. change it to long and upgrade all the dbs that we already have - I do not mind doing this

Now I guess my question is what's right for hibernate 2 Long or long? And if you have docs to back up your answer will bhe great!

Thanks,
John
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problems are you having?
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problems have nothing to do with hibernate or this question.
Can you answer this question according to the data I provided?
Thanks.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:what's right for hibernate 2 Long or long? And if you have docs to back up your answer will bhe great!

John

If the database column allows null values then the related object field should be an object (Long, Integer, etc.). If you use a primitive as the field type then a null value in the column will result in an exception when Hibernate attempts to load the object from the database since a null cannot be put into a primitive field.

If the database column does not allow nulls then you can use either an object or a primitive as the field type. But do note that a null value in the object field will result in an exception when the object is written to the database. If a primitive type is used you will never get the exception since the primitive will always have a value, the defult being zero. But this can lead into another problem, a defult value of zero will probably not be a problem for plain value fields, such as weight, count, etc., but if the field is a foreign key then you'll get a referential integrity exception from the database or, in the worst case, have that record incorrectly releated to the parent record who's primary key is zero.

Make the best choice for your situation and then program with that choice in mind. Dancing with the Devil is not so bad as long you are remember who will be stepping on your toes.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GREAT answer!
Thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic