Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a different object with the same identifier value was already associated with the session: 10, of entity: Sales.Entities.Ttable1

Tags:

nhibernate

When I use session.update(object) I get below error and how can I rectify this error? I have even tried Session.evict(object) still it is not solved for the below error

 a different object with the same identifier value was already associated with the           session: 10, of entity: Sales.Entities.TTable
like image 543
Philip Avatar asked Feb 06 '12 12:02

Philip


2 Answers

Is your application a web application?

Use Session.Merge(object)

In web applications you sometimes serialize and de-serialize objects, then you do so you create new objects, so even if the NHibernate objects has the same ID, the object reference is different. Then you try to update your de-serialized object, NHibernate detects another object with the same ID but different reference pointer thus don't know which object in your memory is the "correct" one.

the Merge() method is used in these cases, so you can update objects that in your web application.

like image 117
Mr Mush Avatar answered Jan 02 '23 21:01

Mr Mush


What are you trying to accomplish, the error means that you are trying to update an object that hibernate already has its internal cache but you are sending a different object reference.

Using Session.Merge is a possibility but in most cases if you are in a web context you should already have the reference somewhere because you most likely use Session per request. So the question is:

  • Why do you have the object you are trying to save in cache but not using it?
like image 45
jakobandersen Avatar answered Jan 02 '23 20:01

jakobandersen