Three states of Java objects
Persistent State:
Student stu=new Student ()
Session.save (Stu);
Both the session and the database have
Free State:
Stu.setid (1);
Session.close ();
There are no databases in the session
Instantaneous state:
Student stu=new Student ()
The session and DB are not
Three status graphs:
Use the New keyword to build an object that has a state of instantaneous state.
1 transient status to persistent state
When an object is saved using the Save () or Saveorupdate () method of the Session object, the state of the object is converted from the transient state to a persistent state.
Gets the object using the Get () or load () method of the Session object, which has a persistent state.
2 persistent state to instantaneous state
After the Delete () method of the Session object is executed, the object changes from the original persistent state to the instantaneous state because the object is not associated with any database data at this time.
3 Persistent state to Free State
Eat the evict (), clear (), or Close () methods of the Session object, and the object is moved from the previous persistent state to the Free State.
4 Free State to persistent state
Retrieves the session object, executes the update () or Saveorupdate () method of the Session object, and the object is moved from the Free State to the persistent state, which is associated with the session object again.
5 Free State transitions to instantaneous state
Executes the Delete () method of the Session object, and the object is shifted from the Free State to the instantaneous state.
When an object that is in a transient or Free State is no longer referenced by another object, it is processed by the Java virtual machine as a garbage collection mechanism.
Here's a dirty check
What is dirty data? Dirty data is not obsolete and useless data, but changes before and after the state of the data.
Dirty check: When a transaction commits, Hiberante detects the persistent state object in the session and determines whether the object's data has changed
Cache cleanup mechanism
When the properties of an object in the session cache change each time, the session does not immediately clean up the cache and executes the associated SQL UPDATE statement, but instead cleans up the cache at a specific point in time, allowing the session to merge several related SQL statements into a single SQL statement. Reduce the number of times a database is accessed, increasing the data access performance of your application.
By default, the session cleans up the cache at the following point in time.
- When the application calls Org.hibernate.Transaction's Commit () method. The Commit method first cleans up the cache before committing the transaction to the database. Hibernate schedules the time at which the cache is cleaned up at the end of the transaction, on the one hand because it reduces the frequency of access to the database and, on the one hand, the ability to lock the current transaction against the associated resources in the database as much as possible.
- When an application performs some query operations, if the properties of persisted objects in the cache have changed, the cache is cleaned up so that the session cache is synchronized with the database, ensuring that the query results return the correct data.
- When the application displays the flush () method that invokes the session.
Ways to update data:
Update () method
Saveorupdate () method
Merge (U) method:
User U=new user ();
User ul= (user) Session.merge (u);
1. Returns an object type, the type of objects returned UL will be associated with the session into a persistent object
2. After you call this method, the modified data after this object is not written to the database
3. The persisted object that the operation returns UL his data will be written to the database)
Three status and dirty checks in Hibernate