当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > JAVA > EJB > 正文

EJB(Enterprise JavaBeans)入门(9)

http://www.zk168.com.cn  招考学习网 2006-4-11 3:06:09
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
EJB(Enterprise JavaBeans)入门(9)

Bean-Managed Persistence(BMP)
本章讲述了Bean-Managed 和 Container-Managed persistence 的区别 、如何实现 Bean-managed persistence 的实体 Bean

本章讲述内容

  • Bean-Managed 和 Container-Managed persistence 的区别
  • 如何实现 Bean-managed persistence 的实体 Bean

Bean-Managed Persistence

  • 容器管理 Bean 的生命周期--何时创建、载入、存储和删除
  • 开发人员处理其机制--如何创建、载入、存储和删除
  • BMP = [业务逻辑] + [存储逻辑]

为何使用 BMP?

  • 为了存储 CMP 现阶段所不能处理的复杂的 Bean
  • 包含如下数据的实体 Bean:
    • 内嵌 Java 对象
    • 对象的集合
    • 对其它 Bean 的引用
    • ...
  • CMP 所不支持的数据源

注意!
BMP 将您紧紧地绑定到某个特定的存储机制上--可能将您限制于一个特定的平台!

生命周期

创建 Bean

  • 在 Home 接口的 Create 方法在 Bean 实例中有对应的 ejbCreate 方法--ejbCreate 方法返回一个主键对象
  • 可以有任意数目的 create 方法
public ClientKey ejbCreate(int id, String first, String last)
throws CreateException, RemoteException {
if (new File("client." + id).exists())
throw new CreateException();
setFirstName(first);
setLastName(last);
setPhoneNumber(null);
return new ClientKey(id);
}

· 存储
ejbStore() 方法完成工作--entityContext 包含主键

public void ejbStore() throws RemoteException {
try {
ObjectOutputStream out
= new ObjectOutputStream(new FileOutputStream("client." + getId()));
out.writeObject(getFirstName());
out.writeObject(getLastName());
out.writeObject(getPhoneNumber());
out.close();
} catch (IOException e) {
throw new RemoteException("An IO error occurred", e);
}
}
private int getId() {
return ((ClientKey)getEntityContext().getPrimaryKey()).id;
}

· 载入

  • 在进行载入前,entityContext 将被设置--entityContext 包含主键
  • 使用主键的值来查找和载入 Bean
public void ejbLoad() throws RemoteException {
try {
ObjectInputStream in
= new ObjectInputStream(new FileInputStream("client." + getId()));
setFirstName((String)in.readObject());
setLastName((String)in.readObject());
setPhoneNumber((String)in.readObject());
in.close();
} catch (ClassNotFoundException e) {
// Shouldn't happen -- We're dealing with Strings here.
} catch (IOException e) {
throw new RemoteException("An IO error occurred", e);
}
}

· 删除 ejbRemove() 方法用来完成删除工作

public void ejbRemove() throws RemoteException, RemoveException {
File file = new File("client." + getId());
if (!file.exists()) throw new RemoveException();
file.delete();
}

· XML 示例

public void ejbStore() throws RemoteException {
try {
PrintWriter out
= new PrintWriter(new FileWriter("client." + getId()));
out.println("<?xml version=\"1.0\" ?>\n");
out.print("<client id=\"");
out.print(getId());
out.print("\">\n\t<first>");
out.print(getFirstName());
out.print("</first>\n\t<last>");
out.print(getLastName());
out.print("</last>\n\t<phone>");
if (getPhoneNumber() != null) out.print(getPhoneNumber());
out.println("</phone>\n</client>");
out.close();
} catch (IOException e) {
e.printStackTrace();
throw new RemoteException("An IO error occurred", e);
}
}
<?xml version="1.0" ?>

<client id="999999">
<first>Kenneth</first>
<last>Beaton</last>
<phone>555-9999</phone>
</client>

· Finder 方法

  • 对每个在 Home 接口中声明的 find 方法必需有一个对应的 ejbFind 方法
  • 单个查找--返回的是找到对象的主键
  • 多个查找--返回的是匹配对象的主键的枚举
  • 容器将载入 Bean--一个 Bean 从池中取出,赋以主键(通过 entityContext)然后被调用载入方法

· 单个查找

public ClientKey ejbFindByPrimaryKey(ClientKey key)
throws RemoteException, FinderException {
if (!new File("client." + key.id).exists())
throw new FinderException();
return key;
}
public ClientKey ejbFindById(int id)
throws RemoteException, FinderException {
if (!new File("client." + id).exists())
throw new FinderException();
return new ClientKey(id);
}

· 多个查找

public Enumeration ejbFindAll()
throws RemoteException, FinderException {
String[] files = new File(".").list(new FilenameFilter() {
public boolean accept(File directory, String name) {
return name.startsWith("client.");
}
});

Vector keys = new Vector();
for (int index=0;index<files.length;index++) {
String name = files[index];
int id=Integer.parseInt(name.substring(name.indexOf('.') + 1));
keys.addElement(new ClientKey(id));
}

return keys.elements();
}

· 多个查找 (JDBC 版本)

public Enumeration ejbFindAll() throws RemoteException, FinderException {
Vector keys = new Vector();
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getDataSource().getConnection();
statement = connection.prepareStatement("Select ID from CLIENT");
ResultSet results = statement.executeQuery();
while (results.next()) {
int id = results.getInt(1);
keys.addElement(new ClientKey(id));
}
results.close();
} catch (SQLException e) {
} finally {
try {statement.close();} catch (SQLException e) { }
try {connection.close();} catch (SQLException e) {}
}
return keys.elements();
}

· JDBC 载入示例

public void ejbLoad() throws RemoteException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getDataSource().getConnection();
statement = connection
.prepareStatement("Select FIRST, LAST, PHONE from CLIENT where ID=?");
statement.setInt(1, getId());
ResultSet results = statement.executeQuery();
if (results.next()) {
setFirstName(results.getString(1));
setLastName(results.getString(2));
setPhoneNumber(results.getString(3));
} else {
throw new RemoteException("The row cannot be found!");
}
results.close();
} catch (SQLException e) {
...
}

· 查找的过程

  • 有一个 Bean 实例被用来查找出其它实例--Bean 必需被从池中取出来进行查找工作
  • Bean 实例必需自己将自己载入--可能开销会很大
  • 使用 JDBC:
    • 一个调用用来取出所有您所关心的 Bean 的 id
    • 对每个 id 又是一个调用来取出一行数据
    • 总共需要 n+1 个对数据库的访问来载入 n 个 Bean!

· 本章讲述内容

  • Bean-Managed Persistence 允许 Bean 开发人员指定如何实现数据的存储
  • ejbLoad, ejbStore 和 ejbRemove 方法必需先从 entityContext 中获得主键
  • BMP create 方法必需返回主键
  • 对应每个在 Home 接口中的 find* 方法必需有一个对应的 ejbFind* 方法--开发人员必需至少提供 ejbFindByPrimaryKey
(全文完)
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
·环保企业人力资源开发与管理的实证研究————巨龙公
·重油制气污水处理系统(A/O)技术改造
·BOG压缩机在液化石油气基地的应用
·英美CPA管理模式及其启示
·改造NERA微波公务信道为国产监控信道
·LFCB-102型微波分相差动保护的应用
·EAStudio让电子商务网站如虎添翼
·基于PB6和ORACLE8开发“劳动信息管理系统”
·巧解Pretty  Park 病毒一例
·利用TDC组件实现对WEB页面的交互操作
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>