3 Replies Latest reply on Jul 26, 2007 1:25 PM by wayne baylor

    Fail to lookup entity bean inside a stateless session bean b

    Calson LI Newbie

      Hi all,

      I am using JBoss 4.0.5 GA and myEclipse 5.1.1 GA to do a web project with EJB for database manipulation.

      I created a stateless session bean which is invoked in doGet method of a servlet. I tried to add CMP entity bean lookup method in a business method of this session bean. It ends up with a "Naming Exception". The code snippet inside the business method of stateless session bean is as follows.

      try {
      // StaffLocalHome is the home interface of CMP entity bean Staff
      StaffLocalHome staffHome = StaffUtil.getLocalHome();
      StaffLocal staff = staffHome.findByPrimaryKey(new StaffPK(loginID));
      StaffData staffData = staff.getValueObject();
      catch (NamingException ne) {
      returnCode = 1;
      catch (FinderException fe) {
      returnCode = -2;
      catch (Exception e) {
      returnCode = 2;

      If I moved the above codes inside the servlet where stateless session bean is invoked, no problem of naming exception when looking up CMP entity bean. So, it seems that ejb-jar.xml, jboss.xml, web.xml and jboss-web.xml are correctly configured.

      Could you advise where the problem is?

      Thank you,

        • 1. Re: Fail to lookup entity bean inside a stateless session be
          wayne baylor Apprentice

          what's the lookup code look like?

          you should be able to use:

          MyBeanLocal bean;

          to inject the slsb.

          • 2. Re: Fail to lookup entity bean inside a stateless session be
            Calson LI Newbie

            Thanks a lot for your reply, Waynebaylor.

            My lookup code is as follows, which is generated from XDoclet.

             * Utility class for Staff.
             * @xdoclet-generated at ${TODAY}
             * @copyright The XDoclet Team
             * @author XDoclet
             * @version ${version}
            public class StaffUtil
             /** Cached local home (EJBLocalHome). Uses lazy loading to obtain its value (loaded by getLocalHome() methods). */
             private static ts.interfaces.StaffLocalHome cachedLocalHome = null;
             private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException {
             // Obtain initial context
             javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
             try {
             Object objRef = initialContext.lookup(jndiName);
             // only narrow if necessary
             if (java.rmi.Remote.class.isAssignableFrom(narrowTo))
             return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
             return objRef;
             } finally {
             // Home interface lookup methods
             * Obtain local home interface from default initial context
             * @return Local home interface for Staff. Lookup using COMP_NAME
             public static ts.interfaces.StaffLocalHome getLocalHome() throws javax.naming.NamingException
             if (cachedLocalHome == null) {
             cachedLocalHome = (ts.interfaces.StaffLocalHome) lookupHome(null, ts.interfaces.StaffLocalHome.COMP_NAME, ts.interfaces.StaffLocalHome.class);
             return cachedLocalHome;

            Thank you,

            • 3. Re: Fail to lookup entity bean inside a stateless session be
              wayne baylor Apprentice

              what's the code in the servlet that does the lookup for the first session bean?

              and is the NamingException from StaffUtil or from the servlet?