Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan
This commit is contained in:
parent
9b1388c6e2
commit
13b16b915a
|
@ -9,13 +9,16 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.dialect.H2Dialect;
|
import org.hibernate.dialect.H2Dialect;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.After;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
public class LazyCollectionTests {
|
public class LazyCollectionTests {
|
||||||
|
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
@ -24,8 +27,8 @@ public class LazyCollectionTests {
|
||||||
|
|
||||||
Branch branch;
|
Branch branch;
|
||||||
|
|
||||||
@PostConstruct
|
@BeforeClass
|
||||||
public void beforeTests() {
|
public static void beforeTests() {
|
||||||
Configuration configuration = new Configuration().addAnnotatedClass(Branch.class).addAnnotatedClass(Employee.class)
|
Configuration configuration = new Configuration().addAnnotatedClass(Branch.class).addAnnotatedClass(Employee.class)
|
||||||
.setProperty("hibernate.dialect", H2Dialect.class.getName())
|
.setProperty("hibernate.dialect", H2Dialect.class.getName())
|
||||||
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
|
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
|
||||||
|
@ -37,10 +40,10 @@ public class LazyCollectionTests {
|
||||||
|
|
||||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
|
||||||
setUp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUp() {
|
@Before
|
||||||
|
public void setUp() {
|
||||||
session = sessionFactory.openSession();
|
session = sessionFactory.openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
|
||||||
|
@ -80,16 +83,27 @@ public class LazyCollectionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLazyFetching() {
|
public void testLazyFetching() {
|
||||||
Assertions.assertFalse(Hibernate.isInitialized(branch.getMainEmployees()));
|
Assert.assertFalse(Hibernate.isInitialized(branch.getMainEmployees()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEagerFetching() {
|
public void testEagerFetching() {
|
||||||
Assertions.assertTrue(Hibernate.isInitialized(branch.getSubEmployees()));
|
Assert.assertTrue(Hibernate.isInitialized(branch.getSubEmployees()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtraFetching() {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue