-->
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.  [ 6 posts ] 
Author Message
 Post subject: Rolling back identity ID?
PostPosted: Thu Dec 22, 2005 7:45 pm 
Newbie

Joined: Thu Dec 22, 2005 6:56 pm
Posts: 8
Hi all.

I have set up a persistant class mapping to a table with an identity ID.
All seems well, except that if I SaveOrUpdate() an object within a transaction which is subsequently rolled back, my object still keeps it's new identity ID (even though the transaction is rolled back in the backend DB).

I also tried using Save() instead of SaveOrUpdate(), and also tried closing the session and even disposing it immediately after rolling back the transaction, but with no luck. My persistant object stays "dirty".

Needless to mention that subsequent Save()s to the same object do nothing agains the DB (I assume this is because it already has an ID), and trying to Load() it by it's ID causes an ObjectNotFound exception, because the ID was assigned but never committed to the DB.

Is NHibernate supposed to be able to un-assign the assigned ID in my persistant object if it's save() is rolled back?

If not, what would be the best way to ensure that my persistant object doesn't stay "dirty" after rolling back it's save()?

I've been reading through the docs, forums and FAQs trying to find any information about this for a couple of weeks but havn't been successful so far, so I hope anybody here can direct me how to address this issue.

Thanks in advance,
- Avi


Hibernate version:
NHibernate.dll v1.1.4322 running under .Net 2.0




Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
   <class name="Entities.DataRecord, Entities" table="DataRecords">
      <id name="ID" column="RecordId" type="Int32"  unsaved-value="0">
         <generator class="identity">
         </generator>
      </id>
      <property name="Name" column="RecordName" type="System.String"        />
   </class>
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():
Code:
         // create a persistable object:
         DataRecord dr = new DataRecord(); // this is my persistant object
         dr.Name = "something";

         // save in a transaction:
         using (ITransaction tx = _s.BeginTransaction()) // _s is my session
         {
            _s.Save(dr);

            // make sure it gets an id:
            Assert.AreNotEqual(0, dr.ID, "ID should be non zero after saving"); // this passes

            // rollback:
            tx.Rollback();
            _s.Close(); // even this doesn't help
            _s.Dispose(); // and even this!
         }
         Assert.AreEqual(0, dr.RecordId, "ID Should be 0 after rolling back"); // this fails no matter what!



Name and version of the database you are using:
MSSQL 7.0.
The mentioned behaviour happens both with OleDb and SqlClient
(I don't think it has to do with the DB anyway, since the transaction *is* rolled back at the DB.


Top
 Profile  
 
 Post subject: Typo fix
PostPosted: Thu Dec 22, 2005 7:54 pm 
Newbie

Joined: Thu Dec 22, 2005 6:56 pm
Posts: 8
In original post my post I had a typo:

Original post:
Code:
Assert.AreEqual(0, dr.RecordId, "ID Should be 0 after rolling back"); // this fails no matter what!


Should be:
Code:
Assert.AreEqual(0, dr.ID, "ID Should be 0 after rolling back"); // this fails no matter what!


Nevertheless the problem still remains (the typo was just in my copy-pasting and formatting for the post, not in my original code)

Still seeking any help on this issue,
- Avi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 23, 2005 5:42 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate doesn't roll back identifiers in case of aborted transactions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 26, 2005 5:38 am 
Newbie

Joined: Thu Dec 22, 2005 6:56 pm
Posts: 8
Hi Sergey.

Thans for the clarification - at least I know not to keep looking through the docs..

Is there any common practice as to dealing with this issue?

Since the unsaved-value of the ID is what tells NH if the object should be INSERTed or UPDATEd later on, having a dirty ID can have serious implications later on throughout the application. I can imagine I'm not the first to address this issue (or am I?)

Thanks,
- Avi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 26, 2005 7:16 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Right now you have to deal with it manually. I believe identifier rollback was implemented in Hibernate 3, so it might be backported to NH in the future. There is already a feature request in JIRA about this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 1:16 pm 
Newbie

Joined: Thu Dec 22, 2005 6:56 pm
Posts: 8
Thanks,
I'll be looking forward to seeing this feature in Nhibernate.

- Avi


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