1

I'm using the Netbeans 7.1 and GlassFish server 3.1 to deploy my first Java EE project. I'm having a problem which is haunting me since the beginning of the project, the code just throws out the exception below (The main one is also the weirdest - java.lang.ClassCastException: com.bestdeal.entity.Games cannot be cast to com.bestdeal.entity.Games), after I make any change/compilation on the ejb code. The problem seems to always be fixed when I restart the glassfish server, but if I make any changes on the code this same problem appears again.

I'm using JPA, EntityManager and EntityManagerFactory to construct an EntityManager when I want. The access to the DB occurs after I make a request through a faces page, which then calls a function in the Managed Bean which in turns triggers a request for dataaccess class in the EBJ code (which I show below).

Any ideas on what can be causing this annoying problem?

javax.ejb.EJBException

javax.faces.el.EvaluationException: javax.ejb.EJBException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.ejb.EJBException
    at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193)
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy219.prepareSession(Unknown Source)
    at com.bestdeal.ejb.__EJB31_Generated__BestDeal__Intf____Bean__.prepareSession(Unknown Source)
    at com.bestdeal.web.BestDealMBean.prepareSession(BestDealMBean.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:737)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:254)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:228)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 33 more
Caused by: java.lang.ClassCastException: com.bestdeal.entity.Games cannot be cast to com.bestdeal.entity.Games
    at com.bestdeal.ejb.DataAccess.GetSessionInit(DataAccess.java:263)
    at com.bestdeal.ejb.EventProcessor.StartEventProcessor(EventProcessor.java:65)
    at com.bestdeal.ejb.BestDeal.prepareSession(BestDeal.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
    ... 47 more

EJB code:

public class DataAccess {
    private final EntityManagerFactory emf;
    private EntityManager em;
    public DataAccess() {
        emf = Persistence.createEntityManagerFactory("Best-ejbPU");
        em = emf.createEntityManager();
    }

    public Games GetSessionInit(int SessionId){

        em.clear();
        Query query = em.createNativeQuery("update games set PeopleInStore=MarketShare*MarketTotal where gameindex=1;");
        query.executeUpdate();


        Query querysql = em.createQuery("select g from Games g where g.sessionNumber=" + SessionId);
        List<Games> l = querysql.getResultList();
        return l.get(0);//=>javax.ejb.EJBException occurs here !!!!!!!!!!!!!!!!!!
    }
}
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Jitesh Pramodray
  • 424
  • 1
  • 7
  • 16
  • Related question: [Servlet Exception + Class Cast Exception + Glassfish + ...](http://stackoverflow.com/q/1853248/620338) – Matt Handy Mar 01 '12 at 07:53
  • also: http://stackoverflow.com/questions/2614989/brainstorming-weird-jpa-problem-possibly-classpath-or-jar-versioning-problem – Jim Mar 02 '12 at 15:48

1 Answers1

0

It's a little depended on how you've setup your persistence unit, but I'm not really surprised that this doesn't work.

The method you've used to obtain the entity manager is the Java SE method. This is not necessarily supported in Java EE. I'm sure the same code doesn't run on JBoss AS either.

Unless you've skipped the annotation, the bean you're showing is also not an EJB (hence, it's not EJB code what you're showing).

In Java EE, you should either inject the entity manager, or look it up from JNDI. Your bean would then look something like this:

@Stateless
public class DataAccess {

    @PersistenceContext    
    private EntityManager em;

    public Games getSessionInit(int SessionId) {            
        em.createNativeQuery("update games set PeopleInStore = MarketShare * MarketTotal where gameindex = 1;")
          .executeUpdate();

        return em.createQuery("select g from Games g where g.sessionNumber=" + SessionId)
                 .getResultList()
                 .get(0);
    }
}

(Unrelated to the question, but you might want to refactor your code not to blindly take the first element; the list might be empty. Additionally consider putting your queries into separate files using the named query mechanism, see e.g. Where to put named queries in JPA)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140