Bài thực hành Ngôn ngữ lập trình Java số 7 nhằm mục tiêu giúp người học hiểu được cách sử dụng các thành phần cơ bản trong ngôn ngữ Hibernate. Bài thực hành gồm có các phần chính: Hibernate Mapping: one to one; Hibernate Mapping: Many to one; Hibernate Mapping: one to many; Hibernate Mapping: many to many.
Trang 1Bài th ực hành số 7
Mục tiêu
Hiểu cách sử dụng các thành phần cơ bản trong ngôn ngữ Hibernate
Hibernate Mapping: one to one
Hibernate Mapping: Many to one
Hibernate Mapping: one to many
Hibernate Mapping: many to many
Trang 2S ử dụng cơ sở dữ liệu tài nguyên simpleHr
Bài 1 Tạo Project và khai báo thư viện
Tạo thư mục Libs trong project và add những file sau
Nhấn phải chuột vào Project chọn Properties
Add hết tất cả các thư viện có trong thư mục libs:
Trang 4Bài 2 T ạo Class Entity
Chúng ta tạo các class Entity Mỗi Entity sẽ mô tả một bảng trong DB
1 Department - Phòng ban
2 Employee - Nhân viên
3 SalaryGrade - Bậc lương
4 Timekeeper - Máy chấm công, giờ ra vào của nhân viên
?
1
2
3
4
5
6
Trang 5
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
@Entity
@Table(name = "DEPARTMENT",
uniqueConstraints = { @UniqueConstraint(columnNames = { "DEPT_NO"
}) })
}
this.deptId = deptId;
this.deptName = deptName;
this.location = location;
}
@Id
@Column(name = "DEPT_ID")
}
Trang 6
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
this.deptId = deptId;
}
@Column(name = "DEPT_NO", length = 20, nullable = false)
}
this.deptNo = deptNo;
}
@Column(name = "DEPT_NAME", nullable = false)
}
this.deptName = deptName;
}
@Column(name = "LOCATION")
}
this.location = location;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
}
this.employees = employees;
Trang 781 }
}
?
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
@Entity
@Table(name = "EMPLOYEE",
uniqueConstraints = { @UniqueConstraint(columnNames = {
"EMP_NO"}) })
Trang 8
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
}
manager,
Date hideDate, Float salary, Float comm, Department
department) {
this.empId = empId;
this.empName = empName;
this.job = job;
this.manager = manager;
this.hideDate = hideDate;
this.salary = salary;
this.department = department;
}
@Id
@Column(name = "EMP_ID")
}
this.empId = empId;
}
@Column(name = "EMP_NO", length = 20, nullable = false)
Trang 968
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
}
this.empNo = empNo;
}
@Column(name = "EMP_NAME", length = 50, nullable = false)
}
this.empName = empName;
}
@Column(name = "JOB", length = 30, nullable = false)
}
this.job = job;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MNG_ID")
}
this.manager = manager;
}
@Column(name = "HIRE_DATE", nullable = false)
@Temporal(TemporalType.DATE)
Trang 10105
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
}
this.hideDate = hideDate;
}
@Column(name = "SALARY", nullable = false)
}
this.salary = salary;
}
@Column(name = "IMAGE", length = 1111111, nullable = true)
@Lob
}
this.image = image;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPT_ID", nullable = false)
}
this.department = department;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "empId")
Trang 11142
143
144
145
146
147
}
this.employees = employees;
}
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Entity
@Table(name = "SALARY_GRADE")
}
highSalary) {
this.grade = grade;
this.lowSalary = lowSalary;
this.highSalary = highSalary;
}
Trang 1224
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
@Id
@Column(name = "GRADE")
}
this.grade = grade;
}
@Column(name = "LOW_SALARY", nullable = false)
}
this.lowSalary = lowSalary;
}
@Column(name = "HIGH_SALARY", nullable = false)
}
this.highSalary = highSalary;
}
}
?
1
2
3
Trang 13
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
@Entity
@Table(name = "TIMEKEEPER")
// 'I' or 'O'
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "Timekeeper_Id", length = 36)
}
Trang 14
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
this.timekeeperId = timekeeperId;
}
@Column(name = "Date_Time", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
}
this.dateTime = dateTime;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EMP_ID", nullable = false)
}
this.employee = employee;
}
@Column(name = "In_Out", nullable = false, length = 1)
}
this.inOut = inOut;
}
}
Trang 15Bài 3
Gi ảng viên giao thêm bài cho sinh viên
Yêu cầu nộp bài
Cuối giờ thực hành, sinh viên tạo thư mục theo tên <Tên đăng nhập SV>_Lab1, chứa tất cả sản phẩm
c ủa những bài lab trên, nén lại thành file zip và upload lên mục nộp bài tương ứng trên LMS
Đánh giá bài lab
1 Bài 1
2 Bài 2
3 Bài 3