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

SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
2
Sử dụng Project đã tạo ở lab 7
Bài 1 Query Object
• QueryObjectDemo.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.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 ";

SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
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
• QueryObjectDemo2.java
?
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 {

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

SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
5
49
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
STT
Bài số
Điểm
1
Bài 1
2
Bài 2
3
Bài 3

