3 Replies Latest reply on Mar 14, 2006 5:39 PM by Bill Burke

    ClassCastException looking up EJB3.0 SLSB

    Kevin Hooke Newbie

      I'm getting this exception when looking up an EJB3.0 Stateless Session Bean:

      java.lang.ClassCastException: org.jnp.interfaces.NamingContext

      I'm using JBoss 4.0.4RC1.

      My SLSB is as simple as this:

      public class AddressBookBean implements AddressBook {
       private EntityManager manager;
       public void addAdress(Address newAddress)
       manager.persist( newAddress );
       public List retrieveAddresses()
       return null;

      The Spring MVC Controller that is trying to look up the SLSB (I've tried using the JndiObjectFactoryBean to wire up the bean, but thats giving the same exception) is using the following code:

      InitialContext ctx = new InitialContext();
       AddressBook addressBook = (AddressBook) ctx.lookup("test1/AddressBookBean");

      The ejb is deployed in test1.ear, so I'm assuming the default jndi name is therefore test1/AddressBookBean

      I obviously get something back from JNDI, just not what I expected to get back.

      I also noticed that the EJB 3.0 trailblazer refers to /remote and /local JNDI names, but neither of these (eg test1/AddressBookBean/local, or test1/AddressBookBean/remote) are bound in JNDI if I look in the JMX console.

      Any ideas?

      Kevin Hooke

        • 1. Re: ClassCastException looking up EJB3.0 SLSB
          gajos Newbie


          I have the same problem, this is funny, but when I simply comment out:

           //private EntityManager em;

          jndi lookup works (i.e. myEARName/BeanName/local) :)

          I don't understand it, it tried to learn EJB3 following trailblazer tutorial,
          but I failed with this issue.

          Has anyone the same experience ?

          Best regards,

          • 2. Re: ClassCastException looking up EJB3.0 SLSB
            Kevin Hooke Newbie

            I worked out the issue, and it all depended on the correct config for the persistence.xml file.

            Since the @PersistenceContext annotation introduces a dependency between my SLSB and the Persistence Context/Persistence Unit, if the persistence.xml file is incorrect, the SLSB would not deploy correctly and hence would not be bound in JNDI.

            I found this same issue that you are seeing - remove the @PersistenceContext and then my bean would deploy (minus of course the dependency on the PersistenceContext.

            In my case I had incorrectly packaged the persistence.xml file in the META-INF of the EAR instead of the JAR containing the bean. Once I'd worked that out, repackaged and redeployed, I did run into a string of other datasource issues (MySQL JDBC jar missing. wrong URL in datasource .xml file), but these were easy to fix.

            If deploying and redeploying while the server was already running I noticed that some of the errors were not reporting. If I stopped and restarted the server when my ear was alreasdy deployed, I saw more informative dependency messages, which helped point me in the right direction:

            --- MBeans waiting for other MBeans ---
            ObjectName: jboss.j2ee:service=EJB3,ear=test1.ear,jar=test.jar,name=AddressBookBean
             State: NOTYETINSTALLED
             I Depend On:
            ObjectName: persistence.units:unitName=em
             State: NOTYETINSTALLED
             Depends On Me:

            I also found that also the persistence.xml format had changed. With JBoss 4.0.4RC1, the syntax is like this:
             <persistence-unit name="em">
             <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            Hope that helps,
            Kevin Hooke

            • 3. Re: ClassCastException looking up EJB3.0 SLSB
              Bill Burke Master

              I'm aware of the errors not showing and some of it has been fixed.