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
|
@ -206,4 +206,4 @@ public class CompletableFutureTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,80 +3,71 @@ package com.baeldung.hibernate.fetching.model;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.sql.Date;
|
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;
|
|
||||||
|
|
||||||
public OrderDetail() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private Long orderId;
|
||||||
|
private Date orderDate;
|
||||||
|
private String orderDesc;
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
public OrderDetail(){
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,11 @@ public class User implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(final Long userId, final String userName, final String firstName, final String lastName) {
|
public User(final Long userId, final String userName, final String firstName, final String lastName) {
|
||||||
super();
|
super();
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,24 +1,27 @@
|
||||||
package com.baeldung.hibernate.fetching.util;
|
package com.baeldung.hibernate.fetching.util;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public static Session getHibernateSession(String fetchMethod) {
|
public static Session getHibernateSession(String fetchMethod) {
|
||||||
//two config files are there
|
//two config files are there
|
||||||
//one with lazy loading enabled
|
//one with lazy loading enabled
|
||||||
//another lazy = false
|
//another lazy = false
|
||||||
|
SessionFactory sf = null;
|
||||||
final String configFileName = "lazy".equals(fetchMethod) ?
|
if ("lazy".equals(fetchMethod)) {
|
||||||
"fetchingLazy.cfg.xml" :
|
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
|
||||||
"fetching.cfg.xml";
|
} else {
|
||||||
|
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
||||||
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() {
|
public static Session getHibernateSession() {
|
||||||
return new Configuration()
|
return new Configuration()
|
||||||
|
|
|
@ -18,93 +18,94 @@ import com.baeldung.hibernate.fetching.model.User;
|
||||||
|
|
||||||
public class FetchingAppView {
|
public class FetchingAppView {
|
||||||
|
|
||||||
public FetchingAppView() {
|
public FetchingAppView(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//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() {
|
||||||
|
|
||||||
}
|
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();
|
||||||
|
|
||||||
|
user1.setFirstName("Priyam");
|
||||||
|
user1.setLastName("Banerjee");
|
||||||
|
user1.setUserName("priyambanerjee");
|
||||||
|
session.save(user1);
|
||||||
|
|
||||||
|
user2.setFirstName("Navneeta");
|
||||||
|
user2.setLastName("Mukherjee");
|
||||||
|
user2.setUserName("nmukh");
|
||||||
|
session.save(user2);
|
||||||
|
|
||||||
|
user3.setFirstName("Molly");
|
||||||
|
user3.setLastName("Banerjee");
|
||||||
|
user3.setUserName("mollyb");
|
||||||
|
session.save(user3);
|
||||||
|
|
||||||
public boolean lazyLoaded() {
|
final OrderDetail order1 = new OrderDetail();
|
||||||
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
final OrderDetail order2 = new OrderDetail();
|
||||||
List<User> users = sessionLazy.createQuery("From User").list();
|
final OrderDetail order3 = new OrderDetail();
|
||||||
User userLazyLoaded = users.get(3);
|
final OrderDetail order4 = new OrderDetail();
|
||||||
//since data is lazyloaded so data won't be initialized
|
final OrderDetail order5 = new OrderDetail();
|
||||||
Set<OrderDetail> orderDetailSet = userLazyLoaded.getOrderDetail();
|
|
||||||
|
|
||||||
return (Hibernate.isInitialized(orderDetailSet));
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
public boolean eagerLoaded() {
|
tx.commit();
|
||||||
final Session sessionEager = HibernateUtil.getHibernateSession();
|
session.close();
|
||||||
//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));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//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 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);
|
|
||||||
|
|
||||||
user2.setFirstName("Navneeta");
|
|
||||||
user2.setLastName("Mukherjee");
|
|
||||||
user2.setUserName("nmukh");
|
|
||||||
session.save(user2);
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// session.saveOrUpdate(user1);
|
|
||||||
tx.commit();
|
|
||||||
session.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,43 @@
|
||||||
package com.baeldung.hibernate.fetching;
|
package com.baeldung.hibernate.fetching;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.hibernate.Hibernate;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.fetching.model.OrderDetail;
|
||||||
|
|
||||||
import com.baeldung.hibernate.fetching.view.FetchingAppView;
|
import com.baeldung.hibernate.fetching.view.FetchingAppView;
|
||||||
|
|
||||||
public class HibernateFetchingTest {
|
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
|
@Test
|
||||||
public void testLazyFetching() {
|
public void testLazyFetching() {
|
||||||
FetchingAppView fav = new FetchingAppView();
|
FetchingAppView fav = new FetchingAppView();
|
||||||
fav.createTestData();
|
Set<OrderDetail> orderDetalSetLazy = fav.lazyLoaded();
|
||||||
assertFalse(fav.lazyLoaded());
|
assertFalse(Hibernate.isInitialized(orderDetalSetLazy));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//testEagerFetching() tests the eager loading
|
||||||
|
//Since it eagerly loaded so orderDetalSetLazy would
|
||||||
|
//be initialized
|
||||||
@Test
|
@Test
|
||||||
public void testEagerFetching() {
|
public void testEagerFetching() {
|
||||||
FetchingAppView fav = new FetchingAppView();
|
FetchingAppView fav = new FetchingAppView();
|
||||||
assertTrue(fav.eagerLoaded());
|
Set<OrderDetail> orderDetalSetEager = fav.eagerLoaded();
|
||||||
|
assertTrue(Hibernate.isInitialized(orderDetalSetEager));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue