Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Hibernate first-level cache

Shulou Source: shulou.com Published: 2022-06-02 08:00:05 09月22日 Update

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!

Tags: Cache query data statement database entity level problem object learning method two things cycle situation time more life help management Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Redmi Shulou Tech Info OPPO Reno