Local Home 的Entity Bean本身是紧耦合的,所以部署时应该将Local Home 的Entity Bean与调用Bean放在一个.jar文件里面。具体应用很简单(下面的例子已经通过调试): 假设有一个SwitchSQL实体Bean: //SwitchSQLBean import javax.ejb.*; import javax.ejb.*; public class SwitchSQLBean implements SessionBean { SessionContext sessionContext; java.lang.String originalSQL; java.lang.String whereSQL; java.lang.String[][] where = new String[9][9]; //java.lang.String switchedSQL; public void ejbCreate() throws CreateException { /**@todo Complete this method*/ } public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } /**get the value of the array where[i][j]*/ public java.lang.String getWhere(int i, int j) { return null; } /**get the sql statement which has been modified by whereSQLS*/ /*public java.lang.String getSwitchedSQL(String originalSQL,String flagString) { return originalSQL; }*/ /**initaialize the array where[][]*/ public void initWhere() { } //get the position of the flagString such as '&1' in the originalString public java.lang.Integer getIndex(String flagString) { Integer index; return null; } public java.lang.String getSwitchedSQL(String originalSQL,String flagString) { return originalSQL; } public java.lang.String[][] getWhere() { return where; } }//SwitchSQLLocal import javax.ejb.*; import java.util.*;public interface SwitchSQLLocal extends javax.ejb.EJBLocalObject { public java.lang.String getSwitchedSQL(String originalSQL,String flagString) ;}//SwitchSQLLocalHome import javax.ejb.*; import java.util.*; public interface SwitchSQLLocalHome extends javax.ejb.EJBLocalHome { public SwitchSQLLocal create() throws CreateException; }部署SwitchSQL的JNDI名字为:SwitchSQLLocal 则在调用它的Bean里面引用方式为:SwitchSQLLocalHome sqlHomeObject SwitchSQLLocal sqlLocalObject; sqlHomeObject = (SwitchSQLLocalHome)ctx.lookup("SwitchSQLLocal"); sqlLocalObject = sqlHomeObject.create();如果还有问题,可以发到我邮件:[email protected]
BookHome bookHome =(BookHome)ctx.lookup("BookLocal");
javax.naming.LinkException: . Root exception is javax.naming.NameNotFoundException: remaining name: /app/ejb/BookCMP.jar#Book/local-home <<no stack trace available>>
到底是哪里的问题,真是郁闷!
假设有一个SwitchSQL实体Bean:
//SwitchSQLBean
import javax.ejb.*;
import javax.ejb.*;
public class SwitchSQLBean implements SessionBean {
SessionContext sessionContext;
java.lang.String originalSQL;
java.lang.String whereSQL;
java.lang.String[][] where = new String[9][9];
//java.lang.String switchedSQL;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} /**get the value of the array where[i][j]*/
public java.lang.String getWhere(int i, int j) {
return null;
}
/**get the sql statement which has been modified by whereSQLS*/
/*public java.lang.String getSwitchedSQL(String originalSQL,String flagString) {
return originalSQL;
}*/ /**initaialize the array where[][]*/
public void initWhere() { } //get the position of the flagString such as '&1' in the originalString
public java.lang.Integer getIndex(String flagString) {
Integer index;
return null;
}
public java.lang.String getSwitchedSQL(String originalSQL,String flagString) {
return originalSQL;
}
public java.lang.String[][] getWhere() {
return where;
}
}//SwitchSQLLocal
import javax.ejb.*;
import java.util.*;public interface SwitchSQLLocal extends javax.ejb.EJBLocalObject {
public java.lang.String getSwitchedSQL(String originalSQL,String flagString) ;}//SwitchSQLLocalHome
import javax.ejb.*;
import java.util.*;
public interface SwitchSQLLocalHome extends javax.ejb.EJBLocalHome {
public SwitchSQLLocal create() throws CreateException;
}部署SwitchSQL的JNDI名字为:SwitchSQLLocal 则在调用它的Bean里面引用方式为:SwitchSQLLocalHome sqlHomeObject
SwitchSQLLocal sqlLocalObject;
sqlHomeObject = (SwitchSQLLocalHome)ctx.lookup("SwitchSQLLocal");
sqlLocalObject = sqlHomeObject.create();如果还有问题,可以发到我邮件:[email protected]
你还没有搞清楚概念,TestClient与EntityBean没有耦合关系,我说的是Session Bean与EntityBean是紧耦合关系,Session Bean和EntityBean放在一个.jar文件里面发布;也就是说用到CMP时,常常用一个Session Bean来调用EntityBean,这就是经常所说的facade模式。然后客户端(比如你所说的TestClient或者jsp等)通过Session Bean的远程接口访问Session Bean。也就是说,客户端常常不是直接访问EntityBean,而是通过Session Bean间接访问Entity Bean。
客户端要访问组件必须通过Remote接口,这是肯定的。我们把Entity Bean定义成Local 接口,是因为我们不直接访问EntityBean。
客户端访问Session Bean,使用的是Session Bean的Remote接口;Session Bean访问Entity Bean使用的是EntityBean的Local接口。 你给我的程序客户端直接通过Local接口调用Entity Bean是不可能调试成功的;解决办法:
方法一:创建一个Session Bean,Session Bean访问Entity Bean的Local接口,TestClient访问Session Bean的Remote接口
方法二:将EntityBean设置成Remote接口,TestClient直接访问Entity Bean的Remote接口。这种方式估计用的人不多不知道我是否将问题说明白,不明白的话再回信,OK?