ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [转载]在容器外使用EJB 3.0 Persistence

[转载]在容器外使用EJB 3.0 Persistence

原创 Linux操作系统 作者:dinner1007 时间:2019-01-07 19:18:06 0 删除 编辑
在容器外使用EJB 3.0 Persistence
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




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)

  6.创建一个客户端

  7.可以运行程序了

  最后的包结构如下:

image

  你体验过EJB3吗?发表下感想吧。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131523/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-08-23

  • 博文量
    1714
  • 访问量
    1302117