hibernate first-level cache
1.Hibernate first-level cache is also called "Session cache" and "session-level cache".
2. When querying an entity from the database through Session, the entity will be stored in the memory. The next time you query the same entity, it will no longer be obtained from the database, but from the memory. This is cache
3. The life cycle of the first-level cache is the same as that of the Session. When the Session is destroyed, it is also destroyed.
4. The applicable range of data in the first-level cache is within the current session.
why (Why use Hibernate cache?)
Hibernate is a persistence layer framework that often accesses physical databases .
In order to reduce the frequency of application access to physical data sources, thereby improving the running performance of the application.
The data in the cache is a copy of the data in the physical data source. The application reads and writes data from the cache at runtime. At a specific moment or event, the data in the cache and the physical data source will be synchronized
#what( What is the principle of Hibernate cache? ) Hibernate cache includes two categories: Hibernate first-level cache and Hibernate second-level cache.
1.Hibernate first-level cache is also called "Session cache". Session built-in cannot be unloaded, and the Session cache is a transaction-scope cache (the life cycle of the Session object usually corresponds to a database transaction or an application transaction). In the first-level cache, each instance of the persistent class has a unique OID.2.Hibernate second-level cache is also called "SessionFactory cache".
1) Data that is rarely modified
2) Data that is not very important, occasional concurrent data is allowed
3) Data that will not be accessed concurrently
4) Constant data
Data that is not suitable for storage in the second level cache?
1) Data that is frequently modified
2) Data that is never allowed to be accessed concurrently, such as financial data, is never allowed to be accessed concurrently
3) Data shared with other applications.
API for managing the first-level cache
1.evict(), used to clear an object from the Session’s first-level cache. 2.clear(), used to clear all objects in the first-level cache.Qurey.list() and Qurey.iterate()
1.Qurey.list() query data , will not search from the first-level cache, directly send the SQL statement to the database, and retain the object returned by the query in the cache. 2.Qurey.iterate() does not search from the first-level cache, but directly sends sql to the database to query the id. When other attributes of the object need to be used, the object is first searched for in the cache according to the id. If there is no Then send a sql query to the database, so using this method alone to query will cause an N+1 problem (that is, sending N+1 statements to the database to query the information of N objects) 3. If this requirement exists: The same object needs to be accessed in two different sessions. Two sql statements or more need to be sent through Qurey.list(). In order to avoid setting up a second-level cache, Query.list() in the first session , in the second session, directly obtain it from the second-level cache through Qurey.iterate() iterationhibernate second-level cache (SessionFactory cache)
1. Add the configuration in hibernate.cfg.xml<!-- 开启二级缓存 --><property>true</property><!-- 二级缓存的提供类 在hibernate4.0版本以后我们都是配置这个属性来指定二级缓存的提供类--><property>org.hibernate.cache.ehcache.EhCacheRegionFactory</property><!-- 二级缓存配置文件的位置 --><property>ehcache.xml</property>2. For details about the configuration of ehcache.xml, see
Query cache (SessionFactory level cache)
1. Continue based on the above second-level cache Configuration2. Add configuration<property>true</property>
2. Add @Cacheable to the entity annotation
3. Call the setCacheable(true) method after the hql statement
4. Only when the HQL query statement is exactly the same, even the parameters The settings must be the same, and the query cache is effective at this time
hibernate's three states (transient (transient state), persistent (persistent state) and detached ( Offline))
The above is the detailed content of Introduction to hibernate caching mechanism. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools