-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Why would you use unsaved-value="0"?
PostPosted: Mon Dec 19, 2005 4:53 pm 
Beginner
Beginner

Joined: Mon Nov 21, 2005 6:38 pm
Posts: 30
Location: New Zealand
Hi,

I am using managed versioning (with a numeric version), and when I create a new object, NH persists it to the database with an initial version number of zero (because Int32Type.Seed returns 0).

The issue I have come across is that when I was using either
Code:
<version name="Version" type="Int32"/>
or
Code:
<version name="Version" type="Int32" unsaved-value="0"/>
in my mapping file, this caused a crash when I updated the object and tried to re-persist it. This is because NH interpreted the version number of 0 as being unsaved and therefore tried to create another record with the same primary key.

The only way I could get my app to work is to set unsaved-value="-1" and to initialise the version number of every object to -1 before persisting it.

So my question is: why does the Hibernate In Action book give examples of mapping files with
Quote:
<version name="Version" type="Int32" unsaved-value="0"/>
when this will not allow you to update a previously persisted object?

IMHO, the problem would be neatly solved if NH made the initial version 1 instead of zero, as this would allow unsaved-value="0" to work and would remove the requirement to set the version number to -1 in the object itself.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 12:52 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This comes as a result of porting from Java where they can use nullable Integers for versions, instead of just ints. I'm not sure I should change the seed in the stable version, it would be a breaking change.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 4:01 pm 
Beginner
Beginner

Joined: Mon Nov 21, 2005 6:38 pm
Posts: 30
Location: New Zealand
Sergey, thanks for the explanation.

I guess that's the risk of using a book about Hibernate and not NHibernate. I agree that you should not change the value of Int32Type.seed, as that is likely to create other problems.

As a possible solution, perhaps 1 could be added to the initial seed number in NHibernate.Engine.Versioning.Seed? As the HIA book says
Quote:
The version number is just a counter value


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.