Lucas Jellema 最近写了篇文章,名字叫:Getting Started with EJB 3.0 Persistence out-of-container using the Reference Implementation (GlassFish)。
该文介绍了如何在J2EE Container之外使用EJB 3.0 Persistence,实现了service类和data model.
Lucas Jellema提到的步骤如下:
1.下载和安装 Java 5 (JDK 5.0/JRE 5.0)
2.下载和配置 GlassFish
GlassFish:https://glassfish.dev.java.net/
3.创建Entities - POJOs with annotations that link them to database objects
比如其中的员工类:package nl.amis.ejb30.hrm;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMP")
public class Employee implements Serializable {
private Double comm;
private Long deptno;
private Long empno;
private String ename;
private Timestamp hiredate;
private String job;
private Long mgr;
private Double sal;
public Employee() {
}
public Employee(Long empno) {
this.empno = empno;
}
@Column(name="COMM")
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
@Column(name="DEPTNO")
public Long getDeptno() {
return deptno;
}
public void setDeptno(Long deptno) {
this.deptno = deptno;
}
@Id
@Column(name="EMPNO", nullable=false)
public Long getEmpno() {
return empno;
}
public void setEmpno(Long empno) {
this.empno = empno;
}
@Column(name="ENAME")
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
@Column(name="HIREDATE")
public Timestamp getHiredate() {
return hiredate;
}
public void setHiredate(Timestamp hiredate) {
this.hiredate = hiredate;
}
@Column(name="JOB")
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Column(name="MGR")
public Long getMgr() {
return mgr;
}
public void setMgr(Long mgr) {
this.mgr = mgr;
}
@Column(name="SAL")
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
4.创建 persistence.xml file 用来连接 Entities (或者 Domain Classes 和 POJOs) 和 database
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="pu1">
<!? Provider class name is required in Java SE ?>
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<!? All persistence classes must be listed ?>
<class>nl.amis.ejb30.hrm.Department</class>
<class>nl.amis.ejb30.hrm.Employee</class>
<properties>
<!? Provider-specific connection properties ?>
<property name="jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbc.connection.string" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="jdbc.user" value="scott"/>
<property name="jdbc.password" value="tiger"/>
<!? Provider-specific settings ?>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
5.创建一个 Business Service class ,提供 Entitiy Services (好像一个out-of-container的Session Bean)
6.创建一个客户端
7.可以运行程序了
最后的包结构如下:
你体验过EJB3吗?发表下感想吧。