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

(11) Hibernate lazy loading

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. The difference between get and load methods?

Get: load in time and query the database as soon as you call the get method

Load: lazy loading is used by default, and the database is queried only when data is used.

2. Lazy loading (lazy)

2.1. Concept

Concept: query the database only when data is used, which is the lazy loading feature of hibernate.

Objective: to improve the efficiency of program execution!

2.2.2.The value of the lazy attribute

In Hibernate, lazy loads the value of the corresponding lazy property.

The sequence number value of the lazy attribute means that 1true uses lazy loading 2false to turn off lazy loading 3extra

Improve efficiency when lazy loading of collection properties (using set tags in configuration)

Sql that sends queries to the database only when the data is actually used

If you call the size () / isEmpty () method of the collection, it's just statistics, not really querying the data!

2.3. Lazy loading exception

(1) after Session is closed, you cannot load data using laziness!

(2) if you use lazy load data to report an error after session is closed: org.hibernate.LazyInitializationException: could not initialize proxy-no Session

(3) how to solve the problem that lazy data cannot be loaded after session is turned off?

Method 1: use the data first

Dept.getDeptName ()

Method 2: force the proxy object to initialize

Hibernate.initialize (dept)

Mode 3: turn off lazy loading

Set lazy=false in the .hbm.xml configuration file

Mode 4: after using the data, close session!

The difference between mode 1 and mode 4 is: mode 1 means that if you still want to use lazily loaded data after shutting down Session, you can first use the data that needs to be lazily loaded, so that those data will be loaded in advance; way 4 means that since lazily loaded data can no longer be accessed after Session is closed, it will find a way to delay the closure of Session.

3. Sample code

Department.java

Package com.rk.hibernate.j_lazy;import java.util.Set;public class Department {private int deptId; private String deptName; private Set emps; public int getDeptId () {return deptId;} public void setDeptId (int deptId) {this.deptId = deptId } public String getDeptName () {return deptName;} public void setDeptName (String deptName) {this.deptName = deptName;} public Set getEmps () {return emps } public void setEmps (Set emps) {this.emps = emps;} @ Override public String toString () {return "Deparment [deptId=" + deptId + ", deptName=" + deptName + "]";}}

Department.hbm.xml

Employee.java

Package com.rk.hibernate.j_lazy;public class Employee {private int empId; private String empName; private int salary; private Department dept; public int getEmpId () {return empId;} public void setEmpId (int empId) {this.empId = empId } public String getEmpName () {return empName;} public void setEmpName (String empName) {this.empName = empName;} public int getSalary () {return salary;} public void setSalary (int salary) {this.salary = salary } public Department getDept () {return dept;} public void setDept (Department dept) {this.dept = dept;} @ Override public String toString () {return "Employee [empId=" + empId + ", empName=" + empName + ", salary=" + salary + "]" }}

Employee.hbm.xml

App.java

Package com.rk.hibernate.j_lazy;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Department.class) .addClass (Employee.class) .buildSessionFactory () } / / 1. Primary key query, the difference between get and load / / here is lazy loading of the JavaBean class @ Test public void testClassGetLoad () {Session session = sf.openSession (); session.beginTransaction (); / / get: timely query / / Department dept = (Department) session.get (Department.class, 1) / / System.out.println (dept.getDeptName ()); / / System.out.println (dept.getEmps ()); / / load, default lazy loading, and send query sql statements to the database only when using data! Department dept = (Department) session.load (Department.class, 1); / / method 1: use data / / dept.getDeptName () first; / / method 2: force proxy object initialization / / Hibernate.initialize (dept) / / Mode 3: turn off lazy loading / / set lazy to false in the configuration file. Session.getTransaction () .commit (); session.close (); / / whether System.out.println (dept.getDeptName ()) can be used after session is closed;} / / 2. Here is lazy loading @ Test public void testProperty () {Session session = sf.openSession (); session.beginTransaction (); Department dept = (Department) session.get (Department.class, 1); System.out.println (dept.getDeptName ()) for attributes (foreign key associations) System.out.println ("- -"); System.out.println (dept.getEmps (). Size ()); System.out.println (dept.getEmps (). IsEmpty ()); System.out.println (dept.getEmps ()) Session.getTransaction (). Commit (); session.close ();}

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