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
• QueryObjectDemo.java
?
Bài 1 Query Object
1
2
3
4
5
6
7
8
9
11
13
14
16
18
19
20
21
22
24
25
26
27
28
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; 10 public class QueryObjectDemo { 12 public static void main(String[] args) { SessionFactory factory = HibernateUtils.getSessionFactory(); 15 Session session = factory.getCurrentSession(); 17 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(); 23 // 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 "; 30
29
SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
3
31
32
34
35
37
38
39
40
42
43
44
45
46
47
48
// Tạo đối tượng Query.
Query query = session.createQuery(sql);
33
// Thực hiện truy vấn.
List employees = query.list();
36
for (Employee emp : employees) {
System.out.println("Emp: " + emp.getEmpNo() + " : "
+ emp.getEmpName());
}
41
// 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();
}
}
49
}
Bài 2 Query Object 2
• QueryObjectDemo2.java
?
1
2
3
4
5
6
7
8
9
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; 10 public class QueryObjectDemo2 {
11
SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
4
13
14
16
18
19
20
21
22
24
25
26
27
28
29
30
31
33
34
36
38
39
41
42
43
44
46
47
48 12
public static void main(String[] args) {
SessionFactory factory = HibernateUtils.getSessionFactory();
15
Session session = factory.getCurrentSession();
17
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();
23
// 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 ";
32
// Tạo đối tượng Query.
Query query = session.createQuery(sql);
35
query.setParameter("deptNo", "D10");
37
// Thực hiện truy vấn.
List
SOF301 – Ngôn ngữ lập trình Java 4 Lab 8
5
49
50
51
e.printStackTrace(); // Rollback trong trường hợp có lỗi xẩy ra. session.getTransaction().rollback(); } } 53 }
52
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
Đánh giá bài lab
STT Bài số Bài 1 Bài 2 Bài 3 1 2 3 Điểm
SOF301 – Ngôn ngữ lập trình Java 4 Lab 8