When performing a Data Conversion Program, some objects are updated with the following exception: a different object with the same Identifier value was already associated with the session: 162, of Class: mag. entity. transaction
At Nhibernate. impl. sessionimpl. checkuniqueness (Key key, object OBJ)
At nhibut. impl. sessionimpl. doupdatemutable (Object OBJ, Object ID, iclasspersister persister)
At nhibster. impl. sessionimpl. doupdate (Object OBJ, Object ID, iclasspersister persister)
At Nhibernate. impl. sessionimpl. Update (Object OBJ)
At Mag. dbconnect. connectsession. Update (Object OBJ) in D: \ magazine_v3 \ Mag. dbconnect \ connectsession. CS: Line 74
At Mag. Window. dbexchangeapp. form1.getexchange4 _ 1 () in D: \ magazine_v3 \ Mag. Window. dbexchangeapp \ form1.cs: Line 561
When querying on the Internet, there is very little information about. net, so let's look at Java and see some code here.
Http://forum.springframework.org/showthread.php? T = 27235
In order to find the root cause, I need to simulate this exception and the above Code gives some prompts.
1. Is there only one object in a connection in the valid state?
Entity. locator. issuelocator isuloc = new issuelocator (conn );
Entity. locator. transactionlocator tranloc = new transactionlocator (conn );
Entity. Issue ISU = isuloc. selectone (ISBN );
Entity. Transaction tran1 = tranloc. selectone (ISU );
Entity. Transaction tran2 = tranloc. selectone (ISU );
Conn. Update (tran1 );
Conn. Update (tran2 );
The above Code did not cause the above error. The test was successful. However, there is no update statement! Oh, is there no command to submit? Change the code to conn. begintransaction (); // Conn is a class that closes the isession.
Conn. Update (tran1 );
Conn. Update (tran2 );
Conn. committransaction ();
There is still no update statement. slightly modify the value of a certain attribute of tran1 and tran2. The update statement appears, but there is only one !! Tran1 updates are ignored. Smart!
2. Isn't the self-constructed object not available for update?
Copy the attributes used in the program to the test.
Private void copyobject (Object objsource, object objdest)
{
Type desttype = objdest. GetType ();
Type type = objsource. GetType ();
System. reflection. propertyinfo [] properties = type. getproperties ();
For (INT I = 0; I <properties. length; I ++)
{
If (! Properties [I]. canwrite)
Continue;
Object objval = properties [I]. getvalue (objsource, null );
If (objval = NULL)
Continue;
Properties [I]. setvalue (objdest, objval, null );
}
}
The test code is changed to entity. Transaction tran1 = tranloc. selectone (ISU );
Entity. Transaction tran2 = new transaction ();
This. copyobject (tran1, tran2 );
Tran1.memo = tran1.memo + "test. ";
Tran2.memo = tran2.memo + "test. ";
Conn. begintransaction ();
// Conn. Update (tran1 );
Conn. Update (tran2 );
Conn. committransaction ();
Exception occurred as scheduled. Happy to find nhib.pdf. nonuniqueobjectexception: a different object with the same Identifier value was already associated with the session: 4583, of Class: Mag. entity. Transaction
Update tran1 again. The test passes.