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>