File tree Expand file tree Collapse file tree
spring-all/src/main/java/org/baeldung/startup
java/com/baeldung/hibernate/fetching
java/com/baeldung/hibernate/fetching
com/baeldung/hibernate/fetching/model Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .startup ;
2+
3+ import org .apache .log4j .Logger ;
4+ import org .springframework .context .event .ContextRefreshedEvent ;
5+ import org .springframework .context .event .EventListener ;
6+ import org .springframework .stereotype .Component ;
7+
8+ @ Component
9+ public class EventListenerExampleBean {
10+ private static final Logger LOG = Logger .getLogger (EventListenerExampleBean .class );
11+
12+ public static int counter ;
13+
14+ @ EventListener
15+ public void onApplicationEvent (ContextRefreshedEvent event ) {
16+ LOG .info ("Increment counter" );
17+ counter ++;
18+ }
19+ }
Original file line number Diff line number Diff line change 11package com .baeldung .hibernate .fetching .model ;
22
3+ import javax .persistence .*;
34import java .io .Serializable ;
45import java .sql .Date ;
56
7+ @ Entity
8+ @ Table (name = "USER_ORDER" )
69public class OrderDetail implements Serializable {
710
811 private static final long serialVersionUID = 1L ;
12+
13+ @ Id
14+ @ GeneratedValue
15+ @ Column (name ="ORDER_ID" )
916 private Long orderId ;
10- private Date orderDate ;
11- private String orderDesc ;
12- private User user ;
1317
14- public OrderDetail (){
18+ public OrderDetail (){
1519 }
1620
1721 public OrderDetail (Date orderDate , String orderDesc ) {
18- super ();
19- this .orderDate = orderDate ;
20- this .orderDesc = orderDesc ;
22+ super ();
2123 }
2224
23- public Date getOrderDate () {
24- return orderDate ;
25- }
26- public void setOrderDate (Date orderDate ) {
27- this .orderDate = orderDate ;
28- }
29- public String getOrderDesc () {
30- return orderDesc ;
31- }
32- public void setOrderDesc (String orderDesc ) {
33- this .orderDesc = orderDesc ;
34- }
35- public User getUser () {
36- return user ;
37- }
38- public void setUser (User user ) {
39- this .user = user ;
40- }
4125 @ Override
4226 public int hashCode () {
4327 final int prime = 31 ;
@@ -62,6 +46,7 @@ public boolean equals(Object obj) {
6246
6347 return true ;
6448 }
49+
6550 public Long getOrderId () {
6651 return orderId ;
6752 }
Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ package com .baeldung .hibernate .fetching .model ;
2+
3+ import javax .persistence .*;
4+ import java .io .Serializable ;
5+ import java .util .HashSet ;
6+ import java .util .Set ;
7+
8+ @ Entity
9+ @ Table (name = "USER" )
10+ public class UserEager implements Serializable {
11+
12+ private static final long serialVersionUID = 1L ;
13+
14+ @ Id
15+ @ GeneratedValue
16+ @ Column (name ="USER_ID" )
17+ private Long userId ;
18+
19+ @ OneToMany (fetch = FetchType .EAGER , mappedBy = "user" )
20+ private Set <OrderDetail > orderDetail = new HashSet ();
21+
22+ public UserEager () {
23+ }
24+
25+ public UserEager (final Long userId ) {
26+ super ();
27+ this .userId = userId ;
28+ }
29+
30+ @ Override
31+ public int hashCode () {
32+ final int prime = 31 ;
33+ int result = 1 ;
34+ result = prime * result + ((userId == null ) ? 0 : userId .hashCode ());
35+ return result ;
36+ }
37+
38+ @ Override
39+ public boolean equals (final Object obj ) {
40+ if (this == obj )
41+ return true ;
42+ if (obj == null )
43+ return false ;
44+ if (getClass () != obj .getClass ())
45+ return false ;
46+ final UserEager other = (UserEager ) obj ;
47+ if (userId == null ) {
48+ if (other .userId != null )
49+ return false ;
50+ } else if (!userId .equals (other .userId ))
51+ return false ;
52+ return true ;
53+ }
54+
55+ public Long getUserId () {
56+ return userId ;
57+ }
58+
59+ public void setUserId (final Long userId ) {
60+ this .userId = userId ;
61+ }
62+
63+ public Set <OrderDetail > getOrderDetail () {
64+ return orderDetail ;
65+ }
66+
67+ public void setOrderDetail (Set <OrderDetail > orderDetail ) {
68+ this .orderDetail = orderDetail ;
69+ }
70+
71+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .hibernate .fetching .model ;
2+
3+ import javax .persistence .*;
4+ import java .io .Serializable ;
5+ import java .util .HashSet ;
6+ import java .util .Set ;
7+
8+ @ Entity
9+ @ Table (name = "USER" )
10+ public class UserLazy implements Serializable {
11+
12+ private static final long serialVersionUID = 1L ;
13+
14+ @ Id
15+ @ GeneratedValue
16+ @ Column (name ="USER_ID" )
17+ private Long userId ;
18+
19+ @ OneToMany (fetch = FetchType .LAZY , mappedBy = "user" )
20+ private Set <OrderDetail > orderDetail = new HashSet ();
21+
22+ public UserLazy () {
23+ }
24+
25+ public UserLazy (final Long userId ) {
26+ super ();
27+ this .userId = userId ;
28+ }
29+
30+ @ Override
31+ public int hashCode () {
32+ final int prime = 31 ;
33+ int result = 1 ;
34+ result = prime * result + ((userId == null ) ? 0 : userId .hashCode ());
35+ return result ;
36+ }
37+
38+ @ Override
39+ public boolean equals (final Object obj ) {
40+ if (this == obj )
41+ return true ;
42+ if (obj == null )
43+ return false ;
44+ if (getClass () != obj .getClass ())
45+ return false ;
46+ final UserLazy other = (UserLazy ) obj ;
47+ if (userId == null ) {
48+ if (other .userId != null )
49+ return false ;
50+ } else if (!userId .equals (other .userId ))
51+ return false ;
52+ return true ;
53+ }
54+
55+ public Long getUserId () {
56+ return userId ;
57+ }
58+
59+ public void setUserId (final Long userId ) {
60+ this .userId = userId ;
61+ }
62+
63+
64+ public Set <OrderDetail > getOrderDetail () {
65+ return orderDetail ;
66+ }
67+
68+ public void setOrderDetail (Set <OrderDetail > orderDetail ) {
69+ this .orderDetail = orderDetail ;
70+ }
71+
72+ }
Original file line number Diff line number Diff line change @@ -11,16 +11,15 @@ public static Session getHibernateSession(String fetchMethod) {
1111 //two config files are there
1212 //one with lazy loading enabled
1313 //another lazy = false
14- SessionFactory sf = null ;
14+ SessionFactory sf ;
1515 if ("lazy" .equals (fetchMethod )) {
1616 sf = new Configuration ().configure ("fetchingLazy.cfg.xml" ).buildSessionFactory ();
1717 } else {
1818 sf = new Configuration ().configure ("fetching.cfg.xml" ).buildSessionFactory ();
1919 }
2020
2121 // fetching.cfg.xml is used for this example
22- final Session session = sf .openSession ();
23- return session ;
22+ return sf .openSession ();
2423 }
2524
2625 public static Session getHibernateSession () {
You can’t perform that action at this time.
0 commit comments