NHibernate Initiator Modifies Exception

I am trying to import some large files using batch requests and parallel processing, and I constantly get errors like

NHibernate.HibernateException: identifier of an instance of xxx was altered from ... to ... 

I know that I do not change my primary key at all. I use NH 2.1.x GA, ThreadStaticSessionContext, each file is processed in a separate thread (using ThreadPool), and the information in the files is uncorrelated. I have a session and transaction for each file, but I do not clear the session at all. In the same thread, I commit a transaction and close the session, but this error creeps me.

If you have any suggestions ...

+4
source share
3 answers

Just to close this, I believe that the error occurred due to weak coding skills (using shared resources spanning multiple streams without worrying about opacity) and the identifier for primary keys.

There was no place in the code where I set or changed the primary key values ​​myself.

0
source

In my case, I had a different solution to this problem. The error I saw was the following:

NHibernate.HibernateException: XXXX instance id was changed from 9 to 9

The problem was that I changed the identifier (column "id") in the XML mapping file from "Int64" to "Int32", but I forgot to switch the data class from Int64 to Int32. This explains the fact that NHibernate cannot change the identifier "9" from "9". The first "9" was Int64, but expected "Int32". Hope this helps someone.

+2
source
  • Set your id:

    public int ID {get; private recruitment; }

    so you can’t change it.

  • Check if the ID type matches the type in the database. (if the database has an int field and you set it to a long field, then nHibernate thinks it has changed.

  • If you use the identifier for PK, change it to Hi-Lo or something else. There was a lot of talk about not using an identifier in SQL Server. In some cases, this may fail.

+1
source

All Articles