Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is Alread

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/zzzz3621/article/details/9776539

To see the meaning of the exception is already obvious, is that the primary key is not unique, in the last execution of the transaction SQL, the session cache contains multiple (>1) primary key objects.

Know that hibernate knows it has a first level cache, session level cache, in the execution of a transaction can manage persisted objects, at the end of the transaction execution of SQL, you can reduce the operation of the database.

To report this anomaly you have to take a closer look at your code, there must be a place to refer to the object has been different.

The following is a typical example:

[Java]View PlainCopy
    1. Public void Update (Object obj) {
    2. Fillobject (obj);
    3. Session.update (obj);
    4. }
    5. Public void Fillobject (Object obj) {
    6. Object obj2 = Session.load (Obj.getid ());
    7. Do set the properties of Obj2
    8. The wrong place
    9. obj = obj2;
    10. }


The right one should be

[HTML]View PlainCopy
    1. public void update (Object obj) {
    2. obj = fillobject (obj);
    3. Session.update (obj);
    4. }
    5. public object Fillobject (object obj) {
    6. Object obj2 = session.load (Obj.getid ());
    7. Do set the properties of Obj2
    8. return obj2;
    9. }

Error condition: When the Fillobject method is called, obj (Fillobject) is the state of 1, and the latter becomes 2. However, obj (update) always points to obj, and when the fillobject is executed, the session has stored the OBJ2 and then the obj is updated to the database with an error.

Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is Alread

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.