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. JAVA天堂
Lucas Jellema提到的步驟如下:
1.下載下傳和安裝 Java 5 (JDK 5.0/JRE 5.0)
JAVA天堂
2.下載下傳和配置 GlassFish
JAVA天堂
GlassFish:https://glassfish.dev.java.net/
3.建立Entities - POJOs with annotations that link them to database objects
JAVA天堂
比如其中的員工類:
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") JAVA天堂
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;
JAVA天堂
}
@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) { JAVA天堂
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
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
nl.amis.ejb30.hrm.Department
nl.amis.ejb30.hrm.Employee
5.建立一個 Business Service class ,提供 Entitiy Services (好像一個out-of-container的Session Bean) JAVA天堂
6.建立一個用戶端
JAVA天堂
7.可以運作程式了
最後的包結構如下:
你體驗過EJB3嗎?發表下感想吧。