Hibernate development practice notes -- database issues updated when the object is set attributes

Source: Internet
Author: User

Hibernate development practice notes -- database issues updated when the object is set attributes

I have never found this problem before. Later, it was because the data in the database in my navigation bar had changed, and when the menu was generated, I updated the data in the database, which made some data values empty, at that time, I felt very strange. Later I carefully checked the log printed by log4j and found that there was an update action. When the code sets an object attribute from the database, the database is updated, but how does this problem occur?

Restatement
Set
 
   users = new HashSet
  
   ();User user = null;for(int i = 0; i < 10; i++){user = new User();user.setUserName("wy" + i);users.add(user);}Company company = userDao.getCompany();company.setUsers(users);
  
 

Like the above Code, company is just obtained from the database. Then I constructed a Set of User objects, then, when setUsers is used directly, the database update action occurs. Of course, this code is just an example of not the actual project code.
Where is the problem? Hibernate has three basic states: Free State, free state (temporary State), and persistent state. Persistent State: it is associated with the session and has data in the database. It is already persistent and cached in the database. My object here should be a persistent object. Then I directly constructed a set of user objects and performed the set operation on this object, so the data in the cache Session changes, then the database will change accordingly. Therefore, the update operation is executed.
How to solve the other two States: 1. Temporary State: the new object is not persisted in the database or in the session. 2. Free State: no longer in the Session, but it has been persistently stored in the database. How can this problem be solved? 1. if this object (company in this example) is not needed, you can directly create a Company object and then setUsers it because it is not the data in the Session, so it will not be synchronized to the database because the object attributes change. 2. if this object (company in the example) is used, you can convert it to the Free State before the set. In this case, several methods of session are used: close, clear, and evict. Close method: close the session. This object must be in the Free State, because the session has been closed. However, in actual development, the session will be used later, therefore, this method is feasible, but not necessarily useful. Clear method: clear all the objects in the session out of the cache. This method is a bit tricky, but after the session clears all the objects, it will naturally become free, this is not very good, I feel. Evict method: clears an object from the cache session. This method is a good implementation method and is recommended. Session. evict (Object obj.
Knowledge expansion: a deep understanding of how to switch between the three statuses of Hibernate
1. "Save" and "Update": When "save" is used, the Free State object is persistently stored in the database.
2. update, saveOrUpdate, and merge: saveOrUpdate are used to throw an exception when an object to be operated exists in the Session. If this object does not exist, save the object. If the object has a persistent identity attribute, update the object directly. For simple update, the program loads the object in a Session, closes the object, and then changes the object, during the update operation, another Session is opened to persistently store the object in the database. In the case of merge, when there is an object to be operated in the Session, the data of this object is directly copied to the persistent State object in the Session instead of throwing an exception, in this way, the object remains in the original state.
3. persist and save: both are used to save the temporary object to the database. However, persist does not guarantee that the object's primary key is created immediately and may be postponed to flush. But it will be created immediately when saving, so you can get the primary key value through the object during saving.
4. flush and update: During update, objects are mostly free objects, that is, objects in the database already exist, but objects in the Session are not updated to the database through update. Flush updates the persistent State data to the database.
5. lock is to convert a hosted object that has not been changed to a persistent state, but it cannot target the hosted objects after the delete operation.
Code instance analysis
User user = new User (); user. setName ("wy"); // currently it is still a temporary object Session = sessionFactory. openSession (); Tansaction tx = session. beginTansaction (); // It is still a temporary object session. save (user); // the user is in the persistent State tx. commit (); session. close (); // free

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.