EntityNotFoundException即使实体实际可用

时间:2016-10-11 14:45:00

标签: mysql spring hibernate exception

我有一个名为branch的表,模型类为Branch.java

当我在id 515上执行getSingleResult时,EntityManager会抛出一个奇怪的错误,如下所示

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();

堆栈跟踪:

  

javax.persistence.EntityNotFoundException:无法找到id为982的com.urbanbuz.model.Branch           在org.hibernate.ejb.Ejb3Configuration $ Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)           在org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:210)           在org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)           在org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)           在org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1078)           在org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1005)           在org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:651)           在org.hibernate.type.EntityType.resolve(EntityType.java:471)           在org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)           在org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)           在org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1103)           在org.hibernate.loader.Loader.processResultSet(Loader.java:960)           在org.hibernate.loader.Loader.doQuery(Loader.java:910)           在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)           在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)           在org.hibernate.loader.Loader.loadEntity(Loader.java:2111)           在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)           在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)           在org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3887)           在org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458)           在org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427)           在org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)           在org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)           在org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)           在org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1078)           在org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1005)           在org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:651)           在org.hibernate.type.EntityType.resolve(EntityType.java:471)           在org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)           在org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)           在org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1103)           在org.hibernate.loader.Loader.processResultSet(Loader.java:960)           在org.hibernate.loader.Loader.doQuery(Loader.java:910)           在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)           在org.hibernate.loader.Loader.doList(Loader.java:2516)           在org.hibernate.loader.Loader.doList(Loader.java:2502)           在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)           在org.hibernate.loader.Loader.list(Loader.java:2327)           在org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)           在org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1783)           在org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231)           在org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)           在org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:283)           在com.urbanbuz.dao.BranchDAO.findById(BranchDAO.java:110)           在com.urbanbuz.dao.BranchDAO $$ FastClassBySpringCGLIB $$ 47398e8.invoke()           在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)           在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)           在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)           在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)           在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)           在com.urbanbuz.dao.BranchDAO $$ EnhancerBySpringCGLIB $$ b0e2bae8.findById()           在com.urbanbuz.service.BranchService.getBranch(BranchService.java:42)           在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)           在com.urbanbuz.mq.MessageProcessor.processMessage(MessageProcessor.java:112)           在com.urbanbuz.mq.CoreController.handleOrder(CoreController.java:22)           at sun.reflect.GeneratedMethodAccessor369.invoke(未知来源)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:498)           在org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)           在org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:327)           在org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:253)           在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:694)           在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:617)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access $ 001(SimpleMessageListenerContainer.java:82)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ 1.invokeListener(SimpleMessageListenerContainer.java:167)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1196)           在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:600)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:960)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:944)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access $ 700(SimpleMessageListenerContainer.java:82)           在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1058)           在java.lang.Thread.run(Thread.java:745)

提前致谢!

1 个答案:

答案 0 :(得分:0)

Branch.class与Device.java(设备表)的映射错误,而设备表有一个branchId 982的条目,该条目不存在。 删除错误的Device.java映射并删除设备表中的错误记录解决了这个问题。