Skip to content

Commit 414821e

Browse files
author
Grzegorz Piwowarek
committed
Refactor Eager Loading vs Lazy Loading
1 parent e5c5b89 commit 414821e

6 files changed

Lines changed: 289 additions & 306 deletions

File tree

‎spring-boot/src/main/java/org/baeldung/main/SpringBootApplication.java‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.baeldung.main;
22

3-
import java.util.concurrent.ExecutorService;
4-
import java.util.concurrent.Executors;
5-
63
import org.baeldung.common.error.SpringHelloServletRegistrationBean;
74
import org.baeldung.common.resources.ExecutorServiceExitCodeGenerator;
85
import org.baeldung.controller.servlet.HelloWorldServlet;
@@ -17,6 +14,9 @@
1714
import org.springframework.web.bind.annotation.RequestMapping;
1815
import org.springframework.web.bind.annotation.RestController;
1916

17+
import java.util.concurrent.ExecutorService;
18+
import java.util.concurrent.Executors;
19+
2020
@RestController
2121
@EnableAutoConfiguration
2222
@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" })
@@ -55,28 +55,6 @@ public SpringHelloServletRegistrationBean servletRegistrationBean() {
5555
return bean;
5656
}
5757

58-
/* @Bean
59-
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
60-
JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory();
61-
jettyContainer.setPort(9000);
62-
jettyContainer.setContextPath("/springbootapp");
63-
return jettyContainer;
64-
}
65-
66-
@Bean
67-
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
68-
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
69-
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
70-
71-
@Override
72-
public void customize(io.undertow.Undertow.Builde builder) {
73-
builder.addHttpListener(8080, "0.0.0.0");
74-
}
75-
76-
});
77-
return factory;
78-
}*/
79-
8058
@Bean
8159
@Autowired
8260
public ExecutorServiceExitCodeGenerator executorServiceExitCodeGenerator(ExecutorService executorService) {

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java‎

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,80 @@
33
import java.io.Serializable;
44
import java.sql.Date;
55

6-
public class OrderDetail implements Serializable{
7-
8-
private static final long serialVersionUID = 1L;
9-
private Long orderId;
10-
private Date orderDate;
11-
private String orderDesc;
12-
private User user;
13-
14-
public OrderDetail(){
15-
16-
}
17-
18-
public OrderDetail(Date orderDate, String orderDesc) {
19-
super();
20-
this.orderDate = orderDate;
21-
this.orderDesc = orderDesc;
22-
}
23-
24-
public Date getOrderDate() {
25-
return orderDate;
26-
}
27-
public void setOrderDate(Date orderDate) {
28-
this.orderDate = orderDate;
29-
}
30-
public String getOrderDesc() {
31-
return orderDesc;
32-
}
33-
public void setOrderDesc(String orderDesc) {
34-
this.orderDesc = orderDesc;
35-
}
36-
public User getUser() {
37-
return user;
38-
}
39-
public void setUser(User user) {
40-
this.user = user;
41-
}
42-
@Override
43-
public int hashCode() {
44-
final int prime = 31;
45-
int result = 1;
46-
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
47-
return result;
48-
}
49-
@Override
50-
public boolean equals(Object obj) {
51-
if (this == obj)
52-
return true;
53-
if (obj == null)
54-
return false;
55-
if (getClass() != obj.getClass())
56-
return false;
57-
OrderDetail other = (OrderDetail) obj;
58-
if (orderId == null) {
59-
if (other.orderId != null)
60-
return false;
61-
} else if (!orderId.equals(other.orderId))
62-
return false;
63-
64-
return true;
65-
}
66-
public Long getOrderId() {
67-
return orderId;
68-
}
69-
public void setOrderId(Long orderId) {
70-
this.orderId = orderId;
71-
}
72-
6+
public class OrderDetail implements Serializable {
7+
8+
private static final long serialVersionUID = 1L;
9+
private Long orderId;
10+
private Date orderDate;
11+
private String orderDesc;
12+
private User user;
13+
14+
public OrderDetail() {
15+
16+
}
17+
18+
public OrderDetail(Date orderDate, String orderDesc) {
19+
super();
20+
this.orderDate = orderDate;
21+
this.orderDesc = orderDesc;
22+
}
23+
24+
public Date getOrderDate() {
25+
return orderDate;
26+
}
27+
28+
public void setOrderDate(Date orderDate) {
29+
this.orderDate = orderDate;
30+
}
31+
32+
public String getOrderDesc() {
33+
return orderDesc;
34+
}
35+
36+
public void setOrderDesc(String orderDesc) {
37+
this.orderDesc = orderDesc;
38+
}
39+
40+
public User getUser() {
41+
return user;
42+
}
43+
44+
public void setUser(User user) {
45+
this.user = user;
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
final int prime = 31;
51+
int result = 1;
52+
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
53+
return result;
54+
}
55+
56+
@Override
57+
public boolean equals(Object obj) {
58+
if (this == obj)
59+
return true;
60+
if (obj == null)
61+
return false;
62+
if (getClass() != obj.getClass())
63+
return false;
64+
OrderDetail other = (OrderDetail) obj;
65+
if (orderId == null) {
66+
if (other.orderId != null)
67+
return false;
68+
} else if (!orderId.equals(other.orderId))
69+
return false;
70+
71+
return true;
72+
}
73+
74+
public Long getOrderId() {
75+
return orderId;
76+
}
77+
78+
public void setOrderId(Long orderId) {
79+
this.orderId = orderId;
80+
}
81+
7382
}

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/fetching/model/User.java‎

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,90 @@
55
import java.util.Set;
66

77
public class User implements Serializable {
8-
9-
private static final long serialVersionUID = 1L;
10-
private Long userId;
8+
9+
private static final long serialVersionUID = 1L;
10+
private Long userId;
1111
private String userName;
1212
private String firstName;
1313
private String lastName;
1414
private Set<OrderDetail> orderDetail = new HashSet<OrderDetail>();
1515

1616
public User() {
17-
1817
}
1918

2019
public User(final Long userId, final String userName, final String firstName, final String lastName) {
21-
super();
22-
this.userId = userId;
23-
this.userName = userName;
24-
this.firstName = firstName;
25-
this.lastName = lastName;
20+
super();
21+
this.userId = userId;
22+
this.userName = userName;
23+
this.firstName = firstName;
24+
this.lastName = lastName;
2625

2726
}
2827

2928
@Override
3029
public int hashCode() {
31-
final int prime = 31;
32-
int result = 1;
33-
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
34-
return result;
30+
final int prime = 31;
31+
int result = 1;
32+
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
33+
return result;
3534
}
3635

3736
@Override
3837
public boolean equals(final Object obj) {
39-
if (this == obj)
40-
return true;
41-
if (obj == null)
42-
return false;
43-
if (getClass() != obj.getClass())
44-
return false;
45-
final User other = (User) obj;
46-
if (userId == null) {
47-
if (other.userId != null)
48-
return false;
49-
} else if (!userId.equals(other.userId))
50-
return false;
51-
return true;
38+
if (this == obj)
39+
return true;
40+
if (obj == null)
41+
return false;
42+
if (getClass() != obj.getClass())
43+
return false;
44+
final User other = (User) obj;
45+
if (userId == null) {
46+
if (other.userId != null)
47+
return false;
48+
} else if (!userId.equals(other.userId))
49+
return false;
50+
return true;
5251
}
5352

5453
public Long getUserId() {
55-
return userId;
54+
return userId;
5655
}
5756

5857
public void setUserId(final Long userId) {
59-
this.userId = userId;
58+
this.userId = userId;
6059
}
6160

6261
public String getUserName() {
63-
return userName;
62+
return userName;
6463
}
6564

6665
public void setUserName(final String userName) {
67-
this.userName = userName;
66+
this.userName = userName;
6867
}
6968

7069
public String getFirstName() {
71-
return firstName;
70+
return firstName;
7271
}
7372

7473
public void setFirstName(final String firstName) {
75-
this.firstName = firstName;
74+
this.firstName = firstName;
7675
}
7776

7877
public String getLastName() {
79-
return lastName;
78+
return lastName;
8079
}
8180

8281
public void setLastName(final String lastName) {
83-
this.lastName = lastName;
82+
this.lastName = lastName;
8483
}
8584

86-
public Set<OrderDetail> getOrderDetail() {
87-
return orderDetail;
88-
}
85+
public Set<OrderDetail> getOrderDetail() {
86+
return orderDetail;
87+
}
8988

90-
public void setOrderDetail(Set<OrderDetail> orderDetail) {
91-
this.orderDetail = orderDetail;
92-
}
89+
public void setOrderDetail(Set<OrderDetail> orderDetail) {
90+
this.orderDetail = orderDetail;
91+
}
9392

94-
9593

9694
}
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
package com.baeldung.hibernate.fetching.util;
22

33
import org.hibernate.Session;
4-
import org.hibernate.SessionFactory;
54
import org.hibernate.cfg.Configuration;
65

76
public class HibernateUtil {
8-
private static SessionFactory factory;
97

10-
@SuppressWarnings("deprecation")
11-
public static Session getHibernateSession(String fetchMethod) {
12-
//two config files are there
13-
//one with lazy loading enabled
14-
//another lazy = false
15-
SessionFactory sf = null;
16-
if ("lazy".equals(fetchMethod)) {
17-
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
18-
} else {
19-
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
20-
}
21-
// fetching.cfg.xml is used for this example
22-
final Session session = sf.openSession();
23-
return session;
24-
}
8+
@SuppressWarnings("deprecation")
9+
public static Session getHibernateSession(String fetchMethod) {
10+
//two config files are there
11+
//one with lazy loading enabled
12+
//another lazy = false
2513

26-
public static Session getHibernateSession() {
27-
SessionFactory sf = null;
28-
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
29-
final Session session = sf.openSession();
30-
return session;
31-
}
14+
final String configFileName = "lazy".equals(fetchMethod) ?
15+
"fetchingLazy.cfg.xml" :
16+
"fetching.cfg.xml";
17+
18+
return new Configuration()
19+
.configure(configFileName)
20+
.buildSessionFactory().openSession();
21+
}
22+
23+
public static Session getHibernateSession() {
24+
return new Configuration()
25+
.configure("fetching.cfg.xml")
26+
.buildSessionFactory()
27+
.openSession();
28+
}
3229

3330
}

0 commit comments

Comments
 (0)