Enterprise Application Development in Java EE
Lab 11
Query and Criteria API
Mục tiêu
- Sử dụng Criteria Query API để viết các truy vấn
Phần I Bài tập step by step
Bài 7.1
Xây dựng truy vấn tìm danh sách customer theo Customer Name trong ứng dụng EJB xây
dựng ở bài 6.1 và 6.2
Step 1: Khai báo phương thức searchCustomer ở CRMApplication-ejb
CustomerBeanLocal.java
CustomerBeanLocal.java
package beanpack;
import entity.Address;
import entity.Customer;
import java.util.List;
import javax.ejb.Local;
/**
*
* @author Quang
*/
@Local
public interface CustomerBeanLocal {
public void createCustomer(Customer customer, List<Address> listAddress);
public List<Customer> searchCustomer(String custName);
}
IT Research Department
@BKAP 2015
Page 1 / 7
Enterprise Application Development in Java EE
Step 2: Thực thi phương thức searchCustomer ở CustomerBean.java. Sử dụng Criteria
query để truy vấn các customer theo Customer Name
CustomerBean.java
package beanpack;
import entity.Address;
import entity.Customer;
import entity.Customer_;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
/**
*
* @author Quang
*/
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CustomerBean implements CustomerBeanLocal {
//Persistence
@PersistenceContext(unitName = "CRMApplication-ejbPU")
EntityManager em;
@Override
public void createCustomer(Customer customer, List<Address> listAddress) {
customer.setListAddress(listAddress);
for (Address addr : listAddress) {
addr.setCustomer(customer);
}
em.persist(customer);
}
@Override
public List<Customer> searchCustomer(String custName) {
List<Customer> listCus = null;
CriteriaBuilder cb = em.getCriteriaBuilder();
//Tao doi tuong CriteriaQuery tu cb
CriteriaQuery<Customer> cq = cb.createQuery(Customer.class);
//Tao root interface tu criteria query
Root<Customer> cust = cq.from(Customer.class);
// Tao ParameterExpression
ParameterExpression<String> p = cb.parameter(String.class);
//dieu kien truy van
cq.select(cust).where(cb.like(cust.get(Customer_.customerName), "%" + custName + "%"));
IT Research Department
@BKAP 2015
Page 2 / 7
Enterprise Application Development in Java EE
//Tao loai truy van
TypedQuery<Customer> query = em.createQuery(cq);
//Nhan ket qua
listCus = query.getResultList();
return listCus;
}
}
Step 3: Tạo form nhập thông tin Customer Name để search
CRMApplication-war Web Pages New Other Web JSP
CustomerSearch.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Search Page</title>
</head>
<body>
<form name="CustSearchForm" method="POST" action="CustomerSearch">
<table colspan="2">
<tr>
<td>Customer Name: </td>
<td><input type="text" size="20" name="customerName"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Search"/></td>
</tr>
</table>
</form>
</body>
</html>
Step 4: Tạo Servlet CustomerSearchServlet thực hiện việc search Customer theo Customer
Name
CRMApplication-war Source Packages Servlet New Other Web Servlet
IT Research Department
@BKAP 2015
Page 3 / 7
Enterprise Application Development in Java EE
CustomerSearchServlet.java
package servlet;
import beanpack.CustomerBeanLocal;
IT Research Department
@BKAP 2015
Page 4 / 7
Enterprise Application Development in Java EE
import entity.Address;
import entity.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Quang
*/
public class CustomerSearch extends HttpServlet {
//Goi Enterprise Bean
@EJB
private CustomerBeanLocal customerBean;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//Nhan thong tin Customer Name nhap vao de search
String custName = request.getParameter("customerName");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet CustomerSearch</title>");
out.println("</head>");
out.println("<body>");
//Goi phuong thuc searchCustomer
List<Customer> listCus = customerBean.searchCustomer(custName);
//Hien thi danh sach Customer tim duoc
out.println("<table border='1'><tr><th>Customer Name</th><th>Address</th></tr>");
for (Customer cust : listCus) {
out.println("<tr><td>"+cust.getCustomerName()+"</td><td>");
for (Address addr : cust.getListAddress()) {
out.println("Address Type - "+addr.getAddressType());
out.println("| Address Line - "+addr.getAddressLine() +"
");
}
out.println("</td></tr>");
}
out.println("</table>");
IT Research Department
@BKAP 2015
Page 5 / 7
Enterprise Application Development in Java EE
out.println("</body>");
out.println("</html>");
}
}
//
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Ứng dụng sau khi hoàn thành
IT Research Department
@BKAP 2015
Page 6 / 7
Enterprise Application Development in Java EE
Step 5: Build and Run Application
IT Research Department
@BKAP 2015
Page 7 / 7