The difference between session.update () and Session.merge () in NHibernate

Source: Internet
Author: User

Today's work encountered a strange problem, as follows:

"A different object with the same identifier value is already associated with the session:19519146"

The context of the exception is as follows:

1: Generated when a WCF remote call is net.tcp bound;

2: Calling a method through the UI does not produce the exception.

Analyze the problem:

As can be seen from the exception information, this problem is caused by an attempt to associate a new entity to the session (19519146) after Session.update has been updated on a set of entities, Because there are already entities of the same identifier under the session.

Also, refer to Session.update's instructions:

Update (Object entity)
Update the given persistent instance, associating it with the current Hibernate Session.

It is true that this problem is caused by the update (why it is not clear that the problem arises, and someone knows not to hesitate to let us know).

Solution:

Review the persistence methods provided by the session:

Object Merge (Object entity)
Copy the state of the given object onto the persistent object with the same identifier.

As can be seen in the definition of the merge interface, the interface is to copy the given entity to a persisted entity under the session (not knowing if the understanding is correct).

Therefore, you can avoid this exception by calling the merge () method.

Ps:

By looking up the data, the Session.evict () interface can purge persisted entities from the session's cache, and in the context of the problem, by first calling the method, clearing the original persisted entity, then update (), and no duplicate associating exception is generated.

 

The difference between session.update () and Session.merge () in NHibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.