5 Replies Latest reply on Oct 20, 2005 12:08 PM by bill.burke

    ClassCastException When Passing an Entity POJO from Stateles

    yxw84

      Here is the scenario. I have a Servlet which calls a simple stateless session bean. The session bean has just a single find method which retrieves an entity bean from the database. Here is the code:

      Servlet:
      // lookup the session bean
      InitialContext ctx = new InitialContext();
      SessionTest st = (SessionTest) ctx.lookup(SessionTest.class.getName());

      // find an instance of MyObject with id of 1
      MyObject mo = st.getMyObject(1);

      Stateless Session:
      public MyObject getMyObject(int id) {
      MyObject result = (MyObject) em.createQuery("FROM MyObject m where m.id = :id")
      .setParameter ("id", id)
      .getSingleResult();

      System.err.println("Query was successful, at least.");
      System.err.println("ID = " + result.getId());
      return result;
      }

      Assume that the entity bean works, I've tested it without the session facade. Here is the output:

      11:29:15,127 INFO [STDOUT] Query was successful, at least.
      11:29:15,127 INFO [STDOUT] ID = 1
      11:29:15,135 ERROR [[EJB3Servlet]] Servlet.service() for servlet EJB3Servlet threw exception
      java.lang.ClassCastException: MyCompany.MyObject
      at $Proxy142.getMyObject(Unknown Source)
      at MyCompany.EJB3Servlet.doGet(EJB3Servlet.java:66)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      at java.lang.Thread.run(Thread.java:595)

      What is puzzling is that the instance of MyObject with id of 1 is retrieved from the database, and a method is invoked on it to get its ID, but when it is returned, there is a ClassCastException. MyObject implements Serializable. Also, if we convert the return result to a String, it works just fine.

      Does anyone have any insight into what might be causing the problem?

      Thanks.