A different object with the same Identifier value

Source: Internet
Author: User

Today I encountered this problem. Baidu found the answer.

This error has not found a good solution.

The reason is that we all know that the same session in hibernate has two identical identifiers.

But it is a different entity. This error is reported when the saveorupdate (object) operation is run.

Haha, maybe you will say that you are no different from saying so. I admit, Haha, I don't know the specifics

Why does this error occur? Otherwise, it will not be solved for a long time. Now, a temporary

Solution: Just like me, one of the people who cannot find the root cause can continue to do so.

(Of course it is right, but not from the cause)

To solve this problem, you only need to perform session. Clean ().

But you have performed the saveorupdate (object) operation after the clean operation, possibly

"Found two representations of same Collection" will be reported. I have found a lot of information,

There is no good explanation. This article has the greatest help

Http://opensource.atlassian.com/projects/hibernate/browse/hhh-509.

The session. Refresh (object) method can be used to solve the problem. Note that when the object is not

When it is an object with existing data in the database, you cannot use session. Refresh (object) because

For refresh, the object is retrieved from the hibernate session.

If this object exists, an error is reported. Therefore, you need to determine the value before using saveorupdate (object ).

Break down

 

Of course, the easiest way to solve this problem is to use Merge () in hibernate ()

Method. However, I always think that this kind of software comes with a very useful method.

(Compared with saveorupdate (), save (), and update.

Later, I also found that such errors often occur in one-to-multiple ing and many-to-many ing.

Be careful when using one-to-many and many-to-many ing.

 

 

Hibernate troubleshooting

1. A different object with the same Identifier value was already associated with the session.

Cause of error: In hibernate, the same session contains two identical identifiers but different entities.

Solution 1: Session. Clean ()

PS: If saveorupdate (object) and other operations to change the data status are performed after the clean operation, the "found two representations of same Collection" exception may be reported.

Solution 2: Session. Refresh (object)

PS: when the object is not an existing data object in the database, the session cannot be used. refresh (object) because this method is used to retrieve the object from the hibernate session, if this object does not exist in the session, an error is returned. Therefore, when you use saveorupdate (object) you need to judge before.

Solution 3: Session. Merge (object)

PS: the built-in methods in hibernate are recommended.

2. Found two representations of same collection

Error cause: see 1.

Solution: Session. Merge (object)

The preceding two exceptions often occur in one-to-multiple ing and many-to-many ing.
 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shenzhen_mydream/archive/2009/03/18/4002692.aspx

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.