Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan

This commit is contained in:
AbdallahSawan 2021-02-16 02:43:49 +02:00
parent 35a9131a72
commit 9b1388c6e2
3 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,98 @@
package com.baeldung.hibernate.lazyCollection.model;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*;
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 = "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,84 @@
package com.baeldung.hibernate.lazyCollection.model;
import javax.persistence.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Long rank;
public Employee() {
}
public Employee(String name, Long rank, Branch mainBranch, Branch subBranch, Branch additionalBranch) {
this.name = name;
this.rank = rank;
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 Long getRank() {
return rank;
}
public void setRank(Long rank) {
this.rank = rank;
}
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,95 @@
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.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.PostConstruct;
@SpringBootTest
public class LazyCollectionTests {
private static SessionFactory sessionFactory;
private Session session;
Branch branch;
@PostConstruct
public 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);
setUp();
}
private void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
branch = new Branch("Main Branch");
session.save(branch);
Employee mainEmployee1 = new Employee("main employee 1",1L, branch, null, null);
Employee mainEmployee2 = new Employee("main employee 2", 2L, branch, null, null);
Employee mainEmployee3 = new Employee("main employee 3", 3L, branch, null, null);
session.save(mainEmployee1);
session.save(mainEmployee2);
session.save(mainEmployee3);
Employee subEmployee1 = new Employee("sub employee 1", 1L, null, branch, null);
Employee subEmployee2 = new Employee("sub employee 2", 2L, null, branch, null);
Employee subEmployee3 = new Employee("sub employee 3", 3L, null, branch, null);
session.save(subEmployee1);
session.save(subEmployee2);
session.save(subEmployee3);
Employee additionalEmployee1 = new Employee("additional employee 1", 1L, null, null, branch);
Employee additionalEmployee2 = new Employee("additional employee 2", 2L, null, null, branch);
Employee additionalEmployee3 = new Employee("additional employee 3", 3L, 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() {
Assertions.assertFalse(Hibernate.isInitialized(branch.getMainEmployees()));
}
@Test
public void testEagerFetching() {
Assertions.assertTrue(Hibernate.isInitialized(branch.getSubEmployees()));
}
@Test
public void testExtraFetching() {
Assertions.assertFalse(Hibernate.isInitialized(branch.getAdditionalEmployees()));
}
}