Quickly learn about session in Hibernate

Session in Hibernate is a level-1 cache, which can be understood as a process level (thread bar) cache. It always exists during the running of the process (thread bar).

A session can be understood as an object that can operate a database, in which there are methods to operate the database.

In Java, cache usually refers to the memory space occupied by the properties of Java objects, usually some collection type properties. A series of Java collections are defined in the implementation class sessionimpl of the session interface, which constitute the session cache.

Generally speaking (my understanding of session): session is the intermediary (an object) between hibernate and DB. There are two things or functions in session

(1) Method of operating database

(2) There are some attributes (such as collections) in the session. These attributes are used to store the SQL language to be sent to the DB and cache the data found from the DB

Session cache

Session cache is composed of a series of Java collections. When an object is added to the session cache, the reference of the object is added to the Java collection. In the future, even if the reference variable in the application no longer references the object, as long as the session cache is not cleared, the object is always in the life cycle.

Session cache functions:

1) Reduce the frequency of accessing the database.

2) Ensure that the objects in the cache are synchronized with the relevant records in the database.

When session cleans up the cache:

1) When the commit () method of transaction is called, the commit () method first cleans up the cache (provided that flushmode. Commit / auto) and then commits the transaction to the database.

2) When the application calls find () or iterate () of session, if the properties of the persistent object in the cache change, the cache will be cleaned up first to ensure that the query result can reflect the latest state of the persistent object.

3) When the application shows that it calls the flush () method of session.

The point in time when session cleanup mode performs cache cleanup operations:

Session interface

Session interface is the main interface for hibernate to manipulate the database. It provides basic methods of saving, updating, deleting and querying.

1. Save (): add a temporary object to the cache to make it a persistent object

-->Select the primary key generator specified in the mapping file to assign a unique oid to the persistent object

-->An insert statement is planned to assemble the current attribute value of the parameter object into the insert statement, but the save () method does not execute the sqlinsert statement immediately. It will be executed only when the session cleans up the cache.

-->If the properties of the persistent object are modified after the save () method, the session will execute an additional sqlupdate statement when cleaning up 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, which is redundant

If a free state object is passed to the save () method, the session will treat it as a temporary object and insert a record into the database again, which does not meet the business requirements!

2. Update (): add the customer object back into the session cache to make it a persistent object.

--->An update statement is planned. It will be executed only when the cache is cleaned up, and the attribute values in the parameter object will be assembled into the update statement.

Note: update () transforms a free object into a persistent object.

As long as the free object is associated with a session through the update () method, even if any properties of the parameter object are not modified, the session will execute the update statement planned by the update method when cleaning up the cache.

3. Saveorupdate(): it includes the functions of both save() and update() methods. If the parameter passed in is a temporary object, call the save method. If the parameter passed in is a free object, call the update() method. If the parameter passed in is a persistent object, return directly.

4. Load() / get(): a persistent object will be loaded from the database according to 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 exception, and the get() method will return null

5. Delete(): used to delete the record corresponding to the parameter object from the database. If the incoming parameter is a persistent object, the session plans to execute a delete statement. If the incoming parameter is a free object, first associate the free object with the session to make it a persistent object, and then plan a delete statement to execute when cleaning up the cache.

6. Evict(): clears the persistent object specified by the parameter from the cache.

Applicable occasion: you do not want the session to update the database synchronously according to the state change 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 operation gives priority to JDBC

7. Clear (): clear all persistent objects in the cache.

summary

The above is to quickly understand all the contents of session in hibernate. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>