I am trying to import some large files using batch queries and paralel processing and I'm constantly getting errors like
NHibernate.HibernateException: identifier of an instance of xxx was altered from ... to ...
I 开发者_JS百科know I am not modifying my primary key at all. I am using NH 2.1.x GA, ThreadStaticSessionContext, every file is processed in a separate thread (using ThreadPool) and the information in the files is uncorelated. I do have a session and a transaction for each file, but I am not flushing the session at all. In the same thread I am commiting the transaction and closing the session but this error creeps me out.
If you have any suggestion...
In my case I had a different solution to the same problem. The error I saw was the following:
NHibernate.HibernateException: identifier of an instance of XXXX was altered from 9 to 9
The problem was that I had changed the identifier ("id" column) in the mapping XML file from "Int64" to "Int32" but I forgot to switch the data class from Int64 to Int32. That explained the fact that NHibernate couldn't alter the ID of "9" with "9". The first "9" was an Int64 but it was expecting an "Int32". I hope that helps someone.
Set your ID property to:
public int ID { get; private set; }
so that you can't change it.
Check if the ID type is the same as type in the database. (if database has an int field and you're setting it to long field then nHibernate thinks that it changed.
If you use identity for PK, changed it to Hi-Lo or something else. There has been a lot of talking about not using identity in SQL Server. In some circumstances it can fail.
Just to close this, I reckon the error occured because of poor coding skills (using shared resources spanning multiple threads without taking care on conccurency) and the identity over primary keys.
There was no place in code where I set or changed the primary key values by myself.
精彩评论