diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Branch.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Branch.java new file mode 100644 index 0000000000..0369bc96ae --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Branch.java @@ -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 mainEmployees; + + @OneToMany(mappedBy = "subBranch") + @LazyCollection(LazyCollectionOption.FALSE) + private List subEmployees; + + @OneToMany(mappedBy = "additionalBranch") + @LazyCollection(LazyCollectionOption.EXTRA) + @OrderColumn(name = "id") + private List 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 getMainEmployees() { + return mainEmployees; + } + + public void setMainEmployees(List mainEmployees) { + this.mainEmployees = mainEmployees; + } + + public List getSubEmployees() { + return subEmployees; + } + + public void setSubEmployees(List subEmployees) { + this.subEmployees = subEmployees; + } + + public List getAdditionalEmployees() { + return additionalEmployees; + } + + public void setAdditionalEmployees(List 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); + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Employee.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Employee.java new file mode 100644 index 0000000000..29c89bb94a --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Employee.java @@ -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; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTests.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTests.java new file mode 100644 index 0000000000..6f71959257 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTests.java @@ -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())); + } +}