Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value was alread---------program error

Source: Internet
Author: User

Today we have this problem:

Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with the Session: ...

The reason for this is because the same object, such as a person in the seession saved a copy, and the addition of the other object, such as company, because of the association, from the database get a person, The person is the same person as the person in the seession, and the get-out person is set to the company, like this, Compnay.setperson (person). This error occurred while adding company.

On the Internet, when adding object, do one of these operations, that is, to merge the same object:

Object = Session.merge (object);

and then save

Session.save (object);

I tried it, and I can fix it.

But because of my operation, after the addition of a modified operation, the increase is successful, but at the time of the modification of a new error, so my Final solution is:

Use the procedure to judge, in taking person's time, the session has to take from the session, no more get out, and finally save company, there is no problem.

It is also possible to determine in action whether the object is already present in the sessiion, if it exists, do not go to the database to take it, or take it out to determine if the object and the session is consistent, if consistent with the session can be

Reproduced in: http://blog.csdn.net/xinmashang/article/category/1120644

Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value was alread---------program error

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.