JAVA技术CMP实体Bean实战开发
【IT168 技术文档】
一个容器管理持续的实体bean允许容器处理一些或者它的全部数据访问逻辑。用容器管理的持续,你需要把实体bean类的一些域公开出来,好让容器在代表bean执行数据库操作时可以设置这些域。
在容器管理持续化的情况下,entity bean类的代码必须满足以下条件。首先,类必须定义为public。此外,这个类还必须实现以下内容:
·EntityBean接口
·零个或多个ejbCreate方法及ejbPostCreate方法
·对应于持续化及关联字段的定义它的get方法及set方法
·home方法
·business方法
entity bean类不能实现以下方法:
·finder方法
·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
0
相关文章