In-depth Session
Session Overview
When Session interfaces are used, Hibernate provides the application with the primary interface for database operations. It provides basic methods for saving, updating, deleting, and loading Java objects.
Understanding Session caching,
The implementation of the session excuse contains Java collections of some columns, which constitute the Session cache, as long as the Session strength does not end the lifecycle, objects stored in the cache do not end the lifecycle. When the save () method of the session persists an object, the object is loaded into the cache, even if the object is not referenced in the program, as long as the cache is not cleared, the object is still in the lifecycle. When a graph load () object is created, it determines whether the object exists in the cache. If yes, it is returned. Database not being queried
Clear Session cache
Session has a cache,
Objects in the cache are called persistent objects.It corresponds to the relevant records in the database. sessions can execute related SQL statements according to the changes of objects in the cache at certain time points to synchronously update the database, this process is called flush. By default, the Session clears the cache at the following time points: when the application calls
Transaction commit ()This method first clears the cache (session. flush (), and submit the transaction (tx. commit () When an application performs some query operations, if the attributes of the persistence object in the cache have changed, the cache is cleared first, to ensure that the query results can reflect the latest status of the Persistent object, the difference between the flush () method of the Session is explicitly called: flush: to clear the cache (the data cached at this time is not lost, let the cache and database execute a series of SQL statements synchronously, but do not commit the transaction; commit: First call the flush () method, and then commit the transaction, this means that the operations committed to the database will be permanently saved in reresh: refresh, synchronize sessions with the database, execute queries, and display the latest database information, update the object status of the ontology cache clear: clear the cache, equivalent to list. removeAll ();
Use Session cache to read data of persistent objects
Cache function:
Reduce the frequency of accessing the database. Ensure that the objects in the cache are synchronized with related records in the database.
Session to Perform Batch operations <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release + release + sG/release + bLFxNyxo9ak1 + release/release + CjxwPsDgvLax8Lzsy/release + yc/release/nS1KOss9a + w7uvttTP87a8u + weight/eyu7W9ttTP88qxu + weight + m/9 s/weight/MO7sdjSqsHLoaM8L3A + weight/weight + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140509/20140509091717150.jpg" alt = "\">
Fetch (Default value: select) |
Lazy (The default value is true) |
Policy |
Join |
False |
Use urgent left Outer Join search. |
Join |
True |
Use urgent left Outer Join search. |
Join |
Extra |
Use urgent left Outer Join search. |
Select |
False |
Use Instant Search |
Select |
True |
Latency Retrieval |
Select |
Extra |
Latency Retrieval C. getOrders (). size () execute select count (id) from orders where customer_id =? For (Order o: set) {o. getOrderNumber: Select customer_id, id, order_number, price from orders where customer_id =? |
Subselect |
False/true/extra It is also divided into three situations |
Nested subquery (when multiple customer objects are retrieved) the Lazy attribute determines the search policy) Select customer_id, order_number, price from orders where customer_id In (select id from MERs mers) |
Search Policy |
Advantages |
Disadvantages |
Applicable scenarios with priority |
Search now |
Completely transparent to the application. no matter whether the object is in a persistent or free state, the application can navigate from an object to the associated object. |
(1) Multiple select statements (2) It may load objects that the application does not need to access, wasting a lot of memory space. |
(1) class level (2) objects that the application needs to access immediately (3) The second-level cache is used. |
Delayed search |
It is up to the application to determine which objects need to be loaded, so as to avoid executing redundant select statements and loading objects that the application does not need to access. Therefore, it can improve retrieval performance and save memory space. |
If an application wants to access a free proxy instance, it must ensure that it has been initialized during persistence. |
(1) one-to-many or many-to-many Association (2) objects that the application does not need to access immediately or are not accessible at all |
Urgent left Outer Join search |
(1) It is completely transparent to the application. no matter whether the object is in a persistent or free state, you can navigate from one object to another. (2) When an external connection is used, fewer select statements are used. |
(1) It may load objects that the application does not need to access, wasting memory. (2) Complex Database Table connections also affect retrieval performance. |
(1) many-to-one (2) objects to be accessed immediately (3) The database has good table connection performance. |
Hibernate Retrieval Method
HQL (Hibernate Query Language)
HQL (Hibernate Query Language) is an Object-Oriented Query Language, which is similar to the SQL Query Language. Among the various retrieval methods provided by Hibernate, HQL is the most widely used retrieval method, it has the following functions: You can set various query conditions in a query statement to support projection query. That is, only partial attributes of the retrieved object support paging query, and connection query supports grouping query, the keyword having and group by can be used to provide cohesion clustering functions, such as sum (), min (), max (); ability to call User-Defined SQL functions or standard SQL functions. Support for subqueries. Support for dynamic parameter binding.
OID Retrieval Method
Retrieve Objects Based on their OID
QBC Retrieval Method
Use the QBC (Query By Criteria) API to retrieve objects. This API encapsulates string-Based Query statements and provides a more object-oriented Query interface.
Simple Query
Use alias
Sort query results
Paging Query
SetFirstResult (int firstResult); specifies the object from which to start retrieval. The firstResult parameter indicates the index position of the object in the query result. The starting value of the index position is 0. By default, query starts from the first object in the Query result.
SetMaxResult (int maxResults); sets the maximum number of objects retrieved at a time. By default, the Query and Criteria interfaces retrieve all objects in the Query results.
Hibernate Retrieval Method
Phrase |
Description |
Expression. eq |
Equal to = |
Expression. allEq |
Use Map and key/value for multiple equal judgments |
Expression. gt |
Greater than> |
Expression. ge |
Greater than or equal to> = |
Expression. lt |
Less than < |
Expression. le |
Less than or equal to <= |
Expression. |
Corresponding SQL between clause |
Expression. like |
Corresponding to the like clause of SQL |
Expression. in |
Corresponding SQL in Clause |
Expression. and |
And relationship |
Expression. or |
Or relationship |
Expression. sqlRestriction |
SQL limit Query |
Expression. asc () |
Sort by input fields in ascending order |
Expression. desc () |
Sort by input fields in descending order |
Various operations supported by HQL and QBC
Operation Type |
HQL Operator |
QBC Calculation Method |
Description |
Comparison |
= |
Express. eq () |
|
|
<> |
Express. not (Express. eq ()) |
|
|
> = |
Express. ge () |
|
|
< |
Express. lt () |
|
|
<= |
Express. le () |
|
|
Is null |
Express. isNull () |
|
|
Is not null |
Express. isNotNull () |
|
Range Operators |
In |
Express. in () |
|
|
Not in |
Express. not (Express. in ()) |
|
|
Between |
Express. () |
|
|
Not |
Express. not (Express. ()) |
|
Comparison |
= |
Express. eq () |
|
|
<> |
Express. not (Express. eq ()) |
|
|
> = |
Express. ge () |
|
|
< |
Express. lt () |
|
|
<= |
Express. le () |
|
|
Is null |
Express. isNull () |
|
|
Is not null |
Express. isNotNull () |
|
Range Operators |
In |
Express. in () |
|
|
Not in |
Express. not (Express. in ()) |
|
|
Between |
Express. () |
|
|
Not |
Express. not (Express. ()) |
|
Logic |
And |
Expression. and () | Expression. conjunction () |
|
|
Or |
Expression. or () | Expression. disjunction () |
|
|
Not |
Expression. not () |
|
Level 2 Cache
Concept of Cache
A very general concept in the computer field. It is between an application and a permanent data storage source (such as a file or database on a hard disk). It is used to reduce the frequency of applications directly reading and writing permanent data inventory storage sources, this improves the Running Performance of applications. The cached data is a copy of the data in the data storage source. Normally, the cache memory is used for physical media.
Hibernate provides two levels of Cache
The Session-level cache is a cache within the transaction scope. This level of cache is managed by hibernate. In general, it is necessary to intervene in the SessionFactory level cache, which is a process-specific cache.
Enable secondary cache Conditions
Rarely modified. Very few system modules use data that is not private. It is a shared second-level cache vendor.