Why is NHibernate removing an instance of an immutable class?

I am trying to port an application from NHibernate 1.x to 2.1 and I noticed something strange: my immutable objects are being deleted in my integration tests.

For example, this test is used to pass:

[Test, ExpectedException(typeof(NHibernate.HibernateException))]
public void DeleteExistingInstanceThrows()
{
    // Note: NHib transaction is opened/rolled back in test setup/teardown
    m_testRecordId = CreateRecordInDB();
    var dao = new NHibFactory().GetTransDao();
    var obj = dao.GetById((long)m_testRecordId, false);

    dao.Delete(obj);
    dao.CommitChanges();

    Assert.IsFalse(TestRecordExists(m_testRecordId));
}

      

The CommitChanges call must be thrown because of an attempt to delete an instance whose type is mapped with change = "false". However, after updating the expected exception type, this test now fails (since no exception is thrown). I checked the database and tracked it and the record was created, the instance was loaded and the record will be deleted afterwards.

The corresponding class maps to the same table as the other class β€” more or less designed to be viewed-only for a read-only subset of that data. Here's the class mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="Sample.Trans, Sample" table="tblTrans" mutable="false">

        <id name="Id" column="transId" unsaved-value="0">
            <generator class="identity" />
        </id>

        <property name="Amount" column="amount" type="Decimal" />

        <!-- ...a half dozen other mapped fields... -->
    </class>
</hibernate-mapping>

      

I expect I'm missing something, but I've been scouring the web for whatever reason why a mutable attribute might be ignored and I came out empty.

UPDATE:

Writing in Log4Net, it's clear that NHibernate is sending a delete command to the database.

+2


source to share


2 answers


After digging into it, I found this block of code commented out in the OnDelete method of the DefaultDeleteEventListener:

/*
if ( !persister.isMutable() ) {
throw new HibernateException(
"attempted to delete an object of immutable class: " +
MessageHelper.infoString(persister)
);
}*/

      

As a result, no exception is thrown when an attempt is made to delete an immutable instance, and the deletion actually occurs afterwards, as if the instance had changed.

I posted a note to NHibernate support with my findings - looks like a bug to me.



Featured another poster, this issue has been fixed in version 2.1.1 as of October 31, 2009.

UPDATE:

As of February 2011, NHibernate can now drop immutable class instances again. Immutable objects must be removed - the fact that they weren't originally an actual error.

+3


source


I had exactly the same problem in the same situation (upgrade from 1.x). Fixed bug in 2.1.1 which was released on October 31st.



/ Emil

0


source







All Articles