sessionBean 如何调用 entityBean 麻烦仁兄给个例子

解决方案 »

  1.   

    这个问题问的太大了点:)
    建议你到IBM网站去看看
      

  2.   

    你看看《J2EETutorial》中的DUKE银行的例子代码就好了,3个会话BEAN调用3个实体BEAN
      

  3.   

    --------------------------------这是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;
      }
    }
      

  4.   

    附:以上例子代码是jbuilder+weblogic中调试通过