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

What is the Hibernate level 1 cache?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what is Hibernate first-level 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!

In learning Hibernate, we all know that because the life cycle of the first-level Hibernate cache is the same as the life cycle of the Session, it can also be understood that the first-level Hibernate cache is the session cache.

1. When making a query, there are several query methods that support first-level Hibernate caching, which are: load (), get (), iterate (). It should be noted that the iterate method only supports first-level caching for entity object queries. If you use iterate to query the relevant attributes in the object, it does not support first-level caching when querying.

1. Load () method.

Example:

Student s = (Student) session.load (Student.class, 1); System.out.println (s.getName ()); System.out.println ("-"); s = (Student) session.load (Student.class, 1); System.out.println (s.getName ())

Only one sql statement is issued. Although the load method is used twice here, * the load method loads the record with an id of 1, and a sql statement and the matching data are issued on line 3, so that the data is placed in the first-level cache, and the first-level cache is extracted from the first-level cache after continuing to use load on line 4.

2. Get () method.

Example:

Student s = (Student) session.get (Student.class, 1); System.out.println (s.getName ()); System.out.println ("-"); s = (Student) session.load (Student.class, 1); System.out.println (s.getName ())

As with load in 1, only one sql statement is issued.

3. The iterate () method queries entity objects.

Example:

Student student = (Student) session.createQuery ("from Student s where s.id=1") .iterate () .next (); System.out.println ("student.name=" + student.getName ()) / sql that queries id is issued, but sql that queries entity objects is not issued, because iterate uses the cache student = (Student) session.createQuery ("from Student s where s.id=1") .iterate () .next (); System.out.println ("student.name=" + student.getName ())

A total of three sql statements are issued, and lines 1 and 2 issue two statements, one is to query the sql of the entity object, and the other is to query the name attributes of the entity object. Due to the use of first-level cache, the entities queried in the previous lines 1 or 2 are stored in the first-level cache, so lines 5 and 6 only issue a sql for querying id using the data in the first-level cache.

4. The iterate () method queries the attributes of entity objects (first-level cache is not supported)

Example:

String name= (String) session.createQuery ("select s.name from Student s where s.id=1") .iterate () .next (); System.out.println ("student.name=" + name) / / iterate queries common attributes, first-level cache will not cache, so issuing sql / / first-level cache is name= (String) session.createQuery ("select s.name from Student s where s.id=1"). Iterate (). Next (); System.out.println ("student.name=" + name)

Because the iterate () method queries the attributes of the entity object, the first-level cache has no effect, so two sql statements are issued.

Second, the first-level cache exists, so in order to manage well more reasonably and improve the efficiency of the program, we usually clear the cache through the clear () and evict () methods, when we do not need to use the first-level cache or use it more efficiently.

If the one-time update or add a large number of cases, it is more necessary to manage the first-level cache.

Example:

For (int iTuno Bandi)

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