Eager Loading vs Lazy Loading (#600)

* BAEL-212
Contains:
1. Hibernate Criteria Query Classes
2. Hibernate Criteria Query Test

* Updating the config file and the HibernateUtil class

* Hibernate Criteria Queries Example

* Hibernate Fetching : Eager Loading vs Lazy Loading

* Hibernate Criteria Query files

* Hibernate Eager Loading and Lazy Loading Changes
This commit is contained in:
PRITAM BANERJEE 2016-08-12 00:06:19 -07:00 committed by Grzegorz Piwowarek
parent b62aa9a661
commit e5c5b89755
12 changed files with 86 additions and 4 deletions

View File

@ -16,7 +16,6 @@ public class HibernateUtil {
if ("lazy".equals(fetchMethod)) {
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
} else {
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
}
// fetching.cfg.xml is used for this example
@ -25,7 +24,6 @@ public class HibernateUtil {
}
public static Session getHibernateSession() {
System.out.println("Loading eager");
SessionFactory sf = null;
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
final Session session = sf.openSession();

View File

@ -51,9 +51,7 @@ public class FetchingAppView {
public void createTestData() {
final Session session = HibernateUtil.getHibernateSession();
Transaction tx = null;
tx = session.beginTransaction();
final User user1 = new User();
final User user2 = new User();

View File

@ -10,6 +10,7 @@
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/baeldung/hibernate/fetching/model/User.hbm.xml" />
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
</session-factory>

View File

@ -10,6 +10,7 @@
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
</session-factory>

View File

@ -0,0 +1,15 @@
package com.baeldung.hibernate.criteria;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class HibernateCriteriaTestRunner {
public static void main(final String[] args) {
Result result = JUnitCore.runClasses(HibernateCriteriaTestSuite.class);
for (Failure failure : result.getFailures()) {
}
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.hibernate.criteria;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ HibernateCriteriaTest.class })
public class HibernateCriteriaTestSuite {
}

View File

@ -0,0 +1,24 @@
package com.baeldung.hibernate.fetching;
import static org.junit.Assert.*;
import org.junit.Test;
import com.baeldung.hibernate.fetching.view.FetchingAppView;
public class HibernateFetchingTest {
@Test
public void testLazyFetching() {
FetchingAppView fav = new FetchingAppView();
fav.createTestData();
assertFalse(fav.lazyLoaded());
}
@Test
public void testEagerFetching() {
FetchingAppView fav = new FetchingAppView();
assertTrue(fav.eagerLoaded());
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/baeldung/hibernate/fetching/model/User.hbm.xml" />
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
</session-factory>
</hibernate-configuration>