Skip to content

Commit e5c5b89

Browse files
pritambanerjee999pivovarit
authored andcommitted
Eager Loading vs Lazy Loading (eugenp#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
1 parent b62aa9a commit e5c5b89

12 files changed

Lines changed: 86 additions & 4 deletions

File tree

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static Session getHibernateSession(String fetchMethod) {
1616
if ("lazy".equals(fetchMethod)) {
1717
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
1818
} else {
19-
2019
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
2120
}
2221
// fetching.cfg.xml is used for this example
@@ -25,7 +24,6 @@ public static Session getHibernateSession(String fetchMethod) {
2524
}
2625

2726
public static Session getHibernateSession() {
28-
System.out.println("Loading eager");
2927
SessionFactory sf = null;
3028
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
3129
final Session session = sf.openSession();

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public boolean eagerLoaded(){
5151
public void createTestData() {
5252

5353
final Session session = HibernateUtil.getHibernateSession();
54-
5554
Transaction tx = null;
56-
5755
tx = session.beginTransaction();
5856
final User user1 = new User();
5957
final User user2 = new User();

‎spring-hibernate4/src/main/resources/fetching.cfg.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<property name="hibernate.connection.username">root</property>
1111
<property name="hibernate.connection.password">iamtheking</property>
1212
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
13+
<property name="show_sql">true</property>
1314
<mapping resource="com/baeldung/hibernate/fetching/model/User.hbm.xml" />
1415
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
1516
</session-factory>

‎spring-hibernate4/src/main/resources/fetchingLazy.cfg.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<property name="hibernate.connection.username">root</property>
1111
<property name="hibernate.connection.password">iamtheking</property>
1212
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
13+
<property name="show_sql">true</property>
1314
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
1415
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
1516
</session-factory>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.hibernate.criteria;
2+
3+
import org.junit.runner.JUnitCore;
4+
import org.junit.runner.Result;
5+
import org.junit.runner.notification.Failure;
6+
7+
public class HibernateCriteriaTestRunner {
8+
9+
public static void main(final String[] args) {
10+
Result result = JUnitCore.runClasses(HibernateCriteriaTestSuite.class);
11+
for (Failure failure : result.getFailures()) {
12+
13+
}
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.hibernate.criteria;
2+
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
6+
@RunWith(Suite.class)
7+
@Suite.SuiteClasses({ HibernateCriteriaTest.class })
8+
9+
public class HibernateCriteriaTestSuite {
10+
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.hibernate.fetching;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.hibernate.fetching.view.FetchingAppView;
8+
9+
public class HibernateFetchingTest {
10+
11+
@Test
12+
public void testLazyFetching() {
13+
FetchingAppView fav = new FetchingAppView();
14+
fav.createTestData();
15+
assertFalse(fav.lazyLoaded());
16+
}
17+
18+
@Test
19+
public void testEagerFetching() {
20+
FetchingAppView fav = new FetchingAppView();
21+
assertTrue(fav.eagerLoaded());
22+
}
23+
24+
}

spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml renamed to spring-hibernate4/src/test/resources/com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml

File renamed without changes.

spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.hbm.xml renamed to spring-hibernate4/src/test/resources/com/baeldung/hibernate/fetching/model/User.hbm.xml

File renamed without changes.

spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml renamed to spring-hibernate4/src/test/resources/com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml

File renamed without changes.

0 commit comments

Comments
 (0)