Org. hibernate. nonuniqueobjectexception and solution appear in the object saved by hibernate

Source: Internet
Author: User

If an object with the same OID (primary key) already exists in the session of hibernate save, an exception occurs. The details are as follows:

Exception in thread "Main" org. hibernate. nonuniqueobjectexception: a different object with the same Identifier value was already associated with the session: [COM. unmi. loandetail #1]
At org. hibernate. event. Def. abstractsaveeventlistener. abstrmsave (abstractsaveeventlistener. Java: 168)
At org. hibernate. event. Def. abstractsaveeventlistener. savewithgeneratedid (abstractsaveeventlistener. Java: 121)
At org. hibernate. event. Def. defaultsaveorupdateeventlistener. savewithgeneratedorrequestedid (defaultsaveorupdateeventlistener. Java: 187)
At org. hibernate. event. Def. defaultsaveeventlistener. savewithgeneratedorrequestedid (defaultsaveeventlistener. Java: 33)
At org. hibernate. event. Def. defaultsaveorupdateeventlistener. entityistransient (defaultsaveorupdateeventlistener. Java: 172)
At org. hibernate. event. Def. defaultsaveeventlistener. performsaveorupdate (defaultsaveeventlistener. Java: 27)
At org. hibernate. event. Def. defaultsaveorupdateeventlistener. onsaveorupdate (defaultsaveorupdateeventlistener. Java: 70)
At org. hibernate. impl. sessionimpl. firesave (sessionimpl. Java: 535)
At org. hibernate. impl. sessionimpl. Save (sessionimpl. Java: 523)
At org. hibernate. impl. sessionimpl. Save (sessionimpl. Java: 519)
At com. unmi. Test. Main (test. Java: 44)

The code for recreating the preceding errors is as follows (excluding the code Lines Controlled by transactions ):

  1. Session session = hibernatesessionfactory. getsession ();
  2. // Load the object whose oid is 1l and put it in the session cache
  3. Loandetail detail = (loandetail) Session. Get (loandetail.Class, 1l );
  4. // A new oid is also a temporary object of 1l
  5. Loandetail newdetail =NewLoandetail (1l );
  6. Ewdetail. setsubjectid (1000l );
  7. // Persists a temporary object and tries to store it in the session cache. An exception occurs due to OID conflict.
  8. Session. Save (newdetail );
  9. // If saveorupdate is executed, the preceding exception occurs.
  10. // Session. saveorupdate (newdetail );

Session session = hibernatesessionfactory. getsession (); </P> <p> // The object whose oid is 1l is loaded and stored in the session cache <br/> loandetail detail = (loandetail) session. get (loandetail. class, 1l); </P> <p> // a new oid is also a temporary object of 1l <br/> loandetail newdetail = new loandetail (1l ); <br/> newdetail. setsubjectid (1000l); </P> <p> // persists a temporary object and tries to store it in the session cache. An exception occurs due to OID conflicts. <br/> session. save (newdetail); </P> <p> // The preceding exception occurs when saveorupdate is executed. <br/> // session. saveorupdate (newdetail );
Solution:
1) if hibernate 2 is used, you need to attach a new property value to the Persistent Object in get/load/query, and then save/update/saveorupdate.
The above code is: you cannot create an existing OID object in a session, directly
Detail. setsubjectid (1000l );
Session. Save (detail );
Session. Save () is a persistent object that is converted into an update call.

2) use the merge method of hibernate 3. session. merge (newdetail) can be used to locate a persistent object in the session cache, assign the attributes of the new object to it, and then save the Persistent object in the original session.
If there are no objects in the session or database, the merge method can also help you insert records into the table, which is equivalent to the Save method.

The above is a simple example. In actual business, it may be difficult to figure out whether a new object exists in the session/database after some complicated operations. Therefore, the first method requires you to understand the state of each of your objects. The second method is more common in hibernate 3.

The following is a comment on the Session. Merge () method by hibernate javadoc:
Copy the state of the given object onto the Persistent object with the same identifier. if there is no persistent instance currently associated with the session, it will be loaded. return the persistent instance. if the given instance is unsaved, save a copy of and return it as a newly persistent instance. the given instance does not become associated with the session. this operation cascades to associated instances if the association is mapped with cascade = "merge ".

The semantics of this method are defined by JSR-220.

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.