java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Hibernate 延迟加载

Hibernate的延迟加载的项目实践

作者:辞暮尔尔-烟火年年

Hibernate延迟加载(Lazy Loading)是一种性能优化技术,仅在需要时加载关联数据,减少初始查询和内存消耗,本文就来介绍一下Hibernate 延迟加载的实现,感兴趣的可以了解一下

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

延迟加载的实现

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

示例代码

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

配置文件hibernate.cfg.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 类

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 类

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 类

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;
    }
}

延迟加载示例代码

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

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注解。

    @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注解可以方便地实现延迟加载。理解并正确应用延迟加载,可以有效地提高应用的性能和资源利用率。

到此这篇关于Hibernate的延迟加载的项目实践的文章就介绍到这了,更多相关Hibernate 延迟加载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文