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.