Giáo trình Lập trình Java 4 - Bài 7: Bài thực hành số 7 - Trường Cao đẳng FPT

Tài liệu Giáo trình Lập trình Java 4 - Bài 7: Bài thực hành số 7 - Trường Cao đẳng FPT: 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.entiti...

pdf15 trang | Chia sẻ: quangot475 | Lượt xem: 903 | Lượt tải: 1download
Bạn đang xem nội dung tài liệu Giáo trình Lập trình Java 4 - Bài 7: Bài thực hành số 7 - Trường Cao đẳng FPT, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
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 employees = new HashSet(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) { SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 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) public String getDeptNo() { return deptNo; } public void setDeptNo(String deptNo) { this.deptNo = deptNo; } @Column(name = "DEPT_NAME", nullable = false) public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } @Column(name = "LOCATION") public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "department") public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 7 81 } } • Employee.java ? 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 package org.Fpoly.tutorial.hibernate.entities; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Lob; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.UniqueConstraint; @Entity @Table(name = "EMPLOYEE", uniqueConstraints = { @UniqueConstraint(columnNames = { "EMP_NO" }) }) public class Employee { private Long empId; private String empNo; private String empName; private String job; private Employee manager; SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 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 private Date hideDate; private Float salary; private byte[] image; private Department department; private Set employees = new HashSet(0); public Employee() { } public Employee(Long empId, String empName, String job, Employee manager, Date hideDate, Float salary, Float comm, Department department) { this.empId = empId; this.empNo = "E" + this.empId; this.empName = empName; this.job = job; this.manager = manager; this.hideDate = hideDate; this.salary = salary; this.department = department; } @Id @Column(name = "EMP_ID") public Long getEmpId() { return empId; } public void setEmpId(Long empId) { this.empId = empId; } @Column(name = "EMP_NO", length = 20, nullable = false) public String getEmpNo() { return empNo; SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 9 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 } public void setEmpNo(String empNo) { this.empNo = empNo; } @Column(name = "EMP_NAME", length = 50, nullable = false) public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } @Column(name = "JOB", length = 30, nullable = false) public String getJob() { return job; } public void setJob(String job) { this.job = job; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "MNG_ID") public Employee getManager() { return manager; } public void setManager(Employee manager) { this.manager = manager; } @Column(name = "HIRE_DATE", nullable = false) @Temporal(TemporalType.DATE) public Date getHideDate() { SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 10 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 return hideDate; } public void setHideDate(Date hideDate) { this.hideDate = hideDate; } @Column(name = "SALARY", nullable = false) public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } @Column(name = "IMAGE", length = 1111111, nullable = true) @Lob public byte[] getImage() { return image; } public void setImage(byte[] image) { this.image = image; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "DEPT_ID", nullable = false) public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "empId") SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 11 142 143 144 145 146 147 public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; } } • SalaryGrade.java ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package org.Fpoly.tutorial.hibernate.entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "SALARY_GRADE") public class SalaryGrade { private Integer grade; private Float lowSalary; private Float highSalary; public SalaryGrade() { } public SalaryGrade(Integer grade, Float lowSalary, Float highSalary) { this.grade = grade; this.lowSalary = lowSalary; this.highSalary = highSalary; } SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 12 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 @Id @Column(name = "GRADE") public Integer getGrade() { return grade; } public void setGrade(Integer grade) { this.grade = grade; } @Column(name = "LOW_SALARY", nullable = false) public Float getLowSalary() { return lowSalary; } public void setLowSalary(Float lowSalary) { this.lowSalary = lowSalary; } @Column(name = "HIGH_SALARY", nullable = false) public Float getHighSalary() { return highSalary; } public void setHighSalary(Float highSalary) { this.highSalary = highSalary; } } • Timekeeper.java ? 1 2 3 package org.Fpoly.tutorial.hibernate.entities; import java.util.Date; SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 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 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "TIMEKEEPER") public class Timekeeper { public static final char IN = 'I'; public static final char OUT = 'O'; private String timekeeperId; private Date dateTime; private Employee employee; // 'I' or 'O' private char inOut; @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") @Column(name = "Timekeeper_Id", length = 36) public String getTimekeeperId() { return timekeeperId; } SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 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 public void setTimekeeperId(String timekeeperId) { this.timekeeperId = timekeeperId; } @Column(name = "Date_Time", nullable = false) @Temporal(TemporalType.TIMESTAMP) public Date getDateTime() { return dateTime; } public void setDateTime(Date dateTime) { this.dateTime = dateTime; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "EMP_ID", nullable = false) public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } @Column(name = "In_Out", nullable = false, length = 1) public char getInOut() { return inOut; } public void setInOut(char inOut) { this.inOut = inOut; } } SOF301 – Ngôn ngữ lập trình Java 4 Lab 7 15 Bà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 _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 STT Bài số Điểm 1 Bài 1 2 Bài 2 3 Bài 3

Các file đính kèm theo tài liệu này:

  • pdfsof301_lab_7_7355_2154497.pdf
Tài liệu liên quan