首先,检查你的CMP设置的是Local 接口还是Remote接口;
其次,如果是Local接口,Session Bean与CMP是否在一个Module里面
如果CMP是Local接口,Session Bean 与 CMP也在一个Module里,则上面的调用方法是不对的,下面给你一个示例(已经贴过一次):
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]
其次,如果是Local接口,Session Bean与CMP是否在一个Module里面
如果CMP是Local接口,Session Bean 与 CMP也在一个Module里,则上面的调用方法是不对的,下面给你一个示例(已经贴过一次):
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]
这里永远都是 true !
对象比较要用 equals()
if(pw.equals(tSysUser.getPw())){ return true;}
另外你的编码习惯很不好..
比如说参数userID的值是"11",假如你用的数据库是Oracle的,userID对应的字段定义长度为3,那么TSysUser tSysUser=tSysUserHome.findByPrimaryKey("11");返回的对象是空的。因为Oracle存储如果不满定义长度时,在右边填充空格。CMP认为"11"和"11 "不是一个值。
我认为你的Entity Bean定义的不清楚,因为如果要使用TSysUser tSysUser=tSysUserHome.findByPrimaryKey(userID);这种形式,那么EntityBean应该是始终包含主键的,因为你返回的是一个TSysUser对象,所以参数应该是TSysUserPK对象;如果你想用tSysUserHome.findByPrimaryKey(userID)这种形式,那么返回的应该是一个Collection对象,而不是一个TSysUser对象。不管从哪个角度讲,TSysUser tSysUser=tSysUserHome.findByPrimaryKey(userID);肯定是不对的。