精通ejb里bmp实体bean的基础问题
在精通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;
}