From 13b16b915a080934fbd4c799a36723ee67170c07 Mon Sep 17 00:00:00 2001 From: AbdallahSawan Date: Tue, 16 Feb 2021 10:40:43 +0200 Subject: [PATCH] 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(); } }