WEB开发网
开发学院WEB开发Jsp CMP实体bean实战开发 阅读

CMP实体bean实战开发

 2008-01-05 10:32:42 来源:WEB开发网   
核心提示:一个容器治理持续的实体bean答应容器处理一些或者它的全部数据访问逻辑,用容器治理的持续,CMP实体bean实战开发,你需要把实体bean类的一些域公开出来,好让容器在代表bean执行数据库操作时可以设置这些域,设置Primary key field name为productID,7.下面的按next跳过,最后单击fi

  一个容器治理持续的实体bean答应容器处理一些或者它的全部数据访问逻辑。用容器治理的持续,你需要把实体bean类的一些域公开出来,好让容器在代表bean执行数据库操作时可以设置这些域。
  在容器治理持续化的情况下,entity bean类的代码必须满足以下条件。首先,类必须定义为public。此外,这个类还必须实现以下内容:
  1.EntityBean接口
  2.零个或多个ejbCreate方法及ejbPostCreate方法
  3.对应于持续化及关联字段的定义它的get方法及set方法
  4.home方法
  5.business方法
  entity bean类不能实现以下方法:
  1、finder方法
  2、finalize方法
  一个访问方法的命名以get或set开始,后面是首字母大写的持续化及关联字段。例如,对salary字段的访问 方法是getSalary和setSalary。
  本示例描述一个简单的CMP 实体bean使用实例,构建一个简单的产品查询应用,实例中包括的3个主要 文件:
  PRodUCt.java-------远程接口文件
  ProductHome.java--------本地接口文件
  ProductBean.java--------Bean文件
  
  Product.java内容如下
  import javax.ejb.*;
  import java.rmi.RemoteException;
  
  /**
  定义了企业bean的公开商务方法.
  客户端通过远程接口和企业bean交互
  */
  public interface Product extends EJBObject {
  
  // 实体bean域的获得器和设置器方法
  
  public String getName() throws RemoteException;
  public void setName(String name) throws RemoteException;
  public String getDescription() throws RemoteException;
  public void setDescription(String description) throws RemoteException;
  public double getBasePrice() throws RemoteException;
  public void setBasePrice(double price) throws RemoteException;
  public String getProductID() throws RemoteException;
  }
  
  ProductHome.java内容如下
  import javax.ejb.*;
  import java.rmi.RemoteException;
  import java.util.Enumeration;
  public interface ProductHome extends EJBHome {
  /*
  这个方法创建EJB对象
  请注重本地接口返回一个EJB对象,而Bean却没有返回值。这是因为EJB容器负责生成这个EJB对象,而Bean负责初始化。
  @参数productID :产品号 (唯一)
  @参数name:产品名t
  @参数description :产品的描述
  @参数basePrice Base:产品的基础价格
  @返回新创建的EJB对象
  */
  public Product create(String productID, String name, String description, double basePrice) throws CreateException, RemoteException;
  
  public Product findByPrimaryKey(String productID) throws FinderException, RemoteException;
  //定位器方法,容器负责实现。
  }
  
  Productbean.java文件内容如下
  import java.sql.*;
  import javax.naming.*;
  import javax.ejb.*;
  import java.util.*;
  import java.rmi.RemoteException;
  /**
  容器治理持续性,一个产品的持续内容包括ID#,name,description和 baseprice
  */
  public class ProductBean implements EntityBean {
  
  protected EntityContext ctx;
  
  //容器治理的状态域必须定义成public类型
  public String productID; // 主键
  public String name;
  public String description;
  public double basePrice;
  
  public ProductBean() {
  System.out.println("New Product Entity Bean Java Object created by EJB Container.");
  }
  public String getName() throws RemoteException {
  System.out.println("getName() called.");
  return name;
  }
  
  public void setName(String name) throws RemoteException {
  System.out.println("getName() called.");
  this.name = name;
  }
  
  public String getDescription() throws RemoteException {
  System.out.println("getDescription() called.");
  return description;
  }
  
  public void setDescription(String description) throws RemoteException {
  System.out.println("setDescription() called.");
  this.description = description;
  }
  
  public double getBasePrice() throws RemoteException {
  System.out.println("getBasePrice() called.");
  return basePrice;
  }
  
  public void setBasePrice(double price) throws RemoteException {
  System.out.println("setBasePrice() called.");
  this.basePrice = price;
  }
  
  public String getProductID() {
  System.out.println("getProductID() called.");
  return productID;
  }
  
  // EJB必需的方法
  // 由容器调用,执行时可获得需要的资源
  public void ejbActivate() throws RemoteException {
  System.out.println("ejbActivate() called.");
  }
  
  /**
  * EJB 容器在它从数据库中移除实体bean前调用这个方法。它和客户端调用home.Remove()方法相对应。
  */
  public void ejbRemove() throws RemoteException {
  System.out.println("ejbRemove() called.");
  }
  
  public void ejbPassivate() throws RemoteException {
  System.out.println("ejbPassivate () called.");
  }
  
  public void ejbLoad() throws RemoteException {
  System.out.println("ejbLoad() called.");
  }
  
  public void ejbStore() throws RemoteException {
  System.out.println("ejbStore() called.");
  }
  
  public void setEntityContext(EntityContext ctx) throws RemoteException {
  System.out.println("setEntityContext called");
  this.ctx = ctx;
  }
  
  public void unsetEntityContext() throws RemoteException {
  System.out.println("unsetEntityContext called");
  this.ctx = null;
  }
  
  public String ejbCreate(String productID, String name, String description, double basePrice) throws CreateException, RemoteException {
  System.out.println("ejbCreate(" + productID + ", " + name + ", " + description + ", " + basePrice + ") called");
  
  this.productID = productID;
  this.name = name;
  this.description = description;
  this.basePrice = basePrice;
  return null;
  }
  
  public void ejbPostCreate(String productID, String name, String description, double basePrice) throws RemoteException {
  System.out.println("ejbPostCreate() called");
  }
  // 因为是容器治理的持续性,不需要定义finder方法。
  }
  将以上3个文件编译成class文件得到Product.class ProductBean.class ProductHome.class
  部署BEAN文件
  预备工作:启动j2ee服务器j2ee -verbose;启动cloudscape数据库服务器cloudscape -start。
  1. 打开部署工具deploytool,新建一个applicaton命名为Product,display name 也使用Product
  2. 给新建的application添加一个EnterPrise Bean,以下操作取默认值。
  
  
  
  
  
  
  
  3.添加建立的三个 class文件(Product.Class ProductHome.Class 和 ProductBean.Class)
  
  4.下一步选择 bean类型Entity bean 设置Enterprise bean Class
  ProductBean,Enterprise bean name 为ProductBean,display name 默认。
  5.设置remote home interface 为ProductHome,remote interface 为Product
  6.下一步操作如下,选取定义的状态域。将Primary key class 修改为java.lang.String。
  设置Primary key field name为productID。
  
  7.下面的按next跳过,最后单击finish按钮完成实体Bean 的创建过程.
  8.选中创建的ProductBean 在右侧的工作栏中点击Entity设置Deployment Setting.
  选中Database Setting ,Database jndi name ---jdbc/cloudscape
  user name :scott,passWord :tiger.
  9.生成默认得sql语句
  10.下面可以部署了点击deploy,设置jndi name。
  
  11.点击finish完成部署
  12.使用客户端测试刚部署的CMP实体bean。
  客户端Client.java
  import javax.ejb.*;
  import javax.naming.*;
  import java.rmi.*;
  import java.util.Enumeration;
  import java.util.Properties;
  public class Client {
  public static void main(String[] args) {
  ProductHome home = null;
  try { Properties props=System.getProperties();
  Context ctx=new InitialContext(props);
  home = (ProductHome) ctx.lookup("ProductHome");
  //创建几个产品EJB对象
  home.create("123-456

Tags:CMP 实体 bean

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接