Merge pull request #10494 from abdallahsawan/BAEL-4121

Usage of the Hibernate @LazyCollection Annotation Article by Abdallah…
This commit is contained in:
Eric Martin 2021-04-08 19:31:13 -05:00 committed by GitHub
commit 8c00c69fde
3 changed files with 301 additions and 0 deletions

View File

@ -0,0 +1,103 @@
package com.baeldung.hibernate.lazycollection.model;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.OneToMany;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Branch() {
}
public Branch(String name) {
this.name = name;
}
@OneToMany(mappedBy = "mainBranch")
@LazyCollection(LazyCollectionOption.TRUE)
private List<Employee> mainEmployees;
@OneToMany(mappedBy = "subBranch")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Employee> subEmployees;
@OneToMany(mappedBy = "additionalBranch")
@LazyCollection(LazyCollectionOption.EXTRA)
@OrderColumn(name = "order_id")
private List<Employee> additionalEmployees;
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 List<Employee> getMainEmployees() {
return mainEmployees;
}
public void setMainEmployees(List<Employee> mainEmployees) {
this.mainEmployees = mainEmployees;
}
public List<Employee> getSubEmployees() {
return subEmployees;
}
public void setSubEmployees(List<Employee> subEmployees) {
this.subEmployees = subEmployees;
}
public List<Employee> getAdditionalEmployees() {
return additionalEmployees;
}
public void setAdditionalEmployees(List<Employee> additionalEmployees) {
this.additionalEmployees = additionalEmployees;
}
public void addMainEmployee(Employee employee) {
if (this.mainEmployees == null) {
this.mainEmployees = new ArrayList<>();
}
this.mainEmployees.add(employee);
}
public void addSubEmployee(Employee employee) {
if (this.subEmployees == null) {
this.subEmployees = new ArrayList<>();
}
this.subEmployees.add(employee);
}
public void addAdditionalEmployee(Employee employee) {
if (this.additionalEmployees == null) {
this.additionalEmployees = new ArrayList<>();
}
this.additionalEmployees.add(employee);
}
}

View File

@ -0,0 +1,88 @@
package com.baeldung.hibernate.lazycollection.model;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Entity;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
public Employee() {
}
public Employee(String name, Branch mainBranch, Branch subBranch, Branch additionalBranch) {
this.name = name;
this.mainBranch = mainBranch;
this.subBranch = subBranch;
this.additionalBranch = additionalBranch;
}
@ManyToOne
private Branch mainBranch;
@ManyToOne
private Branch subBranch;
@ManyToOne
private Branch additionalBranch;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Branch getMainBranch() {
return mainBranch;
}
public void setMainBranch(Branch mainBranch) {
this.mainBranch = mainBranch;
}
public Branch getSubBranch() {
return subBranch;
}
public void setSubBranch(Branch subBranch) {
this.subBranch = subBranch;
}
public Branch getAdditionalBranch() {
return additionalBranch;
}
public void setAdditionalBranch(Branch additionalBranch) {
this.additionalBranch = additionalBranch;
}
}

View File

@ -0,0 +1,110 @@
package com.baeldung.hibernate.lazycollection;
import com.baeldung.hibernate.lazycollection.model.Branch;
import com.baeldung.hibernate.lazycollection.model.Employee;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.annotation.PostConstruct;
public class LazyCollectionIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
Branch branch;
@BeforeClass
public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Branch.class)
.addAnnotatedClass(Employee.class).setProperty("hibernate.dialect", H2Dialect.class.getName())
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
branch = new Branch("Main Branch");
session.save(branch);
Employee mainEmployee1 = new Employee("main employee 1", branch, null, null);
Employee mainEmployee2 = new Employee("main employee 2", branch, null, null);
Employee mainEmployee3 = new Employee("main employee 3", branch, null, null);
session.save(mainEmployee1);
session.save(mainEmployee2);
session.save(mainEmployee3);
Employee subEmployee1 = new Employee("sub employee 1", null, branch, null);
Employee subEmployee2 = new Employee("sub employee 2", null, branch, null);
Employee subEmployee3 = new Employee("sub employee 3", null, branch, null);
session.save(subEmployee1);
session.save(subEmployee2);
session.save(subEmployee3);
Employee additionalEmployee1 = new Employee("additional employee 1", null, null, branch);
Employee additionalEmployee2 = new Employee("additional employee 2", null, null, branch);
Employee additionalEmployee3 = new Employee("additional employee 3", null, null, branch);
session.save(additionalEmployee1);
session.save(additionalEmployee2);
session.save(additionalEmployee3);
session.flush();
session.refresh(branch);
session.getTransaction().commit();
session.close();
}
@Test
public void testLazyFetching() {
Assert.assertFalse(Hibernate.isInitialized(branch.getMainEmployees()));
}
@Test
public void testEagerFetching() {
Assert.assertTrue(Hibernate.isInitialized(branch.getSubEmployees()));
}
@Test
public void testExtraFetching() {
Assert.assertFalse(Hibernate.isInitialized(branch.getAdditionalEmployees()));
}
@After
public void tearDown() {
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}