1. session cache: 1 ). the implementation of the Session Interface contains a series of Java sets which constitute the Session cache. it is used to store the objects associated with the Session (there are many ways to associate the Session object. For example: session. get (Class, OID), session. update (), session
1. session cache: 1 ). the implementation of the Session Interface contains a series of Java sets which constitute the Session cache. it is used to store the objects associated with the Session (there are many ways to associate the Session object. For example: session. get (Class, OID), session. update (), session
1. Session cache:
1). The implementation of the Session Interface contains a series of Java sets.
,These Java sets constitute the Session cache.
Used for storage Session Associated object(There are many ways to associate Session objects. Example: session. get
(Class
,OID
), Session. update
(), Session. save
() ...). As long as the Session instance does not end its lifecycle
,If the cache is not cleared, the objects stored in the cache will not end the lifecycle. Session caching reduces the frequency of Hibernate applications accessing the database.
2). How to operate the Session cache (learn more ).
① If session. get is called
()When an object is loaded from the database, the object will be included in the Session cache.
News news
= (News
)Session. get
(News. class
,1
);
//Will SQL be sent to the database? SQL statement not sent
,Instead, get the object reference (snapshot) News news2 from the Session cache.
= (News
)Session. get
(News. class
,1
);
②. Clear Session
()Method to clear the Session cache
News news
= (News
)Session. get
(News. class
,1
);
//Clear session cache session. clear
(); //Will SQL be sent to the database? Yes! The Session cache is cleared! News news2
= (News
)Session. get
(News. class
,1
);
③. Session
Flush ()Method: Clear Cache
-To make the database record consistent with the object state in the Session cache, an SQL statement may be sent (if the database record is inconsistent with the object state in the Session, an SQL statement will be sent; otherwise, no SQL statement will be sent)
I. By default, the cache is cleared before the transaction is committed.
II. If the primary key generation method uses the underlying auto-increment mode of the database, the Session save
()Method, the cache is cleared and the INSERT statement is executed, instead of waiting until the transaction is committed. Hibernate requires that the object associated with the Session must have the OID corresponding to the data table record, which means that the save
()The primary key must have an OID, and the underlying auto-increment mode generates a primary key. You must execute INSERT to obtain the primary key value.
//If you use the MySQL underlying auto-increment method to generate a primary key, save
()The INSERT statement session. save will be sent.
(News
);System. out. println
(News. getId
());
III. When using HQL (Hibernate Query Language) to Query records, it does not pass the Session cache! Directly query the database, and the query results are the latest! Therefore, you must clear the cache session. save before performing HQL queries.
(News
);
//This will cause the cache to be cleared.
= (News
)Session. createQuery
("FROM News n WHERE n. id =? "
) .SetInteger
(0
,News. getId
()).UniqueResult
();
IV. commit
( ) And flush ()Differences between methods:
FlushExecute a series of SQL statements without committing transactions. The commit method first calls flush
()Method, and then commit the transaction. This means that the committed transaction will be permanently saved for database operations.
④. Refresh
()Method: Make sure that the state of the object in the Session cache is consistent with that in the database record. Therefore, a SELECT statement is forcibly sent. Note that the default isolation level of MySQL is read reptable. Therefore, you must set the transaction isolation level to see the effect of the experiment.
<! -- Set the transaction isolation level of Hibernate to read committed -->
<Property name
="Connection. isolation"
>2
Property>