hibernate a different object

http://blog.csdn.net/majian_1987/article/details/51726569

 

http://blog.csdn.net/lang_man_xing/article/details/7572964

 

Reprinted from http://blog.csdn.net/zhongxianyao/article/details/12294011 

 

        When talking about the difference between the two, it is necessary to talk about the three states of the hibernate object:

        Translated from Hibernate's reference documentation

        Hibernate defines and supports the following object states:
        Transient - Objects created by the new operator that have not been associated with a Hibernate Session are considered transient. Transient objects are not persisted to the database and are not assigned a persistent identifier. If a transient object is not referenced in the program, it will be destroyed by the garbage collector. Using Hibernate Session can make it persistent (Persistent) state. (Hibernate will automatically execute the necessary SQL statements)
        Persistent - Persistent instances have corresponding records in the database and have a persistent identifier. A persistent (Persistent) instance may be just saved, or just loaded, whichever, by definition, it exists in the associated Session scope. Hibernate detects any changes to objects in a persistent state and synchronizes the object data (state) with the database when the current unit of work is completed. Developers do not need to perform UPDATE manually. Changing an object from a persistent (Persistent) state to a transient (Transient) state also does not require a manual DELETE statement.
        Detached - After the Session associated with the Persistent object is closed, the object becomes Detached. The reference to the Detached object is still valid, and the object can continue to be modified. If a Detached object is re-associated with a new Session, it will become Persistent again (changes during Detached will be persisted to the database). This feature enables a programming model of a long-running unit of work that gives user think-time in the middle. We call this an application transaction, which is a unit of work from the user's point of view.

 

        Ok, let’s get to the point, understand the three states, let’s talk about when to use update and when to use merge

        update(): If you are sure that there is no persistent object with the same identifier (primary key) as the update operation in the session of the current session, then call update(). This sentence is a bit confusing to understand, but it will be clear with an example later.

        merge(): If you want to save the data to the database whenever you modify the data, then call merge().

 

        Example program fragment:

        There is a Person object with two properties, id and name

 

[java] view plain copy
 
 print?
  1. Session session = sessionFactory.openSession();   
  2. Transaction tx = session.beginTransaction();   
  3. Person person = (Person) session.get(Person.class"15");  
  4. tx.commit();   
  5. session.clear();  
  6. session.close();  
  7.   
  8. System.out.println(person.getId());  
  9. person.setName("jasper");  
  10. Session session2 = sessionFactory.openSession();   
  11. Transaction tx2 = session2.beginTransaction();   
  12. Person person2 = (Person) session2.get(Person.class , 15" );   //Do you feel that this code is a bit redundant, the problem lies here  
  13. //session2.update(person); //throws org.hibernate.NonUniqueObjectException  
  14. //session2.saveOrUpdate(person); //throws org.hibernate.NonUniqueObjectException  
  15. session2.merge(person);   //Successful execution  
  16. tx2.commit();   
  17. session2.close();  

 

        When calling the update or saveOrUpdate operation, the exception information "org.hibernate.NonUniqueObjectException" was thrown, which is not a "unique" object. Why is this error thrown? The above person2 calls the get method, so person2 is a persistent object. Any modification made to person2 when the session is not closed will be saved to the database; object, then you need to get in touch with the database again, you need to get data from the database, hibernate will find out whether there is a current copy of this object, there just happens to be person2 (but I don’t know if person2 has been modified and saved to the database), instead of saving this may be Useless data, it is better to report an error directly. Calling update here is similar to saveOrUpdate.

 

        There is also a difference between update and merge: update is to directly issue the "sql update" statement, while merge will issue the "sql select" statement first. If there is no query result, the save operation is performed. If the relevant result is queried, the judgment is made. Whether the corresponding field has changed, if so, issue a "sql update" statement, otherwise do nothing. In this respect, merge is the same as saveOrUpdate.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326548048&siteId=291194637