Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hibernate first-level cache

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Hibernate level 1 cache". In daily operation, I believe many people have doubts about how to use Hibernate level 1 cache. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to answer your questions about "how to use Hibernate level 1 cache". Help! Next, please follow the small series to learn together!

1. Session level cache, which is bound to the session. Its life cycle is the same as the session. Session destruction, it is also destroyed at the same time; management of the first level cache, the first level cache can not be cancelled, with two methods to manage, clear(),evict()

2. Two sessions cannot share a first-level cache, because it will be accompanied by the creation and destruction of the session's life cycle;

3. Session cache is an entity-level cache, that is, it is only used when querying the object level.

Use HQL and SQL is query attribute level, is not the use of a cache! Remember!!!

4. iterate query uses cache, SQL and HQL statements for query Id will be issued, but entity query will not be issued.

After querying, it will put the corresponding entities into the cache. If some entity queries are available in the cache, they will query from the cache, but they will still issue SQL and HQL statements for the query id. If it is not in the cache, it will query the database, and then put the queried entities into the cache one by one, so there will be N+1 problems.

5. Difference between List () and iterate queries:

Use iterate,list to query entity object *N+1 problem, by default, use query.iterate query, there can be N+1 problem

The so-called N+1 is N+1 sql statements issued during the query. 1: First issue a sqlN:

According to the id list to the cache query, if the cache does not exist with the matching data, then the corresponding sql statement will be issued according to the id list and iterate difference?

list issues sql statements every time, list puts data into the cache without using the data in the cache

iterate: iterate uses cached data by default, but N+1 problems can occur if there is no data in the cache

6. Both Get () and load(),iterate methods use a level 1 cache,

7.hiberate3 session storage process is as follows:

For example, object

Session.save(object);

At this time, the data will not be put into the database, it will be put into the session cache first, there is no corresponding record in the database, session.flush(); SQL and HQL statements will be issued, there are corresponding records in the database,

However, the database cannot be found with select, which is related to the database transaction level.

Session.beginTrransaction().commit();

Things can be queried after submission.

Session.flush() statement But why not write it, because commit() calls flush() by default;

At this point, the study of "how to use Hibernate level 1 cache" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report