• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate id null issue

 
Prashanth Talapala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I seem to have a problem with setting the id of a bean when the bean is being saved for the first time. The id type="long" and the unsaved-value="null". Sometimes, when an object is being created and tried to be persisted I am getting an error that the id value is null and the object cannot be persisted. This error I am getting in a random fashion, which makes it harder to figure out the problem. Has anyone faced a similar problem? Should I use type="java.lang.Long" instaed of type="long" ? How does it matter what the type is? Please help me.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the generator class that you are using for the ids?
 
Prashanth Talapala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"unsaved-value="null""

Why do you have that? It is saying that if there is no value, then try to put null in it, which would cause a database error because the database is probably set up so that this is the primary key or at least a not null field.

Mark
 
Prashanth Talapala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The unsaved-value="null" is used to identify a new first time to be persisted object from an object which is already persisted and is being updated. So when hibernate tries to persist an object it sees what the id value is. If the id value is null (in the case of a newly created object) then it assigns an id using the generator and persists the object. So which brings me to my earlier problem, I am getting a random error sometimes saying cant insert null as id into db. Why is that? is it because hibernate is unable to set an id or it is running out of ids??

-Prashanth.
 
Ran Giri
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking its because of the column you are trying to access must be not null.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ranjani Giri:
I am thinking its because of the column you are trying to access must be not null.


Which is the same reason why I asked about unsaved-value="null". By default you will not have a value in that attribute when it is a new object, and Hibernate should use the generator type to generate the ID for you.

Mark
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using


That configuration is supposed to be equivalent to


Is there any possibility that there is something strange going on with the "hibernate_unique_key" table or "next_hi" column that would cause the generator to generate nulls instead of IDs (for example, next_hi returns null under some circumstances)?

Your best bet is to set <property name="show_sql">true</property> and watch the traffic on the "hibernate_unique_key" table.

WRT unsaved-value

unsaved-value (optional - defaults to a "sensible" value {null|any|none|undefined|id_value}): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic