From 927b10e02e80bc74e376b562503d69534acff9c4 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 23 Feb 2021 15:51:13 +0200 Subject: [PATCH] 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);