国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CMP實體bean實戰開發

2019-11-18 12:31:05
字體:
來源:轉載
供稿:網友

  一個容器治理持續的實體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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红安县| 上蔡县| 合作市| 色达县| 开封县| 石屏县| 揭东县| 台北市| 武冈市| 寿阳县| 汕尾市| 苏尼特左旗| 原阳县| 南投县| 长汀县| 芜湖县| 南城县| 天气| 和林格尔县| 开封县| 谢通门县| 台南市| 盖州市| 民丰县| 浠水县| 那坡县| 三明市| 都匀市| 洪洞县| 榆林市| 乌兰察布市| 石家庄市| 南召县| 泰宁县| 洛川县| 广宁县| 苍山县| 措勤县| 侯马市| 潜山县| 伊春市|