The Session in Hibernate is a first-level cache, which can be understood as a process-level (thread bar) cache, which exists during the process (thread bar) run.
The session can be understood as an object that can operate the database, and there are methods to operate the database in this object.
In Java, cache usually refers to the memory space occupied by the properties of Java objects, usually some collection type attributes. A series of Java collections are defined in the implementation class SessionImpl of the Session interface, and these Java collections form the Session cache.
In general (my understanding of Session): Session is the middleman (a object) between Hibernate and DB. There are two things or functions in the Session.
(1) Methods for operating the database
(2) There are some attributes (sets, etc.) in the Session, which use these attributes to store the SQL language to be sent to the DB, cache the data that has been found from the DB, etc.
Session Cache
The session cache is composed of a series of Java collections. When an object is added to the Session cache, the reference to this object is added to the Java collection. In the future, even if the reference variable in the application no longer refers to the object, as long as the Session cache is not cleared, the object will remain in its life cycle.
The role of Session cache:
1) Reduce the frequency of accessing the database.
2) Ensure that the objects in the cache are kept in synchronization with the relevant records in the database.
Time to clean cache in Session:
1) When the commit() method of Transaction is called, the commit() method first cleans the cache (provided that FlushMode.COMMIT/AUTO), and then submits transactions to the database.
2) When the application calls Session's find() or iterate(), if the properties of the persistent object in the cache change, the cache will be cleaned first to ensure that the query results can reflect the latest status of the persistent object.
3) When the application displays the flush() method of the Session is called.
The point in time when the Session cleanup mode performs the cleanup cache operation:
Session interface
The Session interface is the most important interface provided by Hibernate to applications for manipulating databases. It provides basic methods for saving, updating, deleting and querying.
1.save(): Add a temporary object to the cache, and it becomes a persistent object
-->Select the primary key generator specified in the mapping file to assign a unique OID to the persistent object
--> Plan an insert statement to assemble the current property value of the parameter object into the insert statement, but the save() method does not execute the SQLinsert statement immediately, and will only be executed when the Session cleanses the cache.
--> If the properties of the persistent object are modified after the save() method, the Session will execute additional SQLupdate statements when cleaning the cache.
Note: The save() method is used to persist a temporary object!
If you pass a persistent object to the save() method, no operation will be performed, redundant steps
If a free state object is passed to the save() method, the session will process it as a temporary object and insert a record into the database again, which does not meet the business needs!
2. update(): Re-add the Customer object to the Session cache and turn it into a persistent object.
--->Schedule an update statement, which will only be executed when the cache is cleaned, and the attribute values in the parameter object will be assembled into the update statement when it is executed.
Note: update() converts a free object into a persistent object.
As long as the free object is associated with a session through the update() method, the Session will execute the Update statement planned by the update method when cleaning the cache.
3.saveOrUpdate(): It contains both the functions of save() and update() methods. If the passed parameter is a temporary object, call the save method. If the parameter is a free object, call the update() method. If the passed parameter is a persistent object, return directly.
4.load()/get(): They will load a persistent object from the database based on the given OID. The difference is that when there is no record corresponding to the OID in the database, the load() method will throw an ObjectNotFoundException exception, and the get() method will return null.
5.delete(): is used to delete records corresponding to parameter objects from the database. If the passed parameter is a persistent object, the Session plans to execute a delete statement. If the passed parameter is a free object, first make the free object associated with the Session to make it a persistent object, and then plan a delete statement to execute when cleaning the cache.
6.evict(): Clear the persistent object specified by the parameter from the cache.
Applicable occasions: You do not want the Session to continue to synchronously update the database according to the state of the object.
In the case of batch update or batch deletion, when an object is updated or deleted, the memory occupied by the object is released in time. Of course, batch operations give priority to JDBC.
7.clear(): Clear all persistent objects in the cache.
Summarize
The above is a quick understanding of the entire content of the Session in Hibernate, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!