Hibernate(18)Hibernate的延迟加载是什么?

Hibernate的延迟加载(Lazy Loading)是一种性能优化技术,通过在需要访问数据时才实际加载它们,而不是在初始加载时将所有相关数据全部加载。这种方式可以减少不必要的数据查询和内存开销,特别是在处理大型数据集和复杂的对象关系时。

延迟加载的实现

在Hibernate中,延迟加载通常应用于集合及关联关系中,比如@OneToMany@ManyToOne@ManyToMany等。通过指定加载策略,可以控制关联实体的加载时机。

示例代码

下面是一个完整的示例,展示如何在Hibernate中使用延迟加载。

配置文件 hibernate.cfg.xml

xml 复制代码
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</property>
        <property name="hibernate.connection.username">your_username</property>
        <property name="hibernate.connection.password">your_password</property>

        <!-- Hibernate 属性配置 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 映射类配置 -->
        <mapping class="com.example.domain.Student"/>
        <mapping class="com.example.domain.Course"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil 类

java 复制代码
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // 从配置文件创建SessionFactory
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            // 记录启动失败的错误
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

实体类 Student 和 Course

Student 类
java 复制代码
package com.example.domain;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
    private int age;

    @OneToMany(mappedBy = "student", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Course> courses = new HashSet<>();

    public Student() {}

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // getters 和 setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Set<Course> getCourses() {
        return courses;
    }

    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }
}
Course 类
java 复制代码
package com.example.domain;

import javax.persistence.*;

@Entity
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "student_id")
    private Student student;

    public Course() {}

    public Course(String name, Student student) {
        this.name = name;
        this.student = student;
    }

    // getters 和 setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

延迟加载示例代码

下面的示例展示了如何在并发环境中使用延迟加载。

java 复制代码
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateLazyLoadingExample {
    public static void main(String[] args) {
        // 获取SessionFactory
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        // 插入示例数据
        insertSampleData(sessionFactory);

        // 示例: 在打开和关闭会话时访问延迟加载的集合
        Session session = null;
        Transaction transaction = null;

        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            // 获取Student对象
            Student student = session.get(Student.class, 1L);
            System.out.println("Student: " + student.getName() + ", Age: " + student.getAge());

            // 在会话关闭前访问延迟加载的集合
            System.out.println("Courses (before session close): " + student.getCourses().size());

            // 显式初始化延迟加载的集合
            Hibernate.initialize(student.getCourses());

            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

        // 在会话关闭后访问延迟加载的集合
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        try {
            Student student = session.get(Student.class, 1L);
            System.out.println("Student: " + student.getName() + ", Age: " + student.getAge());

            // 会话关闭后访问延迟加载的集合会引发 LazyInitializationException
            System.out.println("Courses (after session close): " + student.getCourses().size());

            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

        // 关闭SessionFactory
        sessionFactory.close();
    }

    private static void insertSampleData(SessionFactory sessionFactory) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            Student student = new Student("John Doe", 20);
            Course course1 = new Course("Mathematics", student);
            Course course2 = new Course("Physics", student);
            student.getCourses().add(course1);
            student.getCourses().add(course2);
            session.save(student);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}

详细解释

  1. 配置延迟加载 :在实体类的关联字段上添加fetch = FetchType.LAZY注解。

    java 复制代码
    @OneToMany(mappedBy = "student", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Course> courses = new HashSet<>();
  2. 示例场景

    • 在一个会话中获取Student对象及其关联的Course集合。
    • 在会话关闭前访问延迟加载的集合会触发集合的初始化。
    • 使用Hibernate.initialize方法显式地初始化延迟加载的集合。
    • 会话关闭后访问延迟加载的集合会引发LazyInitializationException异常。
  3. 事务管理:在事务中进行数据操作,并在操作完成后提交事务。如果操作失败,则回滚事务以确保数据一致性。

总结

延迟加载是一种有效的性能优化技术,通过在需要访问数据时才实际加载它们,可以减少不必要的数据查询和内存开销。在Hibernate中,通过fetch = FetchType.LAZY注解可以方便地实现延迟加载。理解并正确应用延迟加载,可以有效地提高应用的性能和资源利用率。

相关推荐
大鸡腿同学5 小时前
【成长类】《只有偏执狂才能生存》读书笔记:程序员的偏执型成长地图
后端
0xDevNull5 小时前
MySQL数据冷热分离详解
后端·mysql
AI袋鼠帝5 小时前
OpenClaw(龙虾)最强开源对手!Github 40K Star了,又一个爆火的Agent..
后端
新知图书7 小时前
搭建Spring Boot开发环境
java·spring boot·后端
宸津-代码粉碎机7 小时前
Spring Boot 4.0虚拟线程实战调优技巧,最大化发挥并发优势
java·人工智能·spring boot·后端·python
小码哥_常8 小时前
一个Starter搞定六种防护,Spring Boot API的超强护盾来了
后端
小村儿9 小时前
连载04-最重要的Skill---一起吃透 Claude Code,告别 AI coding 迷茫
前端·后端·ai编程
IT_陈寒10 小时前
Vite的alias配置把我整不会了,原来是这个坑
前端·人工智能·后端
gelald10 小时前
Spring Boot - 自动配置原理
java·spring boot·后端
希望永不加班11 小时前
SpringBoot 集成测试:@SpringBootTest 与 MockMvc
java·spring boot·后端·log4j·集成测试