Bài thực hành Lập trình Java 4 - Bài 8
Số trang: 5
Loại file: pdf
Dung lượng: 157.18 KB
Lượt xem: 23
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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.
Nội dung trích xuất từ tài liệu:
Bài thực hành Lập trình Java 4 - Bài 8 1 Bài thực hành số 8Mục tiêuHiểu cách sử dụng các thành phần cơ bản trong ngôn ngữ Hibernate Hibernate Query LanguageSOF301 – Ngôn ngữ lập trình Java 4 Lab 8 2Sử dụng Project đã tạo ở lab 7Bài 1 Query Object • QueryObjectDemo.java ? 1 package org.o7planning.tutorial.hibernate.query; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.o7planning.tutorial.hibernate.HibernateUtils; 9 import org.o7planning.tutorial.hibernate.entities.Employee; 10 11public class QueryObjectDemo { 12 13 public static void main(String[] args) { 14 SessionFactory factory = HibernateUtils.getSessionFactory(); 15 16 Session session = factory.getCurrentSession(); 17 18 try { 19 // Tất cả các lệnh hành động với DB thông qua Hibernate 20 // đều phải nằm trong 1 giao dịch (Transaction) 21 // Bắt đầu giao dịch 22 session.getTransaction().begin(); 23 24 // Tạo một câu lệnh HQL query object. 25 // Tương đương với Native SQL: 26 // Select e.* from EMPLOYEE e order by e.EMP_NAME, e.EMP_NO 27 String sql = Select e from + Employee.class.getName() + 28e 29 + order by e.empName, e.empNo ; 30SOF301 – Ngôn ngữ lập trình Java 4 Lab 8 3 31 // Tạo đối tượng Query. 32 Query query = session.createQuery(sql); 33 34 // Thực hiện truy vấn. 35 List employees = query.list(); 36 37 for (Employee emp : employees) { 38 System.out.println(Emp: + emp.getEmpNo() + : 39 + emp.getEmpName()); 40 } 41 42 // Commit dữ liệu 43 session.getTransaction().commit(); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 // Rollback trong trường hợp có lỗi xẩy ra. 47 session.getTransaction().rollback(); 48 } 49 } }Bài 2 Query Object 2 • QueryObjectDemo2.java ? 1 package org.o7planning.tutorial.hibernate.query; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.o7planning.tutorial.hibernate.HibernateUtils; 9 import org.o7planning.tutorial.hibernate.entities.Employee; 10 11public class QueryObjectDemo2 {SOF301 – Ngôn ngữ lập trình Java 4 Lab 8 4 12 13 public static void main(String[] args) { 14 SessionFactory factory = HibernateUtils.getSessionFactory(); 15 16 Session session = factory.getCurrentSession(); 17 18 try { 19 // Tất cả các lệnh hành động với DB thông qua Hibernate 20 // đều phải nằm trong 1 giao dịch (Transaction) 21 // Bắt đầu giao dịch 22 session.getTransaction().begin(); 23 24 // Tạo một câu lệnh HQL query object. 25 // HQL Có tham số. 26 // Tương đương với Native SQL: 27 // Select e.* from EMPLOYEE e cross join DEPARTMENT d 28 // where e.DEPT_ID = d.DEPT_ID and d.DEPT_NO = :deptNo; 29 String sql = Select e from + Employee.class.getName() + 30e 31 + where e.department.deptNo=:deptNo ; 32 33 // Tạo đối tượng Query. 34 Query query = session.createQuery(sql); 35 36 query.setParameter(deptNo, D10); 37 38 // Thực hiện truy vấn. 39 List employees = query.list(); 40 41 for (Employee emp : employees) { 42 System.out.println(Emp: + emp ...
Nội dung trích xuất từ tài liệu:
Bài thực hành Lập trình Java 4 - Bài 8 1 Bài thực hành số 8Mục tiêuHiểu cách sử dụng các thành phần cơ bản trong ngôn ngữ Hibernate Hibernate Query LanguageSOF301 – Ngôn ngữ lập trình Java 4 Lab 8 2Sử dụng Project đã tạo ở lab 7Bài 1 Query Object • QueryObjectDemo.java ? 1 package org.o7planning.tutorial.hibernate.query; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.o7planning.tutorial.hibernate.HibernateUtils; 9 import org.o7planning.tutorial.hibernate.entities.Employee; 10 11public class QueryObjectDemo { 12 13 public static void main(String[] args) { 14 SessionFactory factory = HibernateUtils.getSessionFactory(); 15 16 Session session = factory.getCurrentSession(); 17 18 try { 19 // Tất cả các lệnh hành động với DB thông qua Hibernate 20 // đều phải nằm trong 1 giao dịch (Transaction) 21 // Bắt đầu giao dịch 22 session.getTransaction().begin(); 23 24 // Tạo một câu lệnh HQL query object. 25 // Tương đương với Native SQL: 26 // Select e.* from EMPLOYEE e order by e.EMP_NAME, e.EMP_NO 27 String sql = Select e from + Employee.class.getName() + 28e 29 + order by e.empName, e.empNo ; 30SOF301 – Ngôn ngữ lập trình Java 4 Lab 8 3 31 // Tạo đối tượng Query. 32 Query query = session.createQuery(sql); 33 34 // Thực hiện truy vấn. 35 List employees = query.list(); 36 37 for (Employee emp : employees) { 38 System.out.println(Emp: + emp.getEmpNo() + : 39 + emp.getEmpName()); 40 } 41 42 // Commit dữ liệu 43 session.getTransaction().commit(); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 // Rollback trong trường hợp có lỗi xẩy ra. 47 session.getTransaction().rollback(); 48 } 49 } }Bài 2 Query Object 2 • QueryObjectDemo2.java ? 1 package org.o7planning.tutorial.hibernate.query; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.o7planning.tutorial.hibernate.HibernateUtils; 9 import org.o7planning.tutorial.hibernate.entities.Employee; 10 11public class QueryObjectDemo2 {SOF301 – Ngôn ngữ lập trình Java 4 Lab 8 4 12 13 public static void main(String[] args) { 14 SessionFactory factory = HibernateUtils.getSessionFactory(); 15 16 Session session = factory.getCurrentSession(); 17 18 try { 19 // Tất cả các lệnh hành động với DB thông qua Hibernate 20 // đều phải nằm trong 1 giao dịch (Transaction) 21 // Bắt đầu giao dịch 22 session.getTransaction().begin(); 23 24 // Tạo một câu lệnh HQL query object. 25 // HQL Có tham số. 26 // Tương đương với Native SQL: 27 // Select e.* from EMPLOYEE e cross join DEPARTMENT d 28 // where e.DEPT_ID = d.DEPT_ID and d.DEPT_NO = :deptNo; 29 String sql = Select e from + Employee.class.getName() + 30e 31 + where e.department.deptNo=:deptNo ; 32 33 // Tạo đối tượng Query. 34 Query query = session.createQuery(sql); 35 36 query.setParameter(deptNo, D10); 37 38 // Thực hiện truy vấn. 39 List employees = query.list(); 40 41 for (Employee emp : employees) { 42 System.out.println(Emp: + emp ...
Tìm kiếm theo từ khóa liên quan:
Lập trình Java Lập trình Java 4 Kỹ thuật lập trình Ngôn ngữ lập trình Java Ngôn ngữ Hibernate Hibernate Query LanguageGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 273 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 264 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 206 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 194 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 164 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 153 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 118 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 114 0 0 -
Excel add in development in c and c phần 9
0 trang 109 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 108 0 0