现在的位置: 首页 > 综合 > 正文

EJB3.0实体Bean(二)

2013年01月01日 ⁄ 综合 ⁄ 共 5378字 ⁄ 字号 评论关闭
6.4 单表映射的实体Bean
开发前先介绍需要映射的数据库表
person
字段名称 字段类型属性 描述
personid (主键) Int(11) not null 人员ID
PersonName Varchar(32) not null 姓名
sex Tinyint(1) not null 性别
age Smallint(6) not null 年龄
birthday datetime null 出生日期
建立与Person表进行映射的实体Bean
Person.java
package com.foshanshop.ejb3.bean;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Person")
public class Person {
private Integer personid;
private String name;
private boolean sex;
Jboss EJB3.0实例教程
版权所有:黎活明
private Short age;
private Date birthday;
@Id
@GeneratedValue
public Integer getPersonid() {
return personid;
}
public void setPersonid(Integer personid) {
this.personid = personid;
}
@Column(name = "PersonName",nullable=false,length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable=false)
public boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Column(nullable=false)
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
通过Person类可以看到开发实体Bean非常简单,就像开发一般的java bean一样。@Entity注释指明这是一个实
Jboss EJB3.0实例教程
版权所有:黎活明
体Bean,每个实体Bean类映射数据库中的一个表,@Table注释的name属性指定映射的数据表名称,Person类
映射的数据表为Person。实体Bean的每个实例代表数据表中的一行数据,行中的一列对应实例中的一个属性。
@Column注释定义了映射到列的所有属性,如列名是否唯一,是否允许为空,是否允许更新等,他的属性介绍如
下:
·name: 映射的列名。如:映射Person表的PersonName列,可以在name属性的getName 方法上面加入
@Column(name = "PersonName"),如果不指定映射列名,容器将属性名称作为默认的映射列名。
·unique: 是否唯一
·nullable: 是否允许为空
·length: 对于字符型列,length属性指定列的最大字符长度
·insertable: 是否允许插入
·updatable: 是否允许更新
·columnDefinition: 定义建表时创建此列的DDL
·secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字。
@Id 注释指定personid属性为表的主键,它可以有多种生成方式:
·TABLE:容器指定用底层的数据表确保唯一。
·SEQUENCE:使用数据库的SEQUENCE 列来保证唯一
·IDENTITY:使用数据库的INDENTIT列来保证唯一
·AUTO:由容器挑选一个合适的方式来保证唯一
·NONE:容器不负责主键的生成,由调用程序来完成。
@GeneratedValue注释定义了标识字段的生成方式,本例personid的值由MySQL数据库自动生成。
为了使用上面的实体Bean,我们定义一个Session Bean作为他的使用者。下面是Session Bean 的业务接口,他定
义了两个业务方法insertPerson和getPersonNameByID,insertPerson用作添加一个Person,getPersonNameByID 根
据personid获取人员的姓名。
PersonDAO.java
package com.foshanshop.ejb3;
import java.util.Date;
public interface PersonDAO {
public boolean insertPerson(String name, boolean sex,short age, Date birthday);
public String getPersonNameByID(int personid);
}
下面是Session Bean的实现
PersonDAOBean.java
package com.foshanshop.ejb3.impl;
import java.util.Date;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.foshanshop.ejb3.PersonDAO;
import com.foshanshop.ejb3.bean.Person;
Jboss EJB3.0实例教程
版权所有:黎活明
@Stateless
@Remote ({PersonDAO.class})
public class PersonDAOBean implements PersonDAO {
@PersistenceContext
protected EntityManager em;
public String getPersonNameByID(int personid) {
Person person = em.find(Person.class, Integer.valueOf(personid));
return person.getName();
}
public boolean insertPerson(String name, boolean sex,short age, Date birthday) {
try {
Person person = new Person();
person.setName(name);
person.setSex(sex);
person.setAge(Short.valueOf(age));
person.setBirthday(birthday);
em.persist(person);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
上面我们使用到了一个对象:EntityManager em,EntityManager 是由EJB容器自动地管理和配置的,不需要用户
自己创建,他用作操作实体Bean。关于他的更多介绍请参考持久化实体管理器EntityManager。
上面em.find()方法用作查询主键ID 为personid的记录。em.persist()方法用作向数据库插入一条记录。大家可能感
觉奇怪,在类中并没有看到对EntityManager em进行赋值,后面却可以直接使用他。这是因为在实体Bean加载
时,容器通过@PersistenceContext注释动态注入EntityManager 对象。
如果persistence.xml文件中配置了多个不同的持久化内容。你需要指定持久化名称注入EntityManager 对象,可以
通过@PersistenceContext注释的unitName属性进行指定,例:
@PersistenceContext(unitName="foshanshop")
EntityManager em;
如果只有一个持久化内容配置,不需要明确指定。
下面是persistence.xml文件的配置:
<persistence>
<persistence-unit name="foshanshop">
<jta-data-source>java:/DefaultMySqlDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
Jboss EJB3.0实例教程
版权所有:黎活明
</properties>
</persistence-unit>
</persistence>
到目前为此,实体bean应用已经开发完成。我们按照上节“实体Bean发布前的准备工作”介绍的步骤把他打成
Jar 文件并发布到Jboss中。
在发布前请检查persistence.xml 文件中使用的数据源是否配置(如何配置数据源请参考 “Jboss 数据源的配置”),
数据库驱动Jar 文件是否放进了[Jboss安装目录]/server/all/lib 目录下(放进后需要重启Jboss)。
因为在persistence.xml 文件中指定的Hibernate 属性是<property name="hibernate.hbm2ddl.auto"
value="create-drop"/>,该属性值指定在实体Bean 发布及卸载时将自动创建及删除表。当实体bean 发布成功
后,我们可以查看数据库中是否生成了Person 表。生成的表如下图:
下面是JSP 客户端代码:
EntityBeanTest.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.foshanshop.ejb3.PersonDAO,
javax.naming.*,
java.util.Properties,
java.util.Date,
java.text.SimpleDateFormat"%>
<%
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx = new InitialContext(props);
try {
Jboss EJB3.0实例教程
版权所有:黎活明
PersonDAO persondao = (PersonDAO) ctx.lookup("PersonDAOBean/remote");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
persondao.insertPerson("黎活明", true, (short)26,formatter.parse("1980-9-30"));//
添加一个人
out.println(persondao.getPersonNameByID(1)); //取personid为1的人
} catch (Exception e) {
out.println(e.getMessage());
}
%>
上面代码往数据库添加一个人,然后取personid 为1 的人员姓名。
本例子的EJB 源代码在EntityBean 文件夹(源代码下载:http://www.foshanshop.net/),项目中使用到的类库
在上级目录lib 文件夹下。要发布本例子EJB (确保配置了环境变量JBOSS_HOME 及启动了Jboss),你可以执行
Ant 的deploy 任务。例子使用的数据源配置文件是mysql-ds.xml,你可以在下载的文件中找到。数据库名为
foshanshop
本例子的客户端代码在EJBTest 文件夹,要发布客户端应用,你可以执行Ant 的deploy 任务。通过
http://localhost:8080/EJBTest/EntityBeanTest.jsp 访问客户端。
【上篇】
【下篇】

抱歉!评论已关闭.