In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand Hibernate Session cache". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
As a popular lightweight ORM framework, Hibernate began to be popular in China in 2003 and still occupies a dominant position in the persistence layer. Although rich and flexible HQL and object-oriented Criteria queries liberate programmers from the complex and tedious JDBC, as the framework of the persistence layer, its performance is the most important core issue. And the core of performance is caching, because I have been learning these recently, so I share what I have learned with you, hoping to have the effect of throwing a brick to attract jade.
Before we say that, let's talk about the three states of Hibernate entity objects:
1.Transient: Transient means that entity objects exist in memory and have nothing to do with the records in the database.
The 2.Persient:Persient object corresponds to a record in the database, and it can also be understood that if an entity object is associated with a session and is within the validity period of the corresponding session, it is in the Persient state.
3.Detached: the so-called Detached is the state after the session of the object in the Persient state is turned off.
What we usually call PO (persistent object) refers to the object in the Persient state, while VO (value object) refers to the object in the Transient and Detached state.
Let's start with CRUD saving: there are many ways to save in Hibernate. I'll mainly talk about the following:
The 1.save () method, when calling the save method, will first look for the saved object in the Hibernate Session cache. If the entity object is already in the Persient state, return it directly, and add the saved entity object to the Hibernate Session cache (the save method will not add the entity to the secondary cache). If the entity object is already in the Persient state, return directly, and add the saved entity object to the secondary cache.
The 2.saveOrUpdate () method, like the save method, first looks in the Hibernate Session cache to determine whether the object is in the saved state. If the object is in Persient, no operation is performed, the save operation is performed in Transient, and the Detached calls save to re-associate the object with the session.
It should be noted here that the batch operation should be carried out to flush the session in time to avoid OutOfMenoryError. Delete: delete I mainly say batch deletion, Hibernate must first load all qualified data into memory before batch deletion (this is a problem that all ORM frameworks must face, this is mainly to sort out the data in the current internal cache and secondary cache in order to preserve the consistency with the database) so that if the amount of operation data is too large, OutOfMenoryError will appear. Therefore, iterative deletion is recommended.
Modification: the modification is mainly about the update method. Update, like the previous save, should be looked up in the internal cache first. Two points should be paid attention to.
Calling update from an entity object in 1.Persient state has no effect, and the SQL of update will be executed in the session.flush () method.
Query: query is affected by cache * *. Let's talk about the following query 1.load and get: the difference between the two is that load will look up in the second-level cache, while get will skip the second-level cache to sql directly if it cannot find it in the internal cache.
The list () and iterate () methods of 2.createQuery () cannot actually use the cache, and list () only writes but does not read the cache, while ierate first looks up all eligible data id (first look up in the local cache) to get the corresponding records. The use of the iterate method * * means that the physical objects that are found already exist in the cache, otherwise the query performance is low and it is easy to produce Number1 phenomenon.
That's all for the content of "how to understand Hibernate Session cache". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.