
SOF301 – Ngôn ngữ lập trình Java 4 Lab 7
1
Bà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

SOF301 – Ngôn ngữ lập trình Java 4 Lab 7
2
Sử 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:

SOF301 – Ngôn ngữ lập trình Java 4 Lab 7
3

SOF301 – Ngôn ngữ lập trình Java 4 Lab 7
4
Bà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.
• Department.java
?
1
2
3
4
5
6
package
org.Fpoly.tutorial.hibernate.entities;
import
java.util.HashSet;
import
java.util.Set;
import javax.persistence.Column;

SOF301 – Ngôn ngữ lập trình Java 4 Lab 7
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
import
javax.persistence.Entity;
import
javax.persistence.FetchType;
import
javax.persistence.Id;
import
javax.persistence.OneToMany;
import
javax.persistence.Table;
import
javax.persistence.UniqueConstraint;
@Entity
@Table(name
= "DEPARTMENT",
uniqueConstraints = { @UniqueConstraint(columnNames = { "DEPT_NO"
}) })
public
class Department {
private Integer deptId;
private String deptNo;
private String deptName;
private String location;
private Set<Employee> employees = new HashSet<Employee>(0);
public Department() {
}
public Department(Integer deptId, String deptName, String location) {
this.deptId = deptId;
this.deptNo = "D" + this.deptId;
this.deptName = deptName;
this.location = location;
}
@Id
@Column(name = "DEPT_ID")
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {

