Bài thực hành Ngôn ngữ lập trình Java số 8 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. Phần này sẽ tập trung hướng dẫn Hibernate Query Language. Mời các bạn cùng tham khảo.
Trang 1Bài th ực hành số 8
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 Query Language
Trang 2S ử dụng Project đã tạo ở lab 7
Bài 1 Query Object
?
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.o7planning.tutorial.hibernate.query;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.o7planning.tutorial.hibernate.HibernateUtils;
import org.o7planning.tutorial.hibernate.entities.Employee;
public class QueryObjectDemo {
public static void main(String[] args) {
SessionFactory factory = HibernateUtils.getSessionFactory();
Session session = factory.getCurrentSession();
try {
// Tất cả các lệnh hành động với DB thông qua Hibernate
// đều phải nằm trong 1 giao dịch (Transaction)
// Bắt đầu giao dịch
session.getTransaction().begin();
// Tạo một câu lệnh HQL query object
// Tương đương với Native SQL:
// Select e.* from EMPLOYEE e order by e.EMP_NAME, e.EMP_NO
String sql = "Select e from " + Employee.class.getName() + "
e "
+ " order by e.empName, e.empNo ";
Trang 3
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Tạo đối tượng Query
Query query = session.createQuery(sql);
// Thực hiện truy vấn
List<Employee> employees = query.list();
for (Employee emp : employees) {
System.out.println("Emp: " + emp.getEmpNo() + " : "
+ emp.getEmpName());
}
// Commit dữ liệu
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// Rollback trong trường hợp có lỗi xẩy ra
session.getTransaction().rollback();
}
}
}
Bài 2 Query Object 2
?
1
2
3
4
5
6
7
8
9
10
11
package org.o7planning.tutorial.hibernate.query;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.o7planning.tutorial.hibernate.HibernateUtils;
import org.o7planning.tutorial.hibernate.entities.Employee;
public class QueryObjectDemo2 {
Trang 412
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
public static void main(String[] args) {
SessionFactory factory = HibernateUtils.getSessionFactory();
Session session = factory.getCurrentSession();
try {
// Tất cả các lệnh hành động với DB thông qua Hibernate
// đều phải nằm trong 1 giao dịch (Transaction)
// Bắt đầu giao dịch
session.getTransaction().begin();
// Tạo một câu lệnh HQL query object
// HQL Có tham số
// Tương đương với Native SQL:
// Select e.* from EMPLOYEE e cross join DEPARTMENT d
// where e.DEPT_ID = d.DEPT_ID and d.DEPT_NO = :deptNo;
String sql = "Select e from " + Employee.class.getName() + "
e "
+ " where e.department.deptNo=:deptNo ";
// Tạo đối tượng Query
Query query = session.createQuery(sql);
query.setParameter("deptNo", "D10");
// Thực hiện truy vấn
List<Employee> employees = query.list();
for (Employee emp : employees) {
System.out.println("Emp: " + emp.getEmpNo() + " : "
+ emp.getEmpName());
}
// Commit dữ liệu
session.getTransaction().commit();
} catch (Exception e) {
Trang 549
50
51
52
53
e.printStackTrace();
// Rollback trong trường hợp có lỗi xẩy ra
session.getTransaction().rollback();
}
}
}
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 <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