精通ejb里bmp实体bean的基础问题

djsjeff 2003-10-22 09:14:43
在精通ejb第二版的bmp实体bean章节中的一个Account的例子的某些部分不太了解
实体bean中的代码
try {
ctx = new InitialContext(System.getProperties());
Object obj = ctx.lookup("AccountHome");
AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class);
System.err.println("Total of all accounts in bank initially = " + home.getTotalBankValue());

/*
* Use the factory to create the Account EJB Object
*/
home.create("123-456-7890", "John Smith");

/*
* Find an account
*/
Iterator i = home.findByOwnerName("John Smith").iterator();
if (i.hasNext()) {
account = (Account)
javax.rmi.PortableRemoteObject.narrow(
i.next(), Account.class);

}
else {
throw new Exception("Could not find account");
}

/*
* Call the balance() method, and print it
*/
System.out.println("Initial Balance = " + account.getBalance());
account.deposit(100);
100, account balance = " + account.getBalance());
System.out.println("Total of all accounts in bank now = " + home.getTotalBankValue());
AccountPK pk = (AccountPK) account.getPrimaryKey();
account = null;
account = home.findByPrimaryKey(pk);
System.out.println("Found account with ID " + pk + ". Balance = " + account.getBalance());
account.withdraw(150);
}
前面的部分有一句
Object obj = ctx.lookup("AccountHome");
AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class);
其中()里的obj和AccountHome.class(home接口)有什么关系阿
后面的一段更是琢磨不透
account = (Account) javax.rmi.PortableRemoteObject.narrow( i.next(),Account.class);
这里为什么用i.next;i.next得到的是不是一个AccountPK对象
照前面的用
Object obj = ctx.lookup("Account");
account = (Account) javax.rmi.PortableRemoteObject.narrow(obj, Account.class);不一样吗?
由于是自己初学者所以望大家耐心解答谢谢了


ejbFindByOwnerName大致代码如下:
public Collection ejbFindByOwnerName(String name) throws FinderException {
PreparedStatement pstmt = null;
Connection conn = null;
Vector v = new Vector();

try {
System.out.println("ejbFindByOwnerName(" + name + ") called");

/*
* Acquire DB connection
*/
conn = getConnection();

/*
* Find the primary keys in the DB
*/
pstmt = conn.prepareStatement("select id from accounts where ownerName = ?");
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();

/*
* Insert every primary key found into a vector
*/
while (rs.next()) {
String id = rs.getString("id");
v.addElement(new AccountPK(id));
}

/*
* Return the vector of primary keys
*/
return v;
}
...全文
36 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
djsjeff 2003-10-22
  • 打赏
  • 举报
回复
i.next()得到的是一个new AccountPK(id))吧,那他和Account.class有什么关系吗
就好像前面的AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class);中obj与AccountHome.classd的关系
naxin 2003-10-22
  • 打赏
  • 举报
回复
Iterator i = home.findByOwnerName("John Smith").iterator();
naxin 2003-10-22
  • 打赏
  • 举报
回复
"这个书里面也写了,我很琢磨不透的是account = (Account) javax.rmi.PortableRemoteObject.narrow( i.next(),Account.class);
中的i.next(),代表的是什么意思它在书上面没有讲清楚
"

这是RMI-IIOP的内容,请看该书的附录关于RMI-IIOP的知识.
djsjeff 2003-10-22
  • 打赏
  • 举报
回复
有谁知道吗
djsjeff 2003-10-22
  • 打赏
  • 举报
回复
这个书里面也写了,我很琢磨不透的是account = (Account) javax.rmi.PortableRemoteObject.narrow( i.next(),Account.class);
中的i.next(),代表的是什么意思它在书上面没有讲清楚
etre 2003-10-22
  • 打赏
  • 举报
回复
一个HOME接口,一个是远程 接口,你在看看吧
djsjeff 2003-10-22
  • 打赏
  • 举报
回复
还有就是AccountPK pk = (AccountPK) account.getPrimaryKey();这句也不太明白
getPrimaryKey()不是实体bean里的方法啊,要么就是和AccountPK pk = (AccountPK) ctx.getPrimaryKey();一样调用环境里的方法?那这里用AccountPK pk = (AccountPK) ctx.getPrimaryKey();是否可以

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧