-->
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.  [ 4 posts ] 
Author Message
 Post subject: Unsaved value doesn't work
PostPosted: Tue May 24, 2005 2:48 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
My customer object has a property Id which corresponds with the primary key in the database table.
I defined the hbm.xml like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyApp.Core.Domain.Customer, MyApp.Core" table="customer">
      <id name="Id" type="Int64" column="customerid" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      <property name="Description" column="description"
   </class>
</hibernate-mapping>


When I create a new instance of the customer object, Id remains 0 and not -1 like I defined. Am I doing something wrong?

Greets,
Jack


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 3:26 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, you misunderstood it a bit. You should set unsaved-value to the value that id is initialized by default, not the other way round. You can't really expect NHibernate to somehow detect creation of your objects and alter their variables.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 3:59 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
Yes, I misunderstood that part apparently. Now, I set the property Id of the customer object default to -1 in the constructor of this object.
But why would you want to use the unsaved-value option then? I use an identity-field in MSSQL for the Id-property, so I leave the generation of values for the Id-column up to MSSQL. Can I omit unsaved-value in this situation?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 4:22 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Unsaved-value is used in SaveOrUpdate to determine whether the object should be INSERTed or UPDATEd. Even if you don't call SaveOrUpdate yourself, it is also called when processing cascades, so having unsaved-value specified correctly is important.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.