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

(10) Hibernate level 1 cache

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. Why use caching?

Objective: to reduce the number of visits to the database! So as to improve the execution efficiency of hibernate!

2. Cache classification in Hibernate

First-level cache, second-level cache

3. The concept of first-level cache

1) the first-level cache in Hibenate, also known as session cache, can reduce the number of database visits within the scope of session! Valid only in the session range! Session is closed, first-level cache is invalid!

2) when the save/saveOrUpdate/get/load/list/iterator method of session is called, the object is put into the cache of session.

3) the cache of Session is maintained by hibernate, and users cannot manipulate the cached content. If you want to manipulate the cached content, you must do so through the evit/clear method provided by hibernate.

4. The characteristics of Hibernate first-level cache

Only valid in the (current) session range, the action time is short, the effect is not particularly obvious!

Operate the database many times in a short time, the effect is more obvious!

5. Several methods and functions related to cache

Session.flush (); synchronize the primary cache with the database

Session.evict (object); clears the specified objects in the first-level cache

Session.clear (); clear all cached objects in the first-level cache

Under what circumstances do you use the above method?

For batch operations, use:

Session.flush (); / / synchronize with the database first

Session.clear (); / / clear the first-level cache again

6. Hibernate first-level cached interview questions

Interview question 1: will different session share cached data?

No, I won't.

Interview question 2: what is the difference between list and iterator query?

List (): if all records are queried at once, they will be put into the cache, but the data will not be fetched from the cache.

Iterator (): 1 query is made; N represents the total number of records. That is, a statement will be sent to query the primary key of all records (1 time), and then go to the database according to each primary key (N times)! It will be put into the cache and the data will be fetched from the cache!

User.java

Package com.rk.hibernate.i_status;public class User {private int userId; private String username; public int getUserId () {return userId;} public void setUserId (int userId) {this.userId = userId;} public String getUsername () {return username } public void setUsername (String username) {this.username = username;} @ Override public String toString () {return "User [userId=" + userId + ", username=" + username + "]";}}

User.hbm.xml

App2_Cache.java

Package com.rk.hibernate.i_status;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App2_Cache {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (User.class) .buildSessionFactory () } @ Test public void testGet () {Session session = sf.openSession (); session.beginTransaction (); / / query User U1 = (User) session.get (User.class, 2) / / first check whether there is any data in the cache. If so, do not query the database and get User U2 = (User) session.get (User.class, 2); System.out.println (U1); System.out.println (U2); System.out.println (U1 = = U2) directly from the cache / / true session.getTransaction () .commit (); session.close ();} @ Test public void testFlush () {Session session = sf.openSession (); session.beginTransaction () User u = (User) session.get (User.class, 2); u.setUsername ("ABC"); / / synchronize the cached data with the database session.flush (); / / if there is no such statement, execute update once After adding this statement, execute update twice. U.setUsername ("DEF"); session.getTransaction () .commit (); / / session.flush (); session.close ();} @ Test public void testClear () {Session session = sf.openSession (); session.beginTransaction () / / query User U1 = (User) session.get (User.class, 2); / / clear cache contents / / session.clear (); / / clear all session.evict (U1) / / clear the specified User U2 = (User) session.get (User.class, 2); session.getTransaction () .commit (); session.close ();} @ Test public void testSessionScope () {Session session1 = sf.openSession () Session1.beginTransaction (); Session session2 = sf.openSession (); session2.beginTransaction (); / / U1 into the cache of session1 User U1 = (User) session1.get (User.class, 2) / / U2 is put into session2's cache User U2 = (User) session2.get (User.class, 2); session1.getTransaction (). Commit (); session1.close (); session2.getTransaction (). Commit (); session2.close () }}

App3_List_Iterator.java

Package com.rk.hibernate.i_status;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App3_List_Iterator {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (User.class) .buildSessionFactory () The difference between list and iterator * 1. List method * 2. Iterator method * 3. Cache * @ throws Exception * / / 1. List method @ Test public void list () {Session session = sf.openSession (); session.beginTransaction (); / / HQL query Query Q = session.createQuery ("from User") / / list () method List list = q.list (); for (int I = 0; I

< list.size(); i++) { System.out.println(list.get(i)); } session.getTransaction().commit(); session.close(); } //2. iterator 方法 @Test public void iterator() throws Exception { Session session = sf.openSession(); session.beginTransaction(); // HQL查询 Query q = session.createQuery("from User "); // iterator()方法 Iterator it = q.iterate(); while (it.hasNext()) { // 得到当前迭代的每一个对象 User user = it.next(); System.out.println(user); } session.getTransaction().commit(); session.close(); } //3. 测试List缓存 @Test public void testList() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from User"); List list1 = q.list(); if(list1 != null && list1.size()>

0) {for (User u: list1) {System.out.println (u) }} System.out.println ("- -"); List list2 = q.list () If (list2! = null & & list2.size () > 0) {for (User u: list2) {System.out.println (u) }} System.out.println ("list1 = = list2?" + (list1 = = list2)); / / false for (int item0polii

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

Database

Wechat

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

12
Report