facebook

Getting EJB’s to work!!

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #253515 Reply

    Glenn Puckett
    Participant

    I am in a tight spot with my current project. I had the flexability to make the decision to go with J2EE and utilize myEclipse as my development platform but it has put me out on a limb if I can’t get this figured out.

    I’ll admit I am very new to EJBs. I have been developing in the web container for years and did not have the opportunity to learn about EJBs. Now that I am in a new environment I have the flexability to start building more legitimate J2EE applications but I don’t have an enormous amount of time to beat my head against the wall trying to figure out how to make it work. I would greatly appreciate it if some kindly soul would take a few minutes over the next few days and help get me past this hurdle.

    I started with the EJB quickstart tutorial and then the “Web Development with MyEclipse and JBoss” tutorial. At first I was amazed at how simple to understand the tutorials were and how completely they covered the topics. But unfortunately I was only able to get so far, close to the end, but stonewalled non the less. I have a post regarding my CMP issues and am waiting for some helpful answers.

    Since I am having problems with CMP I decided to try the apparently more simple approach of using BMP. Unfortunately I can’t find any tutorials or guides that address BMP EJBs. I have looked everywhere including Laliluna. So I have tried to build one without the aid of a guide and have run into another wall.

    I generated a BMP Entity EJB using the EJB wizzard. Here is the code:

    
    /**
     * XDoclet-based BMP entity bean.
     * 
     * To generate EJB related classes using XDoclet:
     *
     *        - Add Standard EJB module to XDoclet project properties
     *        - Customize XDoclet configuration
     *        - Run XDoclet
     * 
     * Below are the xdoclet-related tags needed for this EJB.
     *
     * @ejb.bean name="TaxMaster"
     *           display-name="TaxMaster Entity Bean"
     *           description="Provides access to the Tax MasterTable"
     *           jndi-name="TaxMaster"
     *           type="BMP"
     *           view-type="local"
     */
    public class TaxMasterBean 
                        implements EntityBean, AppConstants 
    {
    
        /** The entity context */
        private EntityContext context;
        private Logger logger = null;
        private TaxMasterTblDAO tDAO = null;
    
        public TaxMasterBean() {
            super();
            
        }
    
    
        /**
         *
         * @throws CreateException Thrown if method fails due to system-level error.
         * 
         * @throws CreateException
         *
         * @ejb.create-method
         */
        public boolean ejbCreate(String taxkey) 
                            throws CreateException 
        {
            tDAO = new TaxMasterTblDAO();
            return tDAO==null?false:true;
        }
    
        /**
         * 
         * @throws CreateException Thrown if method fails due to system-level error.
         */
        public void ejbPostCreate(String taxkey) throws CreateException {
        }
    
        public void ejbActivate() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbLoad() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbPassivate() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbRemove() throws RemoveException, EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbStore() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        /**
         * 
         * This method is called with no transaction context. 
         * 
         * @throws EJBException Thrown if method fails due to system-level error.
         */
        public void setEntityContext(EntityContext newContext) throws EJBException {
            context = newContext;
        }
    
        /**
         * Unset the associated entity context. A container invokes this method 
         * before terminating the life of the instance. The entity bean must not 
         * attempt to access its persistent state and relationships using the 
         * accessor methods during this method. <br>
         * 
         * This method is called with no transaction context. 
         * 
         * @throws EJBException Thrown if method fails due to system-level error.
         */
        public void unsetEntityContext() throws EJBException {
            context = null;
        }
        
        /**
         * Get an entry from the Tax Master Table using the primary key.
         * 
         * @ejb.interface-method view-type="both"
         * @param ID
         * @return
         * @throws ErrorException
         */
        public int ejbFindByPrimaryKey(int ID)
                            throws FinderException
        {
            int rtn = 0;
            ResultSet rs = null;
            try {
                rs = tDAO.getBySQL("select "+FLDID+" from "+TaxMasterTBL+" where "+FLDID+" = "+ID);
                if(rs.next()) { 
                    rtn = ID;
                }
            } catch (SQLException e) {
                throw new FinderException("Unable to access the Table");
            } finally {
                if(rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {}
                }
            }
            return rtn;
        }
        
        /**
         * Get an entry from the Tax Master Table for a specific tax key.
         * 
         * If more than one entry is found for the key, return the first one encountered.
         *
         * @ejb.interface-method view-type="both"
         * @param taxkey
         * @return
         * @throws ErrorException
         */
        public TaxMasterTbl ejbFindByTaxKey(String taxkey)
                            throws ErrorException
        {
            TaxMasterTbl rtn = null;
            ArrayList<TaxMasterTbl> tList = null;
            tList = tDAO.getByTaxKey(taxkey);
            if(tList != null && tList.size() > 0)
                rtn = tList.get(0);
            return rtn;
        }
    
    }
    

    Then I created a session EJB to control access to the entity bean. That module is:

    
    /**
     * XDoclet-based session bean.  The class must be declared
     * public according to the EJB specification.
     *
     * To generate the EJB related files to this EJB:
     *        - Add Standard EJB module to XDoclet project properties
     *        - Customize XDoclet configuration for your appserver
     *        - Run XDoclet
     *
     * Below are the xdoclet-related tags needed for this EJB.
     * 
     * @ejb.bean name="TaxMasterAccess"
     *           display-name="TaxMasterAccess Session Bean" 
     *           description="Provides access to the TaxMaster Entity Bean"
     *           jndi-name="TaxMasterAccess"
     *           type="Stateless"
     *           view-type="both"
     */
    public class TaxMasterAccessBean implements SessionBean {
    
        /** The session context */
        private SessionContext context;
        
        private TaxMasterLocalHome taxHome;
        private Logger logger = null;
    
        public TaxMasterAccessBean() {
            super();
            
        }
    
        public void ejbActivate() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbPassivate() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        public void ejbRemove() throws EJBException, RemoteException {
            // TODO Auto-generated method stub
    
        }
    
        /**
         * Set the associated session context. The container calls this method 
         * after the instance creation.
         * 
         * The enterprise bean instance should store the reference to the context 
         * object in an instance variable.
         * 
         * This method is called with no transaction context. 
         * 
         * @throws EJBException Thrown if method fails due to system-level error.
         */
        public void setSessionContext(SessionContext newContext) throws EJBException {
            context = newContext;
        }
    
        /**
         * An ejbCreate method as required by the EJB specification.
         * 
         * The container calls the instance?s <code>ejbCreate</code> method whose
         * signature matches the signature of the <code>create</code> method invoked
         * by the client. The input parameters sent from the client are passed to
         * the <code>ejbCreate</code> method. Each session bean class must have at
         * least one <code>ejbCreate</code> method. The number and signatures
         * of a session bean?s <code>create</code> methods are specific to each 
         * session bean class.
         * 
         * @throws CreateException Thrown if method fails due to system-level error.
         * 
         * @ejb.create-method
         * 
         */
        public void ejbCreate() throws CreateException {
            
            try {
                InitialContext ic = new InitialContext();
                taxHome = (TaxMasterLocalHome)ic.lookup(TaxMasterLocalHome.JNDI_NAME);
            } catch (NamingException e) {
                throw new CreateException("Received NamingException");
            }
        }
    
        /**
         * Create a new Tax Master Table entry
         *
         * @ejb.interface-method view-type = "both"
         * 
         * @throws EJBException Thrown if method fails due to system-level error.
         */
        public void addEntry(TaxMasterTbl pData) throws EJBException {
            // rename and start putting your business logic here
        }
        
        /**
         * Get a Tax Master Table entry by TaxKey
         *
         * @ejb.interface-method view-type = "both"
         * 
         * @throws EJBException Thrown if method fails due to system-level error.
         */
        public TaxMasterTbl getByTaxKey(String taxkey) throws EJBException {
            TaxMasterTbl rtn = null;
            try {
                rtn=taxHome.findByTaxKey(taxkey);
            } catch (ErrorException e) {
                
            }
            return rtn;
        }
    
    }
    

    When I executed XDoclet it generated the following modules:

    in the ejb package:

      TaxMasterAccessSession.java
      TaxMasterBMP.java

    in the interfaces package:

      TaxMasterAccessHome.java
      TaxMasterAccessLocal.java
      TaxMasterAccessLocalHome.java
      TaxMasterLocal.java
      TaxMasterLocalHome.java
      TaxMasterPK.java

    The problem I have run into at the moment is with the getByTaxKey method in the TaxMasterAccessBean module. I have an instance of TaxMasterLocalHome in taxHome. So my anticipation was that I would execute:

    
    return taxHome.ejbFindByTaxKey(taxkey);
    

    However that generates a code error. TaxMasterLocal contains:

    
    /**
     * Local home interface for TaxMaster.
     * @xdoclet-generated at ${TODAY}
     * @copyright The XDoclet Team
     * @author XDoclet
     * @version ${version}
     */
    public interface TaxMasterLocalHome
       extends javax.ejb.EJBLocalHome
    {
       public static final String COMP_NAME="java:comp/env/ejb/TaxMasterLocal";
       public static final String JNDI_NAME="TaxMasteLocal";
    
       public com.metatax.interfaces.TaxMasterLocal create(java.lang.String taxkey)
          throws javax.ejb.CreateException;
    
       public com.metatax.interfaces.TaxMasterLocal findByPrimaryKey(int ID)
          throws javax.ejb.FinderException;
    
       public com.metatax.interfaces.TaxMasterLocal findByTaxKey(java.lang.String taxkey)
          throws com.metamap.exception.ErrorException,javax.ejb.FinderException;
    
    }
    

    According to this interface the return type of findByTaxKey is TaxMasterLocal. I can’t figure out how to get from that to the actual method that returns the data.

    So my question is, am I linking properly from the session bean to the entity bean? If so, how do I properly execute a method in the entity bean from the session bean? Is there XDoclet information that needst to be added to make this work properly?

    Thanks,

    Glenn Puckett
    MetaMAP, Inc.
    Lexington, Kentucky, USA

    #253597 Reply

    Riyad Kalla
    Member

    Glenn,
    I’ve asked our EJB guy to have a look at your post.

    #253616 Reply

    Glenn Puckett
    Participant

    @support-rkalla wrote:

    Glenn,
    I’ve asked our EJB guy to have a look at your post.

    Thanks.. I finally figured out how to get CMP EJBs working. Just one of those things that the experienced users take for granted, I’m sure.

    It would still be great if I could get BMP EJBs working. There may verywell come a time where that would be needed.

    Thanks for your assistance..

    Glenn Puckett

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Getting EJB’s to work!!

You must be logged in to post in the forum log in