Usage of the Hibernate @LazyCollection Annotation Article by Abdallah Sawan

This commit is contained in:
AbdallahSawan 2021-02-23 15:51:13 +02:00
parent 342730c4a9
commit 927b10e02e
3 changed files with 25 additions and 14 deletions

View File

@ -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.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption; 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.ArrayList;
import java.util.List; import java.util.List;

View File

@ -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 @Entity
public class Employee { public class Employee {

View File

@ -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.Branch;
import com.baeldung.hibernate.lazyCollection.model.Employee; import com.baeldung.hibernate.lazycollection.model.Employee;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -29,14 +29,15 @@ public class LazyCollectionIntegrationTest {
@BeforeClass @BeforeClass
public static void beforeTests() { public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Branch.class).addAnnotatedClass(Employee.class) Configuration configuration = new Configuration().addAnnotatedClass(Branch.class)
.setProperty("hibernate.dialect", H2Dialect.class.getName()) .addAnnotatedClass(Employee.class).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())
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test") .setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "") .setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update"); .setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build(); .applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry); sessionFactory = configuration.buildSessionFactory(serviceRegistry);