-
Notifications
You must be signed in to change notification settings - Fork 1
/
hibernate_list.txt
213 lines (156 loc) · 6.79 KB
/
hibernate_list.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Hibernate allows us to map the Java collections object with data structures. Here, we will look at how to deal with Java collections using Hibernate. Java collections commonly include List, Map, Set, and Array. All standard Java collections are supported by hibernate.
Persisting List
--------------
List is an interface provided by Java and accessed from java.util.List that has the capability to store a sequence of elements, allow duplicate elements, and contain elements of the same type. Some classes that implement the List interface are java.util.ArrayList, java.util.LinkedList, and so on. Now, let's look at how to use List while using hibernate.
Here, we consider a new table structure for employee, and each employee has multiple e-mail addresses. So, we create an Employee class that has the List<String> field e-mails called list of e-mail addresses. Here, we use the List class for this recipe. To achieve this, we need to create classes and tables; so, first of all, let's meet the basic prerequisites.
Employee.java
-------------
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name = "email")
private List<String> emails;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
@Override
public String toString() {
return "Employee"
+ "\n\tId:" + this.id
+ "\n\tName:" + this.name
+ "\n\tEmails:" + this.emails;
}
}
@CollectionTable: This annotation indicates that the current field is of the Collection type, and hibernate creates a separate table for it. It also creates a reference between them. In this case, hibernate creates a table named email with email and employee_id. The employee_id column is made by joining the persisted class name and the primary key column of the employee class with an underscore (_).
@ElementCollection: This annotation is used to define the relationship with the embedded or basic type.
Here, we also use the fetch=FetchType.LAZY attribute, which means that hibernate will load a child or referenced record on demand only. In our Retrieving a record example, it will execute the employee and e-mail queries separately. Hibernate uses FetchType.LAZY if no attribute is defined for fetch.
Another possible value with the fetch attribute is FetchType.EAGER, which means that hibernate will load all the child records at the time of the retrieval of the parent record. In other words, it eagerly loads all the records. If we use FetchType.EAGER, hibernate uses the following query:
Inserting a record
-----------------
The following code is used to insert a record into the database. Here, we will try to insert the record of an employee with three e-mail addresses:
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Employee employee = new Employee();
employee.setName("Roger");
List<String> emails = new ArrayList<String>();
emails.add("emailaddress1@provider1.com");
emails.add("emailaddress2@provider2.com");
emails.add("emailaddress3@provider3.com");
employee.setEmails(emails);
session.save(employee);
t.commit();
session.close();
Retrieving a record
-------------------
Employee employee = (Employee) session.get(Employee.class, 1l);
System.out.println(employee.toString());
Updating a record
-----------------
Here, we will try to add one more e-mail address to the list of e-mail IDs for Employee#1, which means that we will update the list of e-mails.
Employee employee = (Employee) session.get(Employee.class, 1l);
List<String> emails = employee.getEmails();
emails.add("emailaddress3@provider3.com");
session.saveOrUpdate(employee);
System.out.println(employee.toString());
Deleting a record
-----------------
Employee employee = new Employee();
employee.setId(1);
session.delete(employee);
Now Mapping Objects with the List:
==================================
we dealt with List of the basic data type, which means that we used List of String (List<String>). Now, let's consider that we have a Account class, which contains the degree name and passing year and want to map it with Employee. For this, we deal with List<Object> instead of List<String>. In our case, it will be List<Account>.
package com.list.demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "account")
public class Account {
@Id
@GeneratedValue
private long id;
@Column(name = "bankname")
private String bankName;
@Column(name = "passingyear")
private int accountNumber;
public Account() {
}
public Account( String bankName, int accountNumber) {
super();
this.bankName = bankName;
this.accountNumber = accountNumber;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
@Override
public String toString() {
return "Account [id=" + id + ", bankName=" + bankName + ", accountNumber=" + accountNumber + "]";
}
}
Here, we used @OneToMany(cascade={CascadeType.ALL}) to map account with employee
Now Main class:
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Employee employee = new Employee();
employee.setName("Pankaj");
/*List<String> emails = new ArrayList<String>();
emails.add("emailaddress1@provider1.com");
emails.add("emailaddress2@provider2.com");
emails.add("emailaddress3@provider3.com");
employee.setEmails(emails);*/
List<Account> accounts = new ArrayList<>();
Account account = new Account("Kotak Mahindra", 12345678910L);
Account account1 = new Account("Bank Of India", 98765432109L);
Account account2 = new Account("Oriental Bank of Commerce", 3425161718L);
accounts.add(account);
accounts.add(account1);
accounts.add(account2);
employee.setAccounts(accounts);
session.save(employee);
t.commit();
session.close();