How does one obtain data in hibernate?

Source: Internet
Author: User
There are several different ways for hibernate to obtain data, and the effects of using it in combination with the cache are also different. In hibernate, how to use the cache is a matter of great concern, performance is directly involved.
Cache has three main aspects in hibernate: level-1 cache, level-2 cache, and query cache; level-1 cache corresponds to session-range cache in hibernate, that is, when the session is closed, the cache is cleared, and the first-level cache is not configurable in hibernate. The second-level cache corresponds to the sessionfactory cache in hibernate, generally, the life cycle of sessionfactory is the same as that of the application. Therefore, it can be regarded as process cache or cluster cache. The second-level cache can be configured in hibernate, you can use Class-cache to configure cache at the class granularity level (Class-cache is automatically updated when data in the class changes ), you can also use collection-cache to configure the cache at the collection granularity level. (Collection-cache is automatically updated only when elements are added to or deleted from the collection, that is, when the element value in the collection changes It will not be updated automatically), and the cache will naturally lead to concurrent access problems. At this time, the corresponding transaction isolation level adopted by the cache should be set based on the application, the concept of transaction isolation level is basically the same as that of the database. There is nothing to mention, ^ _ ^. The query cache is configurable in hibernate. It is disabled by default. You can set the cache. use _ query_cache is true to enable the query cache. According to the general implementation policies of cache, we can understand the three caches of hibernate. The implementation of cache is implemented by map of key/value, the same is true for the level-1, level-2, and query cache of hibernate. The keys used for level-1 and level-2 cache are the primary key ID of the Po, and the value is the Po instance object, the query cache uses the query condition, the query parameters, and the number of pages to be queried. There are two types of values, if select Po is used. in the Property Mode, the value is the entire result set. If the from method is used, the value is the primary key ID of each PO object in the obtained result set, which has obvious effect, memory saving, ^ _ ^
After a brief introduction to the cache of hibernate, the detailed usage of the cache is described based on Hibernate's data retrieval method. There are four common methods for getting data in hibernate: Session. load, session. get, query. list, query. iterator.
1. session. Load
In the execution session. during load, Hibernate first obtains the value corresponding to the ID from the level-1 cache of the current session. If it cannot be obtained, it will perform corresponding processing based on whether the object is configured with a level-2 cache, if the second-level cache is configured, the value corresponding to the ID is obtained from the second-level cache. If the value still cannot be obtained, the execution depends on whether the delayed loading is configured, if delayed loading is not configured, it is directly obtained from the database. when the data is obtained from the database, Hibernate will fill the level-1 cache and level-2 Cache accordingly, if delayed loading is configured, a proxy class is directly returned. The database query operation is performed only when the proxy class is called.
In this case, we can see that when the session is always open, pay attention to refresh the level-1 cache when appropriate, generally, when this object has one-way associated maintenance, you can use session in hibernate. clear, session. evict method to force refresh a level-1 cache.
The second-level cache is automatically updated when any data changes (new, update, or delete.
2. session. Get
When session. Get is executed, the difference from session. load is that when it is not obtained from the cache, the corresponding id value is obtained directly from the database.
3. query. List
In the execution of query. in list, Hibernate first checks whether the query cache is configured. If it is configured, the key is the query statement, query parameter, and paging condition value in the query cache, if no result is obtained, it will be obtained from the database. After the obtained result is obtained from the database, Hibernate will fill in the level-1, level-2, and query cache accordingly. For example, the obtained result set is a direct result set, the result is returned directly. If the obtained value is a bunch of IDS, the corresponding value (Session. load), and finally the result set is returned. In this case, the list may also cause n queries.
The query cache is automatically cleared when any data changes.
4. query. iterator
In the execution of query. iterator, and query. the difference in list is that query is obtained from the database. iterator initiates a statement such as select ID from to the database, that is, it first obtains the ID that meets the query conditions, and then performs iterator. the session is initiated again only when next is called. load call to obtain the actual data.
It can be seen that query. iterator is more efficient than query. List in the case of a second-level cache and variable query parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.