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.  [ 7 posts ] 
Author Message
 Post subject: refresh nhibernate object metadata
PostPosted: Tue Dec 12, 2006 2:00 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
Hibernate version: NHibernate1.2.0Beta 2

Name and version of the database you are using: SQL Server 2000

The generated SQL (show_sql=true): n/a

In attempting to save an nhibernate object to the database, when I get an insert error, I'd like to retry a few times before failing. In order to retry I need nhibernate to treat the object like a new, unpersisted object. However, if I try to save again, it uses the same primary key as the first try and fails in the same manner. If I attempt to "clean" the object so nhibernate will try to persist once again from scratch by changing the id back to 0 (its default), I get an nhibernate error that the id has changed.

How can I get the object back in its original state before I tried to persist it the first time? I could make a copy of the object before I try to save, but is there an easier way?


Top
 Profile  
 
 Post subject: evict()
PostPosted: Tue Dec 12, 2006 6:10 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
This seems strange that you would want to keep retrying the save. Have you tried evicting the object before retrying the save? Something like:

session.evict(instance);
instance.id = 0;
session.save(instance);
session.flush();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 7:01 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
I haven't. I will try that. Thanks for the helpful reply. I will let you know my results.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 1:21 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
I tried that and am in fact still getting the error. Here is the error message:

identifier of an instance of HELM.Domains.Payments altered from 100004939 (System.Int32) to 0 (System.Int32)


Top
 Profile  
 
 Post subject: Mapping files & code
PostPosted: Sat Dec 16, 2006 9:19 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
This seems to work for me, using this code:

Code:
Resource resource = dao.GetById(1);
dao.Evict(resource);
resource.Id = -1;   // my unsaved value is -1
Resource savedResource = dao.Save(resource);
dao.CommitChanges();
Console.WriteLine(savedResource.Id);    // outputs 45


My Id property is an Identity column in the database. What does your code and mapping files look like?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:26 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
Here is my mapping file

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="HELM.Domains.Payments,HELM.Domains" table="Payment" lazy="false" >
    <id name="Paymentid" column="PaymentID" type="System.Int32">
      <generator class="increment"/>
    </id>
      <property column="LastName" type="System.String" name="LastName" />
      <property column="FirstName" type="System.String" name="FirstName" />
      <property column="Address1" type="System.String" name="Address1" />
      <property column="Address2" type="System.String" name="Address2" />
      <property column="City" type="System.String" name="City" />
      <property column="State" type="System.String" name="State" />
      <property column="Zip" type="System.String" name="Zip" />
      <property column="Phone" type="System.String" name="Phone" />
  </class>
</hibernate-mapping>


Here is my code

Code:
//payment retries loop for PK violation
            for (int i = 0; i < 10; i++)
            {
                try
                {

                    //we need a paymentID and an OrderID before we can send
                    //the payment to the CGW
                    PersistenceManager.Save(payment, false);

                    theReturn = ProcessPayment(payment);

                    //process the payment
                    if (theReturn.Result == false)
                    {
                        //payment declined
                        PaymentDeclined(payment);
                    }
                    else
                    {
                        PaymentAuthorized(payment);
                    }

                    //save the payment
                    PersistenceManager.SaveOrUpdate(payment, true);

                    //no pk errors, leave our loop
                    break;
                }
                catch (Exception ex)
                {

                    Exception innerMostException = ex;

                    //get the inner-most exception
                    while (innerMostException.InnerException != null)
                    {
                        innerMostException = innerMostException.InnerException;
                    }

                    if (innerMostException.GetType() == typeof(SqlException))
                    {
                        //these error numbers are all error numbers for PK violations
                        if (((SqlException)innerMostException).Number != 547 && ((SqlException)innerMostException).Number != 2627 && ((SqlException)innerMostException).Number != 2607)
                        {
                            //something other than a PK violation and this loop
                            //isn't here to handle that
                            throw new Exception("Unable to apply payment. \n\n\n" + "message is: \n\n" + ex.Message);
                        }
                        else
                        {
                            payment.Paymentid = 0;
                        }
                    }
                    else
                    {
                        //something other than a PK violation and this loop
                        //isn't here to handle that
                        throw new Exception("Unable to apply payment. \n\n\n" + "message is: \n\n" + ex.Message);
                    }
                }
            }


Thanks for your help


Top
 Profile  
 
 Post subject: identity
PostPosted: Thu Dec 21, 2006 5:17 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
If you can, change your PaymentID column to an IDENTITY column and change your NHibernate mapping appropriately. That would be the easiest thing to do.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.