Code ví dụ Hibernate FetchType = EAGER (Eager loading)

Code ví dụ Hibernate FetchType = EAGER (Eager loading)

(Xem thêm: Hướng dẫn tự học Hibernate)

(Xem lại: Hibernate FetchType là gì? So sánh sự khác nhau giữa FetchType Lazy và Eager.)

(Xem thêm: Code ví dụ Hibernate FetchType = LAZY (Lazy loading))

Các công nghệ sử dụng:

Tạo database MySQL

CREATE SCHEMA `hibernate-fetch` ;

CREATE TABLE `hibernate-fetch`.`company` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `address` VARCHAR(255) NULL,
  PRIMARY KEY (`id`));

  
CREATE TABLE `hibernate-fetch`.`employee` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `company_id` INT NULL,
  `name` VARCHAR(45) NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `emp-company`
    FOREIGN KEY (`company_id`)
    REFERENCES `hibernate-fetch`.`company` (`id`));

Cấu trúc Project

Code ví dụ Hibernate FetchType = EAGER (Eager loading)

Các thư viện sử dụng

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>stackjava.com</groupId>
  <artifactId>HibernateFetchEager</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <hibernate.version>5.3.6.Final</hibernate.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate.version}</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.46</version>
    </dependency>
  </dependencies>


</project>

File cấu hình hibernate, kết nối tới MySQL

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
  <persistence-unit name="persistence">
    <description>Demo HibernateFetchEager</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://localhost:3306/hibernate-fetch" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="admin1234" />
      <property name="hibernate.show_sql" value="true" />
    </properties>
  </persistence-unit>
</persistence>

Trong ví dụ này mình sử dụng entityManager nên sẽ khai báo file persistence.xml nếu các bạn sử dụng hibernateSession thì có thể khai báo file hibernate.cfg.xml

Các File entity

@Entity
@Table(name = "company")
public class Company {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @Column(name = "name")
  private String name;

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "company", cascade = CascadeType.ALL)
  private List<Employee> listEmployee = new ArrayList<>();

  // getter - setter - constructor

  @Override
  public String toString() {
    return "Company [id=" + id + ", name=" + name + "]";
  }

}
@Entity
@Table(name = "employee")
public class Employee {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @Column(name = "name")
  private String name;

  @ManyToOne
  @JoinColumn(name = "company_id", nullable = false)
  private Company company;

  // getter - setter - constructor

  @Override
  public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", company=" + company + "]";
  }

}

File DAO

package stackjava.com.hibernatedemo.dao;

import javax.persistence.*;

import stackjava.com.hibernatedemo.entities.Company;

public class CompanyDAO {
  EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");

  public Company findById(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Company company = entityManager.find(Company.class, id);
    entityManager.close();
    return company;
  }

  public Company save(Company company) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(company);
    entityManager.getTransaction().commit();
    entityManager.close();
    return company;
  }

  public void close() {
    entityManagerFactory.close();
  }

}

Demo

Demo1

Đầu tiên insert đối tượng company và các đối tượng employee cho company đó:

package stackjava.com.hibernatedemo;

import stackjava.com.hibernatedemo.dao.CompanyDAO;
import stackjava.com.hibernatedemo.entities.Company;
import stackjava.com.hibernatedemo.entities.Employee;

public class DemoInsert {

  public static void main(String[] args) {
    CompanyDAO companyDAO = new CompanyDAO();
    Company company = new Company("Google");
    Employee emp1 = new Employee("Bat Man");
    Employee emp2 = new Employee("Super Man");
    Employee emp3 = new Employee("Iron Man");
    emp1.setCompany(company);
    emp2.setCompany(company);
    emp3.setCompany(company);
    company.getListEmployee().add(emp1);
    company.getListEmployee().add(emp2);
    company.getListEmployee().add(emp3);
    companyDAO.save(company);
    System.out.println("Saved!");
    System.out.println(company);
    System.out.println(emp1);
    System.out.println(emp2);
    System.out.println(emp3);
    companyDAO.close();
  }

}

Kết quả:

Code ví dụ Hibernate FetchType = EAGER (Eager loading)

Sau khi được save vào database, các đối tượng employee và company sẽ tự động được tạo id.

Demo2

Bây giờ select đối tượng company vừa tạo ở trên.

package stackjava.com.hibernatedemo;

import stackjava.com.hibernatedemo.dao.CompanyDAO;
import stackjava.com.hibernatedemo.entities.Company;
import stackjava.com.hibernatedemo.entities.Employee;

public class DemoSelect {

  public static void main(String[] args) {
    CompanyDAO companyDAO = new CompanyDAO();
    int companyId = 1;
    Company company = companyDAO.findById(companyId);
    if (company != null) {
      System.out.println(company);
      System.out.println(company.getListEmployee().size());
      for (Employee emp : company.getListEmployee()) {
        System.out.println(emp);
      }

    } else {
      System.out.println("Not found company with id = " + companyId);
    }
    companyDAO.close();
  }

}

Kết quả:

Code ví dụ Hibernate FetchType = EAGER (Eager loading)

Hibernate: select company0_.id as id1_0_0_, company0_.name as name2_0_0_, listemploy1_.company_id as company_3_1_1_, listemploy1_.id as id1_1_1_, listemploy1_.id as id1_1_2_, listemploy1_.company_id as company_3_1_2_, listemploy1_.name as name2_1_2_ from company company0_ left outer join employee listemploy1_ on company0_.id=listemploy1_.company_id where company0_.id=?
Company [id=1, name=Google]
3
Employee [id=1, name=Bat Man, company=Company [id=1, name=Google]]
Employee [id=2, name=Super Man, company=Company [id=1, name=Google]]
Employee [id=3, name=Iron Man, company=Company [id=1, name=Google]]

Các bạn để ý câu sql được sinh ra, khi query company nó query luôn cả các đối tượng employee liên quan.

Trong method findById của CompanyDAO sau khi lấy được company thì transaction đã đóng lại  (entityManager.close();) nhưng ở class DemoSelect.java vẫn có thể lấy được dữ liệu trong listEmployee

_____________________________

Okay, Done!

Download code ví dụ trên tại đây.

References:

https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/FetchType.html

stackjava.com