一. 介绍
百度了一下关于在NetBean开发环境里创建EJB的教程,没有找到好的例子,2天的调试过程,写下来帮助后人。
EJB (Enterprise Java Bean) 是一套高扩展性的开发企业级应用的开发结构,通常部署在JBoss, Glassfish, WildFly等服务器上, 更多介绍可参看链接。
1. Entity Bean: 域模型对象,用于实现O/R映射,负责将数据库中的表记录映射为内存中的Entity对象,事实上,创建一个Entity Bean对象相当于新建一条记录,删除一个Entity Bean会同时从数据库中删除对应记录,修改一个Entity Bean时,容器会自动将Entity Bean的状态和数据库同步。
2. Session Bean: 用于实现业务逻辑,它可以是有状态的,也可以是无状态的。每当客户端请求时,容器就会选择一个Session Bean来为客户端服务。Session Bean可以直接访问数据库,但更多时候,它会通过Entity Bean实现数据访问。
3. Message Driven Bean: MDB是EJB2.0中引入的新的企业Bean,它基于JMS消息,只能接收客户端发送的JMS消息然后处理。MDB实际上是一个异步的无状态Session Bean,客户端调用MDB后无需等待,立刻返回,MDB将异步处理客户请求。这适合于需要异步处理请求的场合,比如订单处理,这样就能避免客户端长时间的等待一个方法调用直到返回结果。
二. 创建EJB模块
点击File --> New Project, 在Java EE目录下选择EJB Module:
给项目取一个名字Sample-EJB,选择合适的项目目录,然后点击下一步
选择Server, 我选的是GlassFish Server 4.1, Java EE Version是Jave EE 7.
三. 创建Application Class Library项目
点击File --> New Project, 在Java目录下选择Java Class Library:
给项目取一个名字Sample-Client,选择合适的项目目录,然后点击Next:
四. 创建Entity Bean
接下来创建一个Peoplede的entity class. Antity Class 一般对应了数据库的一个表,当你创建Entity Class 后NetBeans IDE会给该类添加@Entity标记来表示它是一个Entity类.
每一个Entity Class都必须有primary key. IDE会添加@ID标签在primary key上面, @GeneratedValue来表示primary key 是怎么产生的.
右击Projects窗口的Sample-Client --> New --> Other选择Persistence目录里的Entity Class:
下面的Create Persistence Unit可以不选择!
当点击Finish后,IDE会打开People.java文件编辑器,对People.java做如下修改:
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package com.bruceyo.example.entity; 7 8 import java.io.Serializable; 9 import javax.persistence.Entity; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.GenerationType; 12 import javax.persistence.Id; 13 14 /** 15 * 16 * @author bruce_000 17 */ 18 @Entity 19 public class People implements Serializable { 20 private static final long serialVersionUID = 1L; 21 @Id 22 @GeneratedValue(strategy = GenerationType.AUTO) 23 private Long id; 24 private String firstName; 25 26 public String getFirstName() { 27 return firstName; 28 } 29 30 public void setFirstName(String firstName) { 31 this.firstName = firstName; 32 } 33 34 public String getLastName() { 35 return lastName; 36 } 37 38 public void setLastName(String lastName) { 39 this.lastName = lastName; 40 } 41 private String lastName; 42 43 public Long getId() { 44 return id; 45 } 46 47 public void setId(Long id) { 48 this.id = id; 49 } 50 51 @Override 52 public int hashCode() { 53 int hash = 0; 54 hash += (id != null ? id.hashCode() : 0); 55 return hash; 56 } 57 58 @Override 59 public boolean equals(Object object) { 60 // TODO: Warning - this method won't work in the case the id fields are not set 61 if (!(object instanceof People)) { 62 return false; 63 } 64 People other = (People) object; 65 if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 66 return false; 67 } 68 return true; 69 } 70 71 @Override 72 public String toString() { 73 return "com.bruceyo.example.entity.Customer[ id=" + id + " ]"; 74 } 75 76 }
五. 创建Session Bean
EJB3.1能够轻松创建stateless的session bean, stateless session bean不保存相关用户的状态. 但它可以保存它的实例的状态.
通常用EJB Container来创建bean对象池,然后用对象池里的对象来处理用户请求.
右击Projects窗口的Sample-EJB --> New --> Other选择Enterprise JavaBean目录里的Session Bean:
给EJB取名字叫AccountService, 并写上包名; 选择Stateless, 同时下面的Remote in Project要选上!
按finish后,IDE会打开AccountService.java文件编辑器, 修改文件如下:
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package com.bruceyo.sample.service; 7 8 import com.bruceyo.example.entity.People; 9 import javax.ejb.Stateless; 10 import javax.persistence.EntityManager; 11 import javax.persistence.PersistenceContext; 12 13 /** 14 * 15 * @author bruce_000 16 */ 17 @Stateless 18 public class AccountService implements AccountServiceRemote { 19 20 @PersistenceContext 21 private EntityManager em; 22 23 @Override 24 public People createAccount(String firstName, String lastName) { 25 People customer = new People(); 26 customer.setFirstName(firstName); 27 customer.setLastName(lastName); 28 29 em.persist(customer); 30 return customer; 31 } 32 33 // Add business logic below. (Right-click in editor and choose 34 // "Insert Code > Add Business Method") 35 }
六. 创建持久单元Persistence Unit
创建一个Persistence Unit作为数据源的容器来管理Entities,
右击Projects窗口的Sample-EJB --> New --> Other选择Persistence目录里的Persistence Unit:
选择Data Source为jdbc/sample; 勾选Drop and Create, 如果数据库里有的话就丢弃原来的,记得查看下jdbc/sample里有哪些表!EJB的
打开刚刚创建的persistence.xml, 去掉Include All Entity Classes的勾,然后添加Entity Class.
结果如下:
七. 部署(Deploy) EJB模块
右键EJB模块,选择Build --> Deploy, NetBeans会自动部署EJB服务,可以到Services窗口查看GlassFish本地服务器有没有自动开启.
八. 创建一个Servlet Class来测试EJB
1, 创建一个Web Module:
点击File --> New Project, 在Java Web目录下选择Web Application:
添加项目名字Sample-Web, 选择合适的项目位置:
点击下一步,添加Context Path: /Sample-Web, 部署Web Application的时候要用到这个路径访问网站.
2, 创建 Servlet
右击Projects窗口的Sample-EJB --> New --> Other选择Enterprise JavaBean目录里的Session Bean:
点击Next--> Finish后,IDE打开AccountController.java的编辑器,修改如下:
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package com.bruceyo.example.tester; 7 8 import com.bruceyo.example.entity.People; 9 import com.bruceyo.sample.service.AccountServiceRemote; 10 import java.io.IOException; 11 import java.io.PrintWriter; 12 import javax.ejb.EJB; 13 import javax.servlet.ServletException; 14 import javax.servlet.annotation.WebServlet; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 19 /** 20 * 21 * @author jGauravGupta 22 */ 23 @WebServlet(name = "AccountController", urlPatterns = {"/AccountController"}) 24 public class AccountController extends HttpServlet { 25 @EJB 26 private AccountServiceRemote accountService; 27 28 29 /** 30 * Handles the HTTP <code>POST</code> method. 31 * 32 * @param request servlet request 33 * @param response servlet response 34 * @throws ServletException if a servlet-specific error occurs 35 * @throws IOException if an I/O error occurs 36 */ 37 @Override 38 protected void doPost(HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException { 40 response.setContentType("text/html;charset=UTF-8"); 41 try (PrintWriter out = response.getWriter()) { 42 String firstName = request.getParameter("firstName"); 43 String lastName = request.getParameter("lastName"); 44 45 People customer = accountService.createAccount(firstName, lastName); 46 out.println("Customer["+customer.getFirstName()+"] : " + customer.getId()); 47 } 48 } 49 50 }
九. 添加HTML页面
编辑Sample-Web里的Index.html网页文件如下:
1 <!DOCTYPE html> 2 <!-- 3 To change this license header, choose License Headers in Project Properties. 4 To change this template file, choose Tools | Templates 5 and open the template in the editor. 6 --> 7 <html> 8 <head> 9 <title>TODO supply a title</title> 10 <meta charset="UTF-8"> 11 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 12 </head> 13 <body> 14 <form action="AccountController" method="post" > 15 <input type="text" name="firstName" placeholder="First Name" /> 16 <input type="text" name="lastName" placeholder="Last Name"/> 17 <input type="submit" value="Create"/> 18 </form> 19 </body> 20 </html>
然后右击Sample-Web, Build --> Deploy --> Run或在浏览器输入网址 http://localhost:8080/Sample-Web/
十. 运行代码结果
此时如果查看Services --> Databases --> jdbc:derby... --> sample --> People, 能看到JPA已经添加了一条数据到数据库