Hibernate Fetching Update (#607)

* 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

* Latest Changes on Fetching

* Fetching Changes

* Latest Changes

* Latest Changes

* Undoing the changes
This commit is contained in:
PRITAM BANERJEE 2016-08-16 22:19:34 -07:00 committed by Grzegorz Piwowarek
parent fe16518c17
commit 6c062ca19e
6 changed files with 198 additions and 185 deletions

View File

@ -12,7 +12,6 @@ public class OrderDetail implements Serializable {
private User user;
public OrderDetail(){
}
public OrderDetail(Date orderDate, String orderDesc) {
@ -24,27 +23,21 @@ public class OrderDetail implements Serializable {
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public String getOrderDesc() {
return orderDesc;
}
public void setOrderDesc(String orderDesc) {
this.orderDesc = orderDesc;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public int hashCode() {
final int prime = 31;
@ -52,7 +45,6 @@ public class OrderDetail implements Serializable {
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
@ -70,13 +62,12 @@ public class OrderDetail implements Serializable {
return true;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
}

View File

@ -22,7 +22,6 @@ public class User implements Serializable {
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
}
@Override

View File

@ -1,6 +1,7 @@
package com.baeldung.hibernate.fetching.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
@ -10,14 +11,16 @@ public class HibernateUtil {
//two config files are there
//one with lazy loading enabled
//another lazy = false
SessionFactory sf = null;
if ("lazy".equals(fetchMethod)) {
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
} else {
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
}
final String configFileName = "lazy".equals(fetchMethod) ?
"fetchingLazy.cfg.xml" :
"fetching.cfg.xml";
return new Configuration()
.configure(configFileName)
.buildSessionFactory().openSession();
// fetching.cfg.xml is used for this example
final Session session = sf.openSession();
return session;
}
public static Session getHibernateSession() {

View File

@ -22,25 +22,27 @@ public class FetchingAppView {
}
public boolean lazyLoaded() {
//lazily loaded
public Set<OrderDetail> lazyLoaded(){
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
List<User> users = sessionLazy.createQuery("From User").list();
User userLazyLoaded = users.get(3);
User userLazyLoaded = new User();
userLazyLoaded = users.get(3);
//since data is lazyloaded so data won't be initialized
Set<OrderDetail> orderDetailSet = userLazyLoaded.getOrderDetail();
return (Hibernate.isInitialized(orderDetailSet));
return (orderDetailSet);
}
public boolean eagerLoaded() {
//eagerly loaded
public Set<OrderDetail> eagerLoaded(){
final Session sessionEager = HibernateUtil.getHibernateSession();
//data should be loaded in the following line
//also note the queries generated
List<User> users =sessionEager.createQuery("From User").list();
User userEagerLoaded = users.get(3);
User userEagerLoaded = new User();
userEagerLoaded = users.get(3);
Set<OrderDetail> orderDetailSet = userEagerLoaded.getOrderDetail();
return (Hibernate.isInitialized(orderDetailSet));
return orderDetailSet;
}
@ -49,8 +51,8 @@ public class FetchingAppView {
public void createTestData() {
final Session session = HibernateUtil.getHibernateSession();
Transaction tx = session.beginTransaction();
Transaction tx = null;
tx = session.beginTransaction();
final User user1 = new User();
final User user2 = new User();
final User user3 = new User();
@ -96,15 +98,14 @@ public class FetchingAppView {
order5.setOrderDate(new Date(2014, 9, 11));
order5.setUser(user3);
session.saveOrUpdate(order1);
session.saveOrUpdate(order2);
session.saveOrUpdate(order3);
session.saveOrUpdate(order4);
session.saveOrUpdate(order5);
// session.saveOrUpdate(user1);
tx.commit();
session.close();
}
}

View File

@ -1,24 +1,43 @@
package com.baeldung.hibernate.fetching;
import static org.junit.Assert.*;
import java.util.Set;
import org.hibernate.Hibernate;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.hibernate.fetching.model.OrderDetail;
import com.baeldung.hibernate.fetching.view.FetchingAppView;
public class HibernateFetchingTest {
//this loads sample data in the database
@Before
public void addFecthingTestData(){
FetchingAppView fav = new FetchingAppView();
fav.createTestData();
}
//testLazyFetching() tests the lazy loading
//Since it lazily loaded so orderDetalSetLazy won't
//be initialized
@Test
public void testLazyFetching() {
FetchingAppView fav = new FetchingAppView();
fav.createTestData();
assertFalse(fav.lazyLoaded());
Set<OrderDetail> orderDetalSetLazy = fav.lazyLoaded();
assertFalse(Hibernate.isInitialized(orderDetalSetLazy));
}
//testEagerFetching() tests the eager loading
//Since it eagerly loaded so orderDetalSetLazy would
//be initialized
@Test
public void testEagerFetching() {
FetchingAppView fav = new FetchingAppView();
assertTrue(fav.eagerLoaded());
Set<OrderDetail> orderDetalSetEager = fav.eagerLoaded();
assertTrue(Hibernate.isInitialized(orderDetalSetEager));
}
}