From 9b1388c6e2957bd573de889f6a2c3ba7f3961666 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 02:43:49 +0200 Subject: [PATCH 1/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../com/baeldung/hibernate/model/Branch.java | 98 +++++++++++++++++++ .../baeldung/hibernate/model/Employee.java | 84 ++++++++++++++++ .../lazyCollection/LazyCollectionTests.java | 95 ++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Branch.java create mode 100644 persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Employee.java create mode 100644 persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTests.java 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())); + } +} From 13b16b915a080934fbd4c799a36723ee67170c07 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 10:40:43 +0200 Subject: [PATCH 2/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../lazyCollection/LazyCollectionTests.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) 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 index 6f71959257..14b35cfd7b 100644 --- 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 @@ -9,13 +9,16 @@ 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 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; -@SpringBootTest public class LazyCollectionTests { private static SessionFactory sessionFactory; @@ -24,8 +27,8 @@ public class LazyCollectionTests { Branch branch; - @PostConstruct - public void beforeTests() { + @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()) @@ -37,10 +40,10 @@ public class LazyCollectionTests { sessionFactory = configuration.buildSessionFactory(serviceRegistry); - setUp(); } - private void setUp() { + @Before + public void setUp() { session = sessionFactory.openSession(); session.beginTransaction(); @@ -80,16 +83,27 @@ public class LazyCollectionTests { @Test public void testLazyFetching() { - Assertions.assertFalse(Hibernate.isInitialized(branch.getMainEmployees())); + Assert.assertFalse(Hibernate.isInitialized(branch.getMainEmployees())); } @Test public void testEagerFetching() { - Assertions.assertTrue(Hibernate.isInitialized(branch.getSubEmployees())); + Assert.assertTrue(Hibernate.isInitialized(branch.getSubEmployees())); } @Test public void testExtraFetching() { - Assertions.assertFalse(Hibernate.isInitialized(branch.getAdditionalEmployees())); + Assert.assertFalse(Hibernate.isInitialized(branch.getAdditionalEmployees())); + } + + @After + public void tearDown() { + session.getTransaction().commit(); + session.close(); + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); } } From 34b1a35afbd54c5f19c0b4ab43a2afa0833524b3 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 11:31:04 +0200 Subject: [PATCH 3/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../{LazyCollectionTests.java => LazyCollectionTest.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/{LazyCollectionTests.java => LazyCollectionTest.java} (98%) 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/LazyCollectionTest.java similarity index 98% rename from persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTests.java rename to persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTest.java index 14b35cfd7b..9fa46fcc40 100644 --- 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/LazyCollectionTest.java @@ -19,7 +19,7 @@ import org.junit.Test; import javax.annotation.PostConstruct; -public class LazyCollectionTests { +public class LazyCollectionTest { private static SessionFactory sessionFactory; @@ -51,7 +51,7 @@ public class LazyCollectionTests { session.save(branch); - Employee mainEmployee1 = new Employee("main employee 1",1L, branch, null, null); + 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); From 5f2ea499261a452417738a5bcaf96178f3784102 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 11:41:27 +0200 Subject: [PATCH 4/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- ...zyCollectionTest.java => LazyCollectionIntegrationTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/{LazyCollectionTest.java => LazyCollectionIntegrationTest.java} (98%) diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java similarity index 98% rename from persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTest.java rename to persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java index 9fa46fcc40..d5eb9624b2 100644 --- a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionTest.java +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java @@ -19,7 +19,7 @@ import org.junit.Test; import javax.annotation.PostConstruct; -public class LazyCollectionTest { +public class LazyCollectionIntegrationTest { private static SessionFactory sessionFactory; From 5586a74acdd4f0026186c8dc08b5d70deaed96c4 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 15:47:38 +0200 Subject: [PATCH 5/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../{ => lazyCollection}/model/Branch.java | 0 .../{ => lazyCollection}/model/Employee.java | 13 ++++++------- .../LazyCollectionIntegrationTest.java | 18 +++++++++--------- 3 files changed, 15 insertions(+), 16 deletions(-) rename persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/{ => lazyCollection}/model/Branch.java (100%) rename persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/{ => lazyCollection}/model/Employee.java (83%) 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/lazyCollection/model/Branch.java similarity index 100% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Branch.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java 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/lazyCollection/model/Employee.java similarity index 83% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/model/Employee.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Employee.java index 29c89bb94a..05949334c9 100644 --- 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/lazyCollection/model/Employee.java @@ -11,15 +11,14 @@ public class Employee { private String name; - private Long rank; + private String address; public Employee() { } - public Employee(String name, Long rank, Branch mainBranch, Branch subBranch, Branch additionalBranch) { + public Employee(String name, Branch mainBranch, Branch subBranch, Branch additionalBranch) { this.name = name; - this.rank = rank; this.mainBranch = mainBranch; this.subBranch = subBranch; this.additionalBranch = additionalBranch; @@ -50,12 +49,12 @@ public class Employee { this.name = name; } - public Long getRank() { - return rank; + public String getAddress() { + return address; } - public void setRank(Long rank) { - this.rank = rank; + public void setAddress(String address) { + this.address = address; } public Branch getMainBranch() { diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java index d5eb9624b2..cec57a3dbf 100644 --- a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java @@ -51,25 +51,25 @@ public class LazyCollectionIntegrationTest { 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); + 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", 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); + 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", 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); + 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); From 342730c4a959034671a296904c28a3dca427758f Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 15:48:06 +0200 Subject: [PATCH 6/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../com/baeldung/hibernate/lazyCollection/model/Branch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java index 0369bc96ae..3f9549ca3a 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java @@ -32,7 +32,7 @@ public class Branch { @OneToMany(mappedBy = "additionalBranch") @LazyCollection(LazyCollectionOption.EXTRA) - @OrderColumn(name = "id") + @OrderColumn(name = "order_id") private List additionalEmployees; public Long getId() { From 927b10e02e80bc74e376b562503d69534acff9c4 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 23 Feb 2021 15:51:13 +0200 Subject: [PATCH 7/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../model/Branch.java | 9 ++++++-- .../model/Employee.java | 9 ++++++-- .../LazyCollectionIntegrationTest.java | 21 ++++++++++--------- 3 files changed, 25 insertions(+), 14 deletions(-) rename persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/{lazyCollection => lazycollection}/model/Branch.java (89%) rename persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/{lazyCollection => lazycollection}/model/Employee.java (84%) rename persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/{lazyCollection => lazycollection}/LazyCollectionIntegrationTest.java (80%) diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Branch.java similarity index 89% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Branch.java index 3f9549ca3a..de88647546 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Branch.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Branch.java @@ -1,9 +1,14 @@ -package com.baeldung.hibernate.lazyCollection.model; +package com.baeldung.hibernate.lazycollection.model; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; -import javax.persistence.*; +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; diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Employee.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java similarity index 84% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Employee.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java index 05949334c9..332e676439 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazyCollection/model/Employee.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java @@ -1,6 +1,11 @@ -package com.baeldung.hibernate.lazyCollection.model; +package com.baeldung.hibernate.lazycollection.model; -import javax.persistence.*; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OrderColumn; +import javax.persistence.OneToMany; +import javax.persistence.Entity; @Entity public class Employee { diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazycollection/LazyCollectionIntegrationTest.java similarity index 80% rename from persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java rename to persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazycollection/LazyCollectionIntegrationTest.java index cec57a3dbf..63ea1e1e3f 100644 --- a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazyCollection/LazyCollectionIntegrationTest.java +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/lazycollection/LazyCollectionIntegrationTest.java @@ -1,7 +1,7 @@ -package com.baeldung.hibernate.lazyCollection; +package com.baeldung.hibernate.lazycollection; -import com.baeldung.hibernate.lazyCollection.model.Branch; -import com.baeldung.hibernate.lazyCollection.model.Employee; +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; @@ -29,14 +29,15 @@ public class LazyCollectionIntegrationTest { @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"); + 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(); + .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); From 8a004c459a96f746e1c6bc5b134d86aa86706848 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 23 Feb 2021 15:56:18 +0200 Subject: [PATCH 8/8] Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan --- .../com/baeldung/hibernate/lazycollection/model/Employee.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java index 332e676439..831518a365 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/lazycollection/model/Employee.java @@ -4,7 +4,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OrderColumn; -import javax.persistence.OneToMany; +import javax.persistence.ManyToOne; import javax.persistence.Entity; @Entity