title: EntityNotFoundException Even if the entity is actually available
body: <p>I have a table with name <code>branch</code> and the model class is <code>Branch.java</code></p>
  
  <p>When I do a getSingleResult on <code>id</code> 515, <code>EntityManager</code> is throwing a weird error as below</p>
  
  <pre><code>Query query = getEntityManager()
      .createNativeQuery(
          "SELECT * FROM branch t where t.enabled=true and t.id = :value1",
          Branch.class);
  query.setParameter("value1", id);
  branch = (Branch) query.getSingleResult();
  </code></pre>
  
  <p>StackTrace:</p>
  
  <blockquote>
    <p>javax.persistence.EntityNotFoundException: Unable to find com.urbanbuz.model.Branch with id 982
            at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)
            at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:210)
            at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
            at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
            at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1078)
            at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1005)
            at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:651)
            at org.hibernate.type.EntityType.resolve(EntityType.java:471)
            at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
            at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)
            at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1103)
            at org.hibernate.loader.Loader.processResultSet(Loader.java:960)
            at org.hibernate.loader.Loader.doQuery(Loader.java:910)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)
            at org.hibernate.loader.Loader.loadEntity(Loader.java:2111)
            at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
            at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
            at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3887)
            at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458)
            at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427)
            at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
            at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
            at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
            at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1078)
            at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1005)
            at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:651)
            at org.hibernate.type.EntityType.resolve(EntityType.java:471)
            at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
            at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)
            at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1103)
            at org.hibernate.loader.Loader.processResultSet(Loader.java:960)
            at org.hibernate.loader.Loader.doQuery(Loader.java:910)
            at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
            at org.hibernate.loader.Loader.doList(Loader.java:2516)
            at org.hibernate.loader.Loader.doList(Loader.java:2502)
            at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
            at org.hibernate.loader.Loader.list(Loader.java:2327)
            at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
            at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1783)
            at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231)
            at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
            at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:283)
            at com.urbanbuz.dao.BranchDAO.findById(BranchDAO.java:110)
            at com.urbanbuz.dao.BranchDAO$$FastClassBySpringCGLIB$$47398e8.invoke()
            at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
            at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
            at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
            at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267)
         at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
            at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
            at com.urbanbuz.dao.BranchDAO$$EnhancerBySpringCGLIB$$b0e2bae8.findById()
            at com.urbanbuz.service.BranchService.getBranch(BranchService.java:42)
            at com.urbanbuz.component.SignupComponent.signUpUpload(SignupComponent.java:3030)
            at sun.reflect.GeneratedMethodAccessor1180.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at com.urbanbuz.mq.MessageProcessor.processMessage(MessageProcessor.java:112)
            at com.urbanbuz.mq.CoreController.handleOrder(CoreController.java:22)
            at sun.reflect.GeneratedMethodAccessor369.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
            at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:327)
            at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:253)
            at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:694)
            at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:617)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:82)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:167)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1196)
            at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:600)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:960)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:944)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:82)
            at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1058)
            at java.lang.Thread.run(Thread.java:745)</p>
  </blockquote>
  
  <p>Thanks in advance!</p>