一. 介绍
百度了一下关于在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做如下修改:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bruceyo.example.entity; import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; /**
*
* @author bruce_000
*/
@Entity
public class People implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName; public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
}
private String lastName; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} @Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
} @Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof People)) {
return false;
}
People other = (People) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
} @Override
public String toString() {
return "com.bruceyo.example.entity.Customer[ id=" + id + " ]";
} }
People.java代码
五. 创建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文件编辑器, 修改文件如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bruceyo.sample.service; import com.bruceyo.example.entity.People;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; /**
*
* @author bruce_000
*/
@Stateless
public class AccountService implements AccountServiceRemote { @PersistenceContext
private EntityManager em; @Override
public People createAccount(String firstName, String lastName) {
People customer = new People();
customer.setFirstName(firstName);
customer.setLastName(lastName); em.persist(customer);
return customer;
} // Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
}
AccountService.Java代码
六. 创建持久单元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的编辑器,修改如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bruceyo.example.tester; import com.bruceyo.example.entity.People;
import com.bruceyo.sample.service.AccountServiceRemote;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*
* @author jGauravGupta
*/
@WebServlet(name = "AccountController", urlPatterns = {"/AccountController"})
public class AccountController extends HttpServlet {
@EJB
private AccountServiceRemote accountService; /**
* 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 {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName"); People customer = accountService.createAccount(firstName, lastName);
out.println("Customer["+customer.getFirstName()+"] : " + customer.getId());
}
} }
AccountController.java代码
九. 添加HTML页面
编辑Sample-Web里的Index.html网页文件如下:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="AccountController" method="post" >
<input type="text" name="firstName" placeholder="First Name" />
<input type="text" name="lastName" placeholder="Last Name"/>
<input type="submit" value="Create"/>
</form>
</body>
</html>
然后右击Sample-Web, Build --> Deploy --> Run或在浏览器输入网址 http://localhost:8080/Sample-Web/
十. 运行代码结果
-->
此时如果查看Services --> Databases --> jdbc:derby... --> sample --> People, 能看到JPA已经添加了一条数据到数据库