今天主要是从写程序的角度来学习BMP的,主要学习如何对EntityBean的接口进行编程。目前对以下接口总算弄明白了些:
ejbCreate()
ejbPostCreate()
ejbRemove()
ejbFindByPrimaryKey()
ejbLoad()
ejbStore()
unsetEntityContext()
setEntityContext()
ejbHome()
一、理解接口
1. ejbCreate()、ejbPostCreate()
首先,ejbCreate()和ejbPostCreate()必须成对出现。ejbCreate()的参数列表是什么,ejbPostCreate()的参数列表也必须是什么,也即完全一致。
其次,从调用顺序上来看,容器先调用ejbCreate(),然后调用ejbPostCreate();
最后看看他们的功能。ejbCreate()中的代码是向数据库中插入一行数据,即使用Insert语句。在Home接口中有一个和ejbCreate()相对应的create()方法,对应法则是参数列表相同。客户程序一般通过Home接口的create()方法来插入一行数据,create()方法将这个请求代理给ejbCreate()来完成。
2. ejbRemove()
这个的功能和ejbCreate()恰恰相反,ejbRemove()是从数据库中删除一行数据,使用Delete语句。没太多可说的,注意删除的时候应从EntityContext获得要删除的行的主键。
3. ejbFindByPrimaryKey()
这个比较有意思。方法签名为:
[PrimaryKey] ejbFindByPrimaryKey( [PrimaryKey] pk )
[PrimaryKey]是EntityBean的主键类。按理说应是由主键得到代表该行数据的实体Bean,但这里却返回的依然是主键(容器将使用这个主键,然后在Home接口的相应方法中返回正确的实体Bean,Home接口的实现代码都是由容器自动生成的)。其实你可以什么也不做,直接把pk参数当作返回值返回也是可以的,当然,前提是这个主键所代表的行必须存在。
所以,这个方法的正确实现方式是:用这个主键到数据库中去查找数据,如果有这行数据就把这个主键直接返回,由容器来自动生成相应的实体Bean返回给客户(实际的处理过程可能是:容器实例化一个EntityBean,然后调用setEntityContext()方法,这个方法的参数EntityContext将包含你返回的主键,然后再调用ejbLoad(),在ejbLoad()中获取所有的数据,这样一个正确的实体Bean就生成好,可以给客户使用了);如果没有这样一行数据,你就直接返回null,容器就知道查找失败了。
其他的Finder方法也一样,只是ejbFindByPrimaryKey()是必须要的。
4. ejbLoad()、ejbStore()
ejbLoad()就是从数据库中读取数据,然后更新实体Bean的各个数据域;
ejbStore()正好相反,是将自己的各个数据域写入数据库中。
唯一需要区别的是,ejbLoad()发生的时候,实体Bean还未和具体的一行数据关联,因此,主键应从EntityContext对象中获得;而ejbStore()发生的时候,实体Bean和数据库中的某一行数据肯定已经关联,你可以使用实体Bean本身的数据域来获取主键。
5. unsetEntityContext()、setEntityContext()
这个也很简单,就是设置实体Bean的EntityContext对象,使得实体Bean可以获得环境信息。最常用的方法就是EntityContext.getPrimaryKey()了,可以通过这个方法来获取实体Bean对应的主键。
6. ejbHome()
这是一个特殊的接口方法,它不与任何给定的数据实例(或行)相关,比如要想知道现在有多少用户,我们可以通过返回用户表的行数来获得,这个就与具体的数据实例无关。
客户端一般通过Home接口来调用它。与其他方法不同的是:调用它时,容器是从缓存池中找一个Bean来完成这个方法的,更具体的说,容器并未调用这个Bean的setEntityContext()方法,也即该Bean未和任何具体的数据行相关联,这是与其他方法最大的不同。
这个方法是在Home接口中定义的,实现是在Bean类完成的,关联的方式是:返回值和参数列表相一致,在Home接口中的方法名为xxxx(),则在Bean类中方法名变为ejbHomeXxxx(),即加上ejbHome前缀,并把第一个字母大写。
【示例】在Home接口中定义方法为:
public [return value] xxxx( [parameters list] );
在Bean类中定义为:
public [return value] ejbHomeXxxx( [parameters list] ) { //具体实现… }
7. 其他的访问函数:setXXX()、getXXX()
这些函数很简单。主要是给客户端使用的。在调用getXXX()方法前,ejbLoad()已经被容器调用过了,所以你只需很简单的把相应的数据域返回给客户就可以了。调用setXXX()也一样,只需简单把相应数据域设置成参数所传递的值即可,真正写入数据库的操作是在ejbStore()中完成的。
二、总结
需要编写SQL语句的方法,即需要访问数据库的方法:
ejbCreate()
ejbPostCreate()
ejbRemove()
ejbFindByPrimaryKey()
ejbLoad()
ejbStore()
需要从EntityContext中获取主键的函数:
ejbLoad()
setXXX()
getXXX()
代码示例package iscas.bookstore.DALayer;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.ejb.FinderException;
import java.sql.*;
import javax.naming.*;
public class UsersBean implements EntityBean {
EntityContext entityContext;
String userID;
String password;
public String ejbCreate(String userID, String pwd) throws CreateException {
setUserID(userID);
setPassword(pwd);
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"insert into Users(UserID,password) values(?,?)");
pstmt.setString(1, this.userID);
pstmt.setString(2, this.password);
pstmt.executeUpdate();
return this.userID;
} catch (Exception e) {
//e.printStackTrace();
throw new CreateException(e.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbPostCreate(String userID, String pwd) throws CreateException {
}
public void ejbRemove() throws RemoveException {
String uid = (String) entityContext.getPrimaryKey();
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"delete from Users where UserID=?");
pstmt.setString(1, uid);
pstmt.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserID() {
return userID;
}
public String ejbFindByPrimaryKey(String userID) throws
FinderException {
boolean succ = false;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"select * from Users where UserID=?");
pstmt.setString(1, userID);
ResultSet rs = pstmt.executeQuery();
rs.next();
succ = true;
} catch (Exception e) {
throw new FinderException(e.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
if (succ) {
return userID;
} else {
return null;
}
}
public void ejbLoad() {
String uid = (String)this.entityContext.getPrimaryKey();
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"select * from Users where UserID=?");
pstmt.setString(1, uid);
ResultSet rs = pstmt.executeQuery();
rs.next();
this.userID = rs.getString("UserID");
this.password = rs.getString("password");
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbStore() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"update Users set UserID=?, password=? where UserID=?");
pstmt.setString(1, this.userID);
pstmt.setString(2, this.password);
pstmt.setString(3, this.userID);
pstmt.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
private Connection getConnection() throws Exception {
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(
"java:MSSQLDS");
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.ejb.FinderException;
import java.sql.*;
import javax.naming.*;
public class UsersBean implements EntityBean {
EntityContext entityContext;
String userID;
String password;
public String ejbCreate(String userID, String pwd) throws CreateException {
setUserID(userID);
setPassword(pwd);
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"insert into Users(UserID,password) values(?,?)");
pstmt.setString(1, this.userID);
pstmt.setString(2, this.password);
pstmt.executeUpdate();
return this.userID;
} catch (Exception e) {
//e.printStackTrace();
throw new CreateException(e.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbPostCreate(String userID, String pwd) throws CreateException {
}
public void ejbRemove() throws RemoveException {
String uid = (String) entityContext.getPrimaryKey();
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"delete from Users where UserID=?");
pstmt.setString(1, uid);
pstmt.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserID() {
return userID;
}
public String ejbFindByPrimaryKey(String userID) throws
FinderException {
boolean succ = false;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"select * from Users where UserID=?");
pstmt.setString(1, userID);
ResultSet rs = pstmt.executeQuery();
rs.next();
succ = true;
} catch (Exception e) {
throw new FinderException(e.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
if (succ) {
return userID;
} else {
return null;
}
}
public void ejbLoad() {
String uid = (String)this.entityContext.getPrimaryKey();
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"select * from Users where UserID=?");
pstmt.setString(1, uid);
ResultSet rs = pstmt.executeQuery();
rs.next();
this.userID = rs.getString("UserID");
this.password = rs.getString("password");
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbStore() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(
"update Users set UserID=?, password=? where UserID=?");
pstmt.setString(1, this.userID);
pstmt.setString(2, this.password);
pstmt.setString(3, this.userID);
pstmt.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
private Connection getConnection() throws Exception {
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(
"java:MSSQLDS");
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}