Another object with the same identifier value was already associated with the session: 10, object: Sales.Entities.Ttable1

When I use session.update (object), I get below error and how can I fix this error? I even tried Session.evict (object), but it is not yet allowed for error below

a different object with the same identifier value was already associated with the session: 10, of entity: Sales.Entities.TTable 
+6
source share
3 answers

Is your application a web application?

Use Session.Merge(object)

In web applications, you sometimes serialize and de-serialize objects, then you create new objects, so even if NHibernate objects have the same identifier, the reference to the object is different. Then you try to update your de-serialized object, NHibernate discovers another object with the same identifier, but a different reference pointer, thus, does not know which of the objects in your memory is β€œcorrect”.

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

+17
source

What you are trying to execute, an error means that you are trying to update an object that hibernate already has its own internal cache, but you are sending another link to the object.

Using Session.Merge is an opportunity, but in most cases, if you are in a web context, you already have a link, because you are most likely using Session for the request. So the question is:

  • Why do you have an object that you are trying to store in the cache, but not using it?
+4
source

Use Session.Clear () before Session.Update (object).

-6
source

Source: https://habr.com/ru/post/907765/


All Articles