Skip to content

Commit

Permalink
Refactor Eager Loading vs Lazy Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Piwowarek committed Aug 12, 2016
1 parent e5c5b89 commit 414821e
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 306 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.baeldung.main;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.baeldung.common.error.SpringHelloServletRegistrationBean;
import org.baeldung.common.resources.ExecutorServiceExitCodeGenerator;
import org.baeldung.controller.servlet.HelloWorldServlet;
Expand All @@ -17,6 +14,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@EnableAutoConfiguration
@ComponentScan({ "org.baeldung.common.error", "org.baeldung.common.error.controller", "org.baeldung.common.properties", "org.baeldung.common.resources", "org.baeldung.endpoints", "org.baeldung.service", "org.baeldung.monitor.jmx", "org.baeldung.service" })
Expand Down Expand Up @@ -55,28 +55,6 @@ public SpringHelloServletRegistrationBean servletRegistrationBean() {
return bean;
}

/* @Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory();
jettyContainer.setPort(9000);
jettyContainer.setContextPath("/springbootapp");
return jettyContainer;
}
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(io.undertow.Undertow.Builde builder) {
builder.addHttpListener(8080, "0.0.0.0");
}
});
return factory;
}*/

@Bean
@Autowired
public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,80 @@
import java.io.Serializable;
import java.sql.Date;

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;
}

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,90 @@
import java.util.Set;

public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long userId;

private static final long serialVersionUID = 1L;
private Long userId;
private String userName;
private String firstName;
private String lastName;
private Set<OrderDetail> orderDetail = new HashSet<OrderDetail>();

public User() {

}

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
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final User other = (User) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final User other = (User) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}

public Long getUserId() {
return userId;
return userId;
}

public void setUserId(final Long userId) {
this.userId = userId;
this.userId = userId;
}

public String getUserName() {
return userName;
return userName;
}

public void setUserName(final String userName) {
this.userName = userName;
this.userName = userName;
}

public String getFirstName() {
return firstName;
return firstName;
}

public void setFirstName(final String firstName) {
this.firstName = firstName;
this.firstName = firstName;
}

public String getLastName() {
return lastName;
return lastName;
}

public void setLastName(final String lastName) {
this.lastName = lastName;
this.lastName = lastName;
}

public Set<OrderDetail> getOrderDetail() {
return orderDetail;
}
public Set<OrderDetail> getOrderDetail() {
return orderDetail;
}

public void setOrderDetail(Set<OrderDetail> orderDetail) {
this.orderDetail = orderDetail;
}
public void setOrderDetail(Set<OrderDetail> orderDetail) {
this.orderDetail = orderDetail;
}



}
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package com.baeldung.hibernate.fetching.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory factory;

@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();
}
// fetching.cfg.xml is used for this example
final Session session = sf.openSession();
return session;
}
@SuppressWarnings("deprecation")
public static Session getHibernateSession(String fetchMethod) {
//two config files are there
//one with lazy loading enabled
//another lazy = false

public static Session getHibernateSession() {
SessionFactory sf = null;
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
final Session session = sf.openSession();
return session;
}
final String configFileName = "lazy".equals(fetchMethod) ?
"fetchingLazy.cfg.xml" :
"fetching.cfg.xml";

return new Configuration()
.configure(configFileName)
.buildSessionFactory().openSession();
}

public static Session getHibernateSession() {
return new Configuration()
.configure("fetching.cfg.xml")
.buildSessionFactory()
.openSession();
}

}
Loading

0 comments on commit 414821e

Please sign in to comment.