I. Overview of the SESSION
The 1.Session interface is the most important interface that Hibernate provides to the application to manipulate the database, providing a basic way to save, update, delete, and load Java objects.
2. Understanding the session Cache
Purpose of using caching: Minimize the frequency of access to the database
(1) A series of Java collections are included in the implementation of the session interface, which forms the session cache. As long as the Session instance does not have an end-of-life cycle, objects stored in its cache will not end the life cycle
(2) when the Save () method of the session persists an object, the object is loaded into the cache, and in the future even if the object is no longer referenced in the program, the object remains in the lifecycle as long as the cache is not emptied. When an attempt is made to load () an object, it is determined whether the object exists in the cache, and then returns. Not in Query database
3. Clean up the cache
The Session has a cache, and objects in the cache are called persisted objects , which correspond to related records in the database. Session can update the database synchronously by executing the relevant SQL statements at some point in time, according to the changes in the objects in the cache, a process known as clean cache (flush)
4. By default, the Session cleans up the cache at the following point in time:
(1) When the application calls Transaction 's commit() method, the method cleans the cache (Session.flush ()) first, and then commits the transaction to the database (Tx.commit ( ))
(2) When an application performs some query operations, if the properties of persisted objects in the cache have changed, the cache is cleaned first to ensure that the query results reflect the latest state of the persisted object
(3) explicitly invoke the flush () method of the Session .
5.flush/commit/refresh/clear the difference between four different methods
Flush: An operation to clean up the cache (at which time the data in the cache is not lost), allowing the cache and database to synchronize some column SQL statements without committing the transaction;
commit: First call the Flush () method and then commit the transaction. means committing a transaction means permanently saving the database operation.
Reresh: Refresh, synchronize session and database, execute query, display the latest information of the database, update the state of local cached object.
Clear: Empties the cache, equivalent to List.removeall ();
二、一级 Cache
1. Unlike the level two cache, Hibernate has implemented a first level cache internally
2. Research on a number of issues to be studied at level cache
(1) The lifecycle of a first-level cache: created as the session is created and disappears as the session closes
(2) First-level caching is dependent on who exists: Session object
(3) How to store the data in the first level cache:,
(4) How to get data from a first-level cache
(5) How to synchronize the data in the cache to the database
(6) How's it going? Data in the database is synchronized to the first-level cache
(7) First-level caching features
(8) Purging an object from the first level cache
(9) Clear all the data in the first-level cache.
3.
"Java EE Learning Day 47th" "Hibernate learning Fourth Day" "Sesion" "First cache" "Lazy load"