日志文章

2008年03月22日 21:10:00

Ejb3.0 step by step <2> entity bean

wangwj/scttsc
valenwon/cnoug
22/Mar/08
Oracle10r2/jboss4.22
这回,我来实现一个更复杂的entity bean ,首先创建一个新表:
create table EMPLOYEE
(
EMPLOYEEID NUMBER(
19) not null,
ADDRESS   VARCHAR2(
255 CHAR),
NAME     VARCHAR2(
255 CHAR),
STATUS   NUMBER(
19),
REMARK   CLOB,
BIRTHDAY   DATE,
PHONE     VARCHAR2(
255 CHAR),
CELLPHONE VARCHAR2(
255 CHAR),
STATUSDATE TIMESTAMP(
6)
)



里面基本包含常用字段,其中还包括CLOB以及TIMESTAMP。我将映射这个employee表,并实现基本的UIDQ

EJB3相比EJB2.1在这方面做了很多改进,在实验过程中,我们会发现什么配置文件、部署文件通通都可以用简洁的annotation代替。我以前的项目中,我不喜欢用EJB2.1entity bean,而用hibernate,最多也部署一些Message driven beanSession bean,现在又多了一种官方选择。
由于本例的环境和bean和第一例及其相似,所以不在详细记录(提供完整代码下载)
创建bean
package com.scttsc.domain;



import java.sql.Timestamp;

import java.util.Date;



import javax.persistence.Basic;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Lob;

import javax.persistence.SequenceGenerator;

import javax.persistence.GenerationType;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;





@Entity

@Table(name = "EMPLOYEE")

@SequenceGenerator(name = "SEQ_EMPLOYEE", sequenceName = "SEQ_EMPLOYEE")

public class Employee implements java.io.Serializable {

    private static final long serialVersionUID = 1L;



    private long employeeid;

    private String name;

    private Date birthday;

    private String address;

    private String phone;

    private String cellphohe;

    private long status;

    private Date statusdate;

    private String remark;

    @Id

    @Column(name = "EMPLOYEEID")

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_EMPLOYEE")

    public long getEmployeeid() {

        return this.employeeid;

    }

    public void setEmployeeid(long employeeid) {

        this.employeeid = employeeid;

    }

    @Column(name = "NAME")

    @Basic(optional = true)

    public String getName() {

        return this.name;

    }

    public void setName(String name) {

        this.name = name;

    }



    @Column(name = "BIRTHDAY")

    @Temporal(TemporalType.DATE)

    public Date getBirthday() {

        return this.birthday;

    }

    public void setBirthday(Date Birthday) {

        this.birthday = Birthday;

    }

    @Column(name = "ADDRESS")

    public String getAddress() {

        return this.address;

    }

    public void setAddress(String address) {

        this.address = address;

    }

    @Column(name = "PHONE")

    public String getPhone() {

        return this.phone;

    }

    public void setPhone(String phone) {

        this.phone = phone;

    }

    @Column(name = "CELLPHONE")

    public String getCellphohe() {

        return this.cellphohe;

    }

    public void setCellphohe(String cellphone) {

        this.cellphohe = cellphone;

    }

    @Column(name = "STATUS")

    @Basic(optional = true)

    public long getStatus() {

        return this.status;

    }



    public void setStatus(long status) {

        this.status = status;

    }

    @Column(name = "STATUSDATE")

    @Temporal(TemporalType.TIMESTAMP)

    public Date getStatusdate() {

        return this.statusdate;

    }

    public void setStatusdate(Date statusdate) {

        this.statusdate = statusdate;

    }

    @Lob

    @Column(name = "REMARK")

    @Basic(fetch = FetchType.LAZY)

    public String getRemark() {

        return this.remark;

    }

    public void setRemark(String remark) {

        this.remark = remark;

    }

}

下面依次解释用到的annotation

@Entity
表示这个是一个entity bean
@Table(name = "EMPLOYEE")
要映射什么表
@SequenceGenerator(name = "SEQ_EMPLOYEE", sequenceName = "SEQ_EMPLOYEE")

主键对应的序列生成器

@Id
它是主键
@Column(name = "EMPLOYEEID")
对应什么字段
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_EMPLOYEE")

它对应的序列生成器

@Basic(optional = true)

必填项

@Temporal(TemporalType.DATE)

允许你映射java date到数据库的date类型上

@Lob
@Basic(fetch = FetchType.LAZY)

它是LOB类型的,延迟读取

其它一些在本例没用到的注解

@IdClass@Embeddedld 定义复合主键

@Transient非持久化对象

@Enumerate 映射枚举类型

@SecondaryTable多表映射

OK,省略了配置文件,方便了很多。

建立Session BEAN,和前文类似,不做介绍()

创建一个客户端Client来测试。

package com.scttsc.client;



import java.text.SimpleDateFormat;

import java.util.Date;



import javax.naming.Context;

import com.scttsc.domain.Employee;

import com.scttsc.useragent.EmployeeRemote;



public class Client {

    public static void main(String[] args) {

        try {

              Context jndiContext = getInitialContext();

              Object ref = jndiContext.lookup("EmployeeBean/remote");

              EmployeeRemote dao = (EmployeeRemote) ref;

              // create a employee

              Employee em = new Employee();



              em.setAddress("chengdu");

              em.setName("wwj");

              em.setStatus(1);

              em.setRemark("this is a new guy in our company");

              em.setBirthday(new Date());

              em.setStatusdate(new Date());

             

              long i = dao.createEmployee(em);

              // check it

              System.out.println("The id of new employee is " + i);

              Employee em1 = dao.findEmployee(i);

              System.out.println(em1.getAddress());

              System.out.println(em1.getName());

              System.out.println(em1.getRemark());

              System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(em

                      .getBirthday()));

              System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(em

                      .getStatusdate()));

        //update it

              em1.setAddress("beijing");

              dao.updateEmployee(em1);

        //check again

              Employee em2 = dao.findEmployee(i);

              System.out.println(em2.getAddress());

             



        } catch (javax.naming.NamingException ne) {

              ne.printStackTrace();

        }

    }



    public static Context getInitialContext()

              throws javax.naming.NamingException {

        return new javax.naming.InitialContext();

    }

}



创建一个对象,查询出来,更新地址,再次查询。

ant 编译运行,结果如下:

  [java] The id of new employee is 50
  [java] chengdu

  [java] wwj
  [java] this is a new guy in our company
  [java] 2008-03-22
  [java] 2008-03-22
  [java] Beijing

代码下载:
orgframe.rar


另外,我发现测试表数据时有时无,后来发现那是应为在persistence的行为是AUTO create and drop,每当deploy的时候,就会重建表。这一点可以在配置文件中看到,同时在dba_objectsCREATED字段记录变化的时间。

Tags: ejb3  

类别: JAVA ee |  评论(0) |  浏览(4872) |  收藏
发表评论