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:
parent
fe16518c17
commit
6c062ca19e
|
@ -3,80 +3,71 @@ package com.baeldung.hibernate.fetching.model;
|
|||
import java.io.Serializable;
|
||||
import java.sql.Date;
|
||||
|
||||
public class OrderDetail implements Serializable {
|
||||
public class OrderDetail implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Long orderId;
|
||||
private Date orderDate;
|
||||
private String orderDesc;
|
||||
private User user;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Long orderId;
|
||||
private Date orderDate;
|
||||
private String orderDesc;
|
||||
private User user;
|
||||
|
||||
public OrderDetail() {
|
||||
public OrderDetail(){
|
||||
}
|
||||
|
||||
}
|
||||
public OrderDetail(Date orderDate, String orderDesc) {
|
||||
super();
|
||||
this.orderDate = orderDate;
|
||||
this.orderDesc = orderDesc;
|
||||
}
|
||||
|
||||
public OrderDetail(Date orderDate, String orderDesc) {
|
||||
super();
|
||||
this.orderDate = orderDate;
|
||||
this.orderDesc = orderDesc;
|
||||
}
|
||||
|
||||
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;
|
||||
int result = 1;
|
||||
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
OrderDetail other = (OrderDetail) obj;
|
||||
if (orderId == null) {
|
||||
if (other.orderId != null)
|
||||
return false;
|
||||
} else if (!orderId.equals(other.orderId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(Long orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
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;
|
||||
int result = 1;
|
||||
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
OrderDetail other = (OrderDetail) obj;
|
||||
if (orderId == null) {
|
||||
if (other.orderId != null)
|
||||
return false;
|
||||
} else if (!orderId.equals(other.orderId))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
public void setOrderId(Long orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -17,12 +17,11 @@ public class User implements Serializable {
|
|||
}
|
||||
|
||||
public User(final Long userId, final String userName, final String firstName, final String lastName) {
|
||||
super();
|
||||
this.userId = userId;
|
||||
this.userName = userName;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
|
||||
super();
|
||||
this.userId = userId;
|
||||
this.userName = userName;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,24 +1,27 @@
|
|||
package com.baeldung.hibernate.fetching.util;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Session getHibernateSession(String fetchMethod) {
|
||||
//two config files are there
|
||||
//one with lazy loading enabled
|
||||
//another lazy = false
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Session getHibernateSession(String fetchMethod) {
|
||||
//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() {
|
||||
return new Configuration()
|
||||
|
|
|
@ -18,93 +18,94 @@ import com.baeldung.hibernate.fetching.model.User;
|
|||
|
||||
public class FetchingAppView {
|
||||
|
||||
public FetchingAppView() {
|
||||
public FetchingAppView(){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lazyLoaded() {
|
||||
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
||||
List<User> users = sessionLazy.createQuery("From User").list();
|
||||
User userLazyLoaded = users.get(3);
|
||||
//since data is lazyloaded so data won't be initialized
|
||||
Set<OrderDetail> orderDetailSet = userLazyLoaded.getOrderDetail();
|
||||
//lazily loaded
|
||||
public Set<OrderDetail> lazyLoaded(){
|
||||
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
||||
List<User> users = sessionLazy.createQuery("From User").list();
|
||||
User userLazyLoaded = new User();
|
||||
userLazyLoaded = users.get(3);
|
||||
//since data is lazyloaded so data won't be initialized
|
||||
Set<OrderDetail> orderDetailSet = userLazyLoaded.getOrderDetail();
|
||||
return (orderDetailSet);
|
||||
}
|
||||
|
||||
return (Hibernate.isInitialized(orderDetailSet));
|
||||
}
|
||||
|
||||
public boolean 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);
|
||||
Set<OrderDetail> orderDetailSet = userEagerLoaded.getOrderDetail();
|
||||
|
||||
return (Hibernate.isInitialized(orderDetailSet));
|
||||
}
|
||||
//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 = new User();
|
||||
userEagerLoaded = users.get(3);
|
||||
Set<OrderDetail> orderDetailSet = userEagerLoaded.getOrderDetail();
|
||||
return orderDetailSet;
|
||||
}
|
||||
|
||||
|
||||
//creates test data
|
||||
//call this method to create the data in the database
|
||||
public void createTestData() {
|
||||
//creates test data
|
||||
//call this method to create the data in the database
|
||||
public void createTestData() {
|
||||
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
Transaction tx = null;
|
||||
tx = session.beginTransaction();
|
||||
final User user1 = new User();
|
||||
final User user2 = new User();
|
||||
final User user3 = new User();
|
||||
|
||||
final User user1 = new User();
|
||||
final User user2 = new User();
|
||||
final User user3 = new User();
|
||||
user1.setFirstName("Priyam");
|
||||
user1.setLastName("Banerjee");
|
||||
user1.setUserName("priyambanerjee");
|
||||
session.save(user1);
|
||||
|
||||
user1.setFirstName("Priyam");
|
||||
user1.setLastName("Banerjee");
|
||||
user1.setUserName("priyambanerjee");
|
||||
session.save(user1);
|
||||
user2.setFirstName("Navneeta");
|
||||
user2.setLastName("Mukherjee");
|
||||
user2.setUserName("nmukh");
|
||||
session.save(user2);
|
||||
|
||||
user2.setFirstName("Navneeta");
|
||||
user2.setLastName("Mukherjee");
|
||||
user2.setUserName("nmukh");
|
||||
session.save(user2);
|
||||
user3.setFirstName("Molly");
|
||||
user3.setLastName("Banerjee");
|
||||
user3.setUserName("mollyb");
|
||||
session.save(user3);
|
||||
|
||||
user3.setFirstName("Molly");
|
||||
user3.setLastName("Banerjee");
|
||||
user3.setUserName("mollyb");
|
||||
session.save(user3);
|
||||
final OrderDetail order1 = new OrderDetail();
|
||||
final OrderDetail order2 = new OrderDetail();
|
||||
final OrderDetail order3 = new OrderDetail();
|
||||
final OrderDetail order4 = new OrderDetail();
|
||||
final OrderDetail order5 = new OrderDetail();
|
||||
|
||||
final OrderDetail order1 = new OrderDetail();
|
||||
final OrderDetail order2 = new OrderDetail();
|
||||
final OrderDetail order3 = new OrderDetail();
|
||||
final OrderDetail order4 = new OrderDetail();
|
||||
final OrderDetail order5 = new OrderDetail();
|
||||
order1.setOrderDesc("First Order");
|
||||
order1.setOrderDate(new Date(2014, 10, 12));
|
||||
order1.setUser(user1);
|
||||
|
||||
order1.setOrderDesc("First Order");
|
||||
order1.setOrderDate(new Date(2014, 10, 12));
|
||||
order1.setUser(user1);
|
||||
order2.setOrderDesc("Second Order");
|
||||
order2.setOrderDate(new Date(2016, 10, 25));
|
||||
order2.setUser(user1);
|
||||
|
||||
order2.setOrderDesc("Second Order");
|
||||
order2.setOrderDate(new Date(2016, 10, 25));
|
||||
order2.setUser(user1);
|
||||
order3.setOrderDesc("Third Order");
|
||||
order3.setOrderDate(new Date(2015, 2, 17));
|
||||
order3.setUser(user2);
|
||||
|
||||
order3.setOrderDesc("Third Order");
|
||||
order3.setOrderDate(new Date(2015, 2, 17));
|
||||
order3.setUser(user2);
|
||||
order4.setOrderDesc("Fourth Order");
|
||||
order4.setOrderDate(new Date(2014, 10, 1));
|
||||
order4.setUser(user2);
|
||||
|
||||
order4.setOrderDesc("Fourth Order");
|
||||
order4.setOrderDate(new Date(2014, 10, 1));
|
||||
order4.setUser(user2);
|
||||
order5.setOrderDesc("Fifth Order");
|
||||
order5.setOrderDate(new Date(2014, 9, 11));
|
||||
order5.setUser(user3);
|
||||
|
||||
order5.setOrderDesc("Fifth Order");
|
||||
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);
|
||||
|
||||
tx.commit();
|
||||
session.close();
|
||||
|
||||
session.saveOrUpdate(order1);
|
||||
session.saveOrUpdate(order2);
|
||||
session.saveOrUpdate(order3);
|
||||
session.saveOrUpdate(order4);
|
||||
session.saveOrUpdate(order5);
|
||||
|
||||
// session.saveOrUpdate(user1);
|
||||
tx.commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue