-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path15 Fetch EAGER LAZY.txt
174 lines (136 loc) · 4.34 KB
/
15 Fetch EAGER LAZY.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
--------------------------------------------------------------------------------
App.java
--------------------------------------------------------------------------------
package com.telusko.DemoHib;
import java.util.Collection;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class App
{
public static void main( String[] args )
{
Configuration config = new Configuration().configure().addAnnotatedClass(Laptop.class).addAnnotatedClass(Alien.class);
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Alien a1 = session.get(Alien.class, 1);
System.out.println(a1.getAname());
// Collection<Laptop> laps = a1.getLaps();
//
// for(Laptop l : laps)
// {
// System.out.println(l);
// }
session.getTransaction().commit();
}
}
--------------------------------------------------------------------------------
Laptop.java
--------------------------------------------------------------------------------
package com.telusko.DemoHib;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
@Entity
public class Laptop
{
@Id
private int lid;
private String brand;
private String price;
@ManyToOne
private Alien alien;
public int getLid() {
return lid;
}
public void setLid(int lid) {
this.lid = lid;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Alien getAlien() {
return alien;
}
public void setAlien(Alien alien) {
this.alien = alien;
}
}
--------------------------------------------------------------------------------
Alien.java
--------------------------------------------------------------------------------
package com.telusko.DemoHib;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
public class Alien
{
@Id
private int aid;
private String aname;
@OneToMany(mappedBy = "alien",fetch=FetchType.EAGER)
private Collection<Laptop> laps = new ArrayList<Laptop>();
public Collection<Laptop> getLaps() {
return laps;
}
public void setLaps(Collection<Laptop> laps) {
this.laps = laps;
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
@Override
public String toString() {
return "Alien [aid=" + aid + ", aname=" + aname + " ]";
}
}
--------------------------------------------------------------------------------
hibernate.cfg.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>