-->
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.  [ 5 posts ] 
Author Message
 Post subject: Getting primary key value after saving a new object
PostPosted: Wed Jun 01, 2005 5:19 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I have a customer object with a corresponding table in a SQL Server-database. The object has a property 'Id' which corresponds with the primary key 'customerid' which is an identity column.
When I make a new customer and then push the save button, the following code is executed:
Code:
private void SaveCustomer()
      {
         try
         {
            if (_customer.Id == -1)
            {
               SaveObject(_customer);
            }
            else
            {
               UpdateObject(_customer);
            }
         }
         catch (Exception ex)
         {
            ShowError(ex.Message);
         }            
      }

public void SaveObject(object obj)
      {
         ITransaction trn = _activeSession.BeginTransaction();
         try
         {
            _activeSession.Save(obj);
            trn.Commit();
         }
         catch (Exception ex)
         {
            trn.Rollback();
            throw ex;
         }
      }


This works fine, the new customer is saved into the database. But...
When I push the save button again, the Id-property of the customer is still -1 and SaveObject is invoked while this time it shouldn't be. NHibernate comes up with the following error: 'An error occured: null id in entry (don't flush the Session after an exception occurs)'.
In other words, how do I get the Id-value of a new customer? Should I refresh the customer object manually or has NHibernate accommodations for this situation?

My mapping file looks 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="Int32" column="customerid" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      
      <property name="Name" column="name" type="String" length="100"/>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 6:09 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
Oops, sorry guys. The problem had nothing to do with NHibernate.
In the Page_Load event I didn't check on IsPostback, so on every postback event a new instance of customer was created. When I click on the Save button, a new customer was made which id is obviously -1. After that the SaveObject was called.
Now I save the current customer in a Session-variable to maintain the right instance of a customer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 6:14 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
JackBigMac wrote:
Now I save the current customer in a Session-variable to maintain the right instance of a customer.


Just be careful. This may not scale well.

BOb


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 4:20 am 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
What other solution would you use then? The click on the save button triggers a postback, so the instance of the customer has to be saved somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 10:02 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
It really depends on your system. But, if you want to scale up and out I would say:

Rehydrate the object on each postback were it is needed. This requires that you only store the Customer Object ID in the page view state. If you do need caching of the object you can use a second level NHibernate cache. Of course, YMMV.

BOb


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