This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21246|Reply: 0

[Source] a different object with the same identifier value was already associated with...

[Copy link]
Posted on 7/1/2016 10:48:52 AM | | | |
a different object with the same identifier value was already associated with the session: 398fdbba-57b8-4d3d-b84b-4c0c0b497f02, of entity: XXX



I have encountered this error twice in total, and I have never found a good solution, and this error occurred

The reason is probably well known, because in hibernate there are two identical logos in the same session

However, if it is a different entity, this error will be reported when running the saveOrUpdate(object) operation.

Hehe, maybe you will say, there is no difference between what you say and what you don't say, I admit, hehe, I don't know the specifics

Why this error occurred, otherwise it would not have been solved for a long time, now, give a temporary one

The solution is to give people like me who have no way to find the root cause a way to continue to implement

(Of course it is correct, but it does not start with the cause)
    In fact, it is very simple to solve this problem, just perform session.clean() operation

But you performed the saveOrUpdate(object) operation after the clean operation, which is possible

"Found two representations of the same collection", I searched for a lot of information.

There is no good explanation, of which this article helps the most

http://opensource.atlassian.com/projects/hibernate/browse/HHH-509。

Finally, it can be solved by the session.refresh(object) method, note that when the object is not

If it is an object with data in the database, you cannot use the session.refresh(object) factor

For refresh is to refetch the object from the hibernate session, if there is no object in the session

If there is this object, an error will be reported, so you need to judge before using saveOrUpdate(object).

Break it

    Of course, the easiest solution to this problem is to use the merge() that comes with Hibernate.

method. However, I always feel that when I encounter problems, I use the very useful method that comes with this software

(compared to saveOrUpdate(), save(), update()) feels very unpleasant.
    Later I also found that this error often appears in one-to-many mapping and many-to-many mapping, please be here

Be careful when using one-to-many and many-to-many mapping
Hibernate Troubleshooting and Handling
1、a different object with the same identifier value was already associated with the session。
Error reason: In hibernate, there are two identical identities in the same session, but different entities.
Workaround 1: session.clean()
PS: If you perform operations to change the data state such as saveOrUpdate(object) after the clean operation, you may receive a "Found two representations of same collection" exception.
Workaround 2: session.refresh(object)
PS: When the object is not an object with data in the database, you cannot use session.refresh(object) because the method is to refetch the object from the hibernate session, if there is no object in the session, an error will be reported, so you need to judge before using saveOrUpdate(object).
Solution 3: session.merge(object)
PS: Hibernate's own method is recommended.
2、Found two representations of same collection
Error cause: See 1.
Workaround: session.merge(object)
The above two anomalies often appear in one-to-many mapping and many-to-many mapping




Error causes:

Myth:You can't create a new user object and then get the user information based on the user ID and assign it to this object, so that a different object with the same identifier value was already associated with the session error!!





Previous:Usage of dynamic in C# 4.0
Next:Clone shallow replication of c# objects
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com