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:
parent
b62aa9a661
commit
e5c5b89755
@ -16,7 +16,6 @@ public class HibernateUtil {
|
|||||||
if ("lazy".equals(fetchMethod)) {
|
if ("lazy".equals(fetchMethod)) {
|
||||||
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
|
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
||||||
}
|
}
|
||||||
// fetching.cfg.xml is used for this example
|
// fetching.cfg.xml is used for this example
|
||||||
@ -25,7 +24,6 @@ public class HibernateUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Session getHibernateSession() {
|
public static Session getHibernateSession() {
|
||||||
System.out.println("Loading eager");
|
|
||||||
SessionFactory sf = null;
|
SessionFactory sf = null;
|
||||||
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
||||||
final Session session = sf.openSession();
|
final Session session = sf.openSession();
|
||||||
|
@ -51,9 +51,7 @@ public class FetchingAppView {
|
|||||||
public void createTestData() {
|
public void createTestData() {
|
||||||
|
|
||||||
final Session session = HibernateUtil.getHibernateSession();
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
|
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
final User user1 = new User();
|
final User user1 = new User();
|
||||||
final User user2 = new User();
|
final User user2 = new User();
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<property name="hibernate.connection.username">root</property>
|
<property name="hibernate.connection.username">root</property>
|
||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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/User.hbm.xml" />
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
||||||
</session-factory>
|
</session-factory>
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<property name="hibernate.connection.username">root</property>
|
<property name="hibernate.connection.username">root</property>
|
||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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/OrderDetail.hbm.xml" />
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
|
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
|
||||||
</session-factory>
|
</session-factory>
|
||||||
|
@ -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()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
spring-hibernate4/src/test/resources/fetching.cfg.xml
Normal file
17
spring-hibernate4/src/test/resources/fetching.cfg.xml
Normal 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>
|
17
spring-hibernate4/src/test/resources/fetchingLazy.cfg.xml
Normal file
17
spring-hibernate4/src/test/resources/fetchingLazy.cfg.xml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user