IT Research Department @BKAP 2015 Page 1 / 25 Lab 09 - 10 Persistence of Entities Advanced Persistence Concepts Mục tiêu - Tạo một Entity Class và Persistence Unit trong ứng dụng Ent
Trang 1IT Research Department @BKAP 2015 Page 1 / 25
Lab 09 - 10
Persistence of Entities
Advanced Persistence Concepts
Mục tiêu
- Tạo một Entity Class và Persistence Unit trong ứng dụng Enterprise
- Tạo liên kết giữa các thực thể
- Tạo Web Client để test
Phần I Bài tập step by step
Bài 6.1
Tạo một Entity Class từ một bảng trong Database và tạo Persistence Unit từ JNDI
Step 1: Tạo Database CRMDB
Tạo bảng Customer có các trường
CustomerId: Int, Primary Key, Identity(1,1)
CustomerName: Nvarchar(50)
Trang 2Step 2: Tạo JDBC Connection Pools có tên CRMPool kết nối với CRMDB
Step 3: Tạo JDBC Resources có tên jdbc/CRMDS với Pool Name là CRMDB
Step 4: Tạo ứng dụng Enterprise CRMApplication
Trang 3IT Research Department @BKAP 2015 Page 3 / 25
Ứng dụng sau khi hoàn thành
Step 5: Tạo Entity Class Customer và Persistence Unit
CRMApplication-ejb New Other Persistence Entity Class
Trang 5IT Research Department @BKAP 2015 Page 5 / 25
Ứng dụng sau khi hoàn thành
Trang 6public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
//Map id voi column CustomerID
private String customerName;
//Getter and Setter
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Customer)) {
return false;
}
Customer other = (Customer) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
Trang 7IT Research Department @BKAP 2015 Page 7 / 25
//Override phuong thuc toString
@Override
public String toString() {
return "entity.Customer[ id=" + id + " , name=" + customerName + " ]";
Step 6: Tạo Stateless Session Bean với tên CustomerBean thực hiện tạo một Customer
CRMApplication-ejb New Other Enterprise JavaBeans Session Bean
Trang 8 Khai báo phương thức tạo Customer ở CustomerBeanLocal.java
package beanpack;
Trang 9IT Research Department @BKAP 2015 Page 9 / 25
public interface CustomerBeanLocal {
public void createCustomer(Customer customer);
Step 7: Tạo form nhập thông tin Customer: CustomerForm.jsp
CRMApplication-war Web Pages New Other Web JSP
Trang 10<title>Create Customer</title>
</head>
<body>
<h2>Create Customer</h2>
<form action="CustomerServlet" method="Post">
Customer Name: <input type="text" name="customerName"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Step 8: Tạo Servlet CustomerServlet
CRMApplication-war New Other Web Servlet
Trang 11IT Research Department @BKAP 2015 Page 11 / 25
CustomerServlet.java
Trang 12* @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");
String customerName = request.getParameter("customerName");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here You may use following sample code */
//Khoi tao Customer Entity va set thuoc tinh customerName cho no
Customer customer = new Customer();
Trang 13IT Research Department @BKAP 2015 Page 13 / 25
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods Click on the + sign on the left to edit the code.">
/**
* 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 {
* @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 {
public String getServletInfo() {
return "Short description";
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>servlet.CustomerServlet</servlet-class>
</servlet>
Trang 14<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/CustomerServlet</url-pattern>
Trang 15IT Research Department @BKAP 2015 Page 15 / 25
Step 2: Tạo Address Entity trong package entity của module CRMApplication-ejb (bài 6.1)
Tạo mối liên kết giữa Address Entity và Customer Entity
CRMApplication-ejb Source Packages entity RC New Other
Persistence Entity Class
Trang 17IT Research Department @BKAP 2015 Page 17 / 25
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "AddressId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
private String addressLine;
//Lien ket Address voi Customer
@ManyToOne
@JoinColumn(name = "CustomerId")
private Customer customer;
//Getter and Setter
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType;
Trang 18}
public String getAddressLine() {
return addressLine;
}
public void setAddressLine(String addressLine) {
this.addressLine = addressLine;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Address)) {
return false;
}
Address other = (Address) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
public String toString() {
return "entity.Address[ id=" + id + " ]";
Trang 19IT Research Department @BKAP 2015 Page 19 / 25
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
//Map id voi column CustomerID
@Id
@Column(name = "CustomerId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//Map customerName voi column CustomerName
@Column(name = "CustomerName")
private String customerName;
//Map One to Many voi Address
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "Customer")
private List<Address> listAddress;
//Constuctor
public Customer() {
this.listAddress = new ArrayList<>();
}
//Getter and Setter
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
Trang 20this.listAddress = listAddress;
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Customer)) {
return false;
}
Customer other = (Customer) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
public String toString() {
return "entity.Customer[ id=" + id + " , name=" + customerName + " ]";
public interface CustomerBeanLocal {
public void createCustomer(Customer customer, List<Address> listAddress);
}
CustomerBean.java
package beanpack;
Trang 21IT Research Department @BKAP 2015 Page 21 / 25
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Customer</title>
</head>
<body>
<h2>Create Customer</h2>
<form action="CustomerServlet" method="Post">
Customer Name: <input type="text" name="customerName"/><br/>
Present Address: <input type="text" name="presAddress"/><br/>
Permanent Address: <input type="text" name="permAddrss"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
CRMApplication-war CustomerServlet.java
Trang 22* @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");
String customerName = request.getParameter("customerName");
String presAddress = request.getParameter("presAddress");
String permAddress = request.getParameter("permAddress");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here You may use following sample code */
//Khoi tao Customer Entity va set thuoc tinh customerName cho no
Customer customer = new Customer();
customer.setCustomerName(customerName);
List<Address> listAddress = new ArrayList<>();
//Add Present Address
if (presAddress != null && !presAddress.isEmpty()) {
Address addr = new Address();
addr.setAddressType("Present");
addr.setAddressLine(presAddress);
listAddress.add(addr);
Trang 23IT Research Department @BKAP 2015 Page 23 / 25
}
//Add Permanent Address
if (permAddress != null && !permAddress.isEmpty()) {
Address addr = new Address();
* @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 {
* @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 {
Trang 24@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Step 5: Build and Run Application
Trang 25IT Research Department @BKAP 2015 Page 25 / 25