Performance optimization is a big topic, ranging from program architecture optimization to database design to a specific algorithm or js optimization. Here, let's just talk about hibernat.
Performance optimization is a big topic, ranging from program architecture optimization to database design to a specific algorithm or js optimization. Here, let's just talk about hibernat.
Performance optimization is a big topic, ranging from program architecture optimization to database design to a specific algorithm or js optimization. Here we will only briefly discuss the performance optimization of hibernate.
When using Hibernate, you can improve the execution efficiency through correct methods and policies.
(1) capture policy
Fetching strategy: How does Hibernate obtain the associated Object Policy when an application needs to navigate between associations (in the Hibernate object graph. The capture policy can be declared in the metadata of the O/R ing, or in a specific HQL or condition Query (Criteria Query.
For example, single-ended proxy, set proxy, batch capture, etc.
Generally
* Fetch = "join". hibernate uses a select statement to join (Inline/external) to capture its associated objects or sets.
* Fetch = "subselect". In addition, a select statement is sent to capture the association set of all objects previously queried.
* You can use fetch to pre-capture Classes to solve the N + 1 problem. In hql, use join fetch and select s from Student s join fetch s. classes where s. id in (: ids)
* The batch-size attribute can be used to load entity classes in batches.
(2) Cache
Cache principle: data that is read much more than written is usually cached.
Hibernate sessions cache persistent data at the transaction level. Of course, it is also possible to configure the cluster or JVM-level (SessionFactory level) cache for each class (or set. You can even insert a cluster cache for it. Note that the cache never knows the possible modifications that other applications may make to the persistent repository (database), even if the cache data can be set to expire on a regular basis ).
1. Level 1 Cache
The lifecycle of a first-level cache is very short and consistent with that of a session. A first-level cache is also called a session-level cache or a transaction-level cache.
* When loading/get/iterate queries an object, it supports a level-1 cache.
* The first-level cache caches object images and does not cache attributes.
* Add large volumes of data
Session. beginTransaction ();
For (int I = 0; I <100000; I ++ ){
Student student = new Student ();
Student. setName ("James" + I );
Session. save (student );
// Every 20 updates
If (I % 20 = 0 ){
Session. flush ();
// Clear the cached content
Session. clear ();
}
}
Session. getTransaction (). commit ();