sessionBean 调用 entityBean sessionBean 如何调用 entityBean 麻烦仁兄给个例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个问题问的太大了点:)建议你到IBM网站去看看 你看看《J2EETutorial》中的DUKE银行的例子代码就好了,3个会话BEAN调用3个实体BEAN --------------------------------这是entityBean:StudentBean,jndi名为Student--------------package cmpsample;import javax.ejb.*;import java.util.Collection;abstract public class StudentBean implements EntityBean { EntityContext entityContext; public java.lang.Integer ejbCreate(java.lang.Integer stuid) throws CreateException { setStuid(stuid); return null; } public void ejbPostCreate(java.lang.Integer stuid) throws CreateException { /**@todo Complete this method*/ } public void ejbRemove() throws RemoveException { /**@todo Complete this method*/ } public abstract void setStuid(java.lang.Integer stuid); public abstract void setStuname(java.lang.String stuname); public abstract void setLockerid(java.lang.Integer lockerid); public abstract java.lang.Integer getStuid(); public abstract java.lang.String getStuname(); public abstract java.lang.Integer getLockerid(); public void ejbLoad() { /**@todo Complete this method*/ } public void ejbStore() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void unsetEntityContext() { this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; }}--------------------------------这是entityBean的HOME接口-------------------------------package cmpsample;import javax.ejb.*;import java.util.*;import java.rmi.*;public interface StudentRemoteHome extends javax.ejb.EJBHome { public StudentRemote create(Integer stuid) throws CreateException, RemoteException; public StudentRemote findByName(String name) throws FinderException, RemoteException; public StudentRemote findByPrimaryKey(Integer stuid) throws FinderException, RemoteException;}--------------------------------这是entityBean的Remote接口-------------------------------package cmpsample;import javax.ejb.*;import java.util.*;import java.rmi.*;public interface StudentRemote extends javax.ejb.EJBObject { public Integer getStuid() throws RemoteException; public void setStuname(String stuname) throws RemoteException; public String getStuname() throws RemoteException; public void setLockerid(Integer lockerid) throws RemoteException; public Integer getLockerid() throws RemoteException;}--------------------------------这是调用entityBean的sessionBean:facadeBean-------------------------------package cmpsample;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;import javax.ejb.*;public class facadeBean implements SessionBean { SessionContext sessionContext; public Student student=null; public StudentHome studentHome=null; public void ejbCreate() throws CreateException { /**@todo Complete this method*/ try{ Context context = new InitialContext(); //look up jndi name Object ref = context.lookup("java:/comp/env/student"); //look up jndi name and cast to Home interface studentHome= (StudentHome) ref; }catch(Exception e){e.printStackTrace();} } public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { try{ Context context = new InitialContext(); //look up jndi name Object ref = context.lookup("java:/comp/env/student"); //look up jndi name and cast to Home interface studentHome= (StudentHome) ref; }catch(Exception e){e.printStackTrace();} } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public String getStuInfo(String stuid){ String strStu=null; try{ student = studentHome.findByPrimaryKey(new Integer(stuid)); strStu ="the studentID is "+ student.getStuid() + " Name is "+student.getStuname(); }catch(Exception e){} return strStu; }} 附:以上例子代码是jbuilder+weblogic中调试通过 关于SPRING注入问题 hibernate配置的一个小问题 两个应用程序的内存共享? J2EE网站经验共享! JSP自动刷新,急。。。 请教函数Runtime.getRuntime().exec()的问题 wicket页面link跳转至jsp页面 关于正则表达式的提取数据法,高手进来救命 window2000下,关于JAVA打印获得输入流无效的问题。(高手请进)(150分) 已知和 求三位数 jbuilder里面建CMP的怪问题? 谁有hibernate2.0.3???
建议你到IBM网站去看看
package cmpsample;import javax.ejb.*;
import java.util.Collection;abstract public class StudentBean implements EntityBean {
EntityContext entityContext;
public java.lang.Integer ejbCreate(java.lang.Integer stuid) throws CreateException {
setStuid(stuid);
return null;
}
public void ejbPostCreate(java.lang.Integer stuid) throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() throws RemoveException {
/**@todo Complete this method*/
}
public abstract void setStuid(java.lang.Integer stuid);
public abstract void setStuname(java.lang.String stuname);
public abstract void setLockerid(java.lang.Integer lockerid);
public abstract java.lang.Integer getStuid();
public abstract java.lang.String getStuname();
public abstract java.lang.Integer getLockerid();
public void ejbLoad() {
/**@todo Complete this method*/
}
public void ejbStore() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
}
--------------------------------这是entityBean的HOME接口-------------------------------
package cmpsample;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface StudentRemoteHome extends javax.ejb.EJBHome {
public StudentRemote create(Integer stuid) throws CreateException, RemoteException;
public StudentRemote findByName(String name) throws FinderException, RemoteException;
public StudentRemote findByPrimaryKey(Integer stuid) throws FinderException, RemoteException;
}--------------------------------这是entityBean的Remote接口-------------------------------
package cmpsample;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface StudentRemote extends javax.ejb.EJBObject {
public Integer getStuid() throws RemoteException;
public void setStuname(String stuname) throws RemoteException;
public String getStuname() throws RemoteException;
public void setLockerid(Integer lockerid) throws RemoteException;
public Integer getLockerid() throws RemoteException;
}--------------------------------这是调用entityBean的sessionBean:facadeBean-------------------------------
package cmpsample;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;import javax.ejb.*;public class facadeBean implements SessionBean {
SessionContext sessionContext;
public Student student=null;
public StudentHome studentHome=null;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
try{
Context context = new InitialContext(); //look up jndi name
Object ref = context.lookup("java:/comp/env/student");
//look up jndi name and cast to Home interface
studentHome= (StudentHome) ref;
}catch(Exception e){e.printStackTrace();}
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
try{
Context context = new InitialContext(); //look up jndi name
Object ref = context.lookup("java:/comp/env/student");
//look up jndi name and cast to Home interface
studentHome= (StudentHome) ref;
}catch(Exception e){e.printStackTrace();} }
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public String getStuInfo(String stuid){
String strStu=null;
try{
student = studentHome.findByPrimaryKey(new Integer(stuid));
strStu ="the studentID is "+ student.getStuid() + " Name is "+student.getStuname();
}catch(Exception e){}
return strStu;
}
}