Skip to content

Instantly share code, notes, and snippets.

@baybatu
Last active April 21, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baybatu/67922ae32f5576e8c2604b69a4e4f583 to your computer and use it in GitHub Desktop.
Save baybatu/67922ae32f5576e8c2604b69a4e4f583 to your computer and use it in GitHub Desktop.
Why we must use wrapper type such as Long instead of primitive ones such as long for entity ID in Hibernate?

I ran into similar problem, where I was trying to save an object where his id was a primitive field (long), and at the time of create the object, the id was set to 0 by default, and Hibernate assumed I wanted to update the row with id = 0 (which obviously doesn't exist). My solution was change the id field to Long (wrapper), and that way, when I create a new instance the id is set to null, and Hibernate understands I am inserting a new element to the database. I hope it helps. Thanks.

Source: http://stackoverflow.com/a/30492460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment