title: Exception on EntityManager only after compiling code on Netbeans 7.1 and GlassFish Server 3.1
body: <p>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.</p>
  
  <p>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).</p>
  
  <p>Any ideas on what can be causing this annoying problem? </p>
  
  <pre><code>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
  </code></pre>
  
  <p>EJB code:</p>
  
  <pre><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&lt;Games&gt; l = querysql.getResultList();
          return l.get(0);//=&gt;javax.ejb.EJBException occurs here !!!!!!!!!!!!!!!!!!
      }
  }
  </code></pre>