[JPA错误]javax.persistence.EntityNotFoundException: Unable to find xxx

[JPA] javax.persistence.EntityNotFoundException: Unable to find com.duona.base.bean.Business with id 1


根本原因:由于后来加上的实体、对象在之前的数据没有关联上,所以造成脏数据、故根本的解决方法,就是将之前的测试数据删除,重新添加数据就行了。

问题原因:


    无论是@OneToOne 还是@ManyToOne,出现这个原因都是因为子表(被关联表)中没有主表(关联表)中ID所对应的记录。

解决办法:

    1. 检查为什么子表中没有主表中ID对应的记录

    2. 如果子表中没有主表ID对应的记录也可以正常加载数据,那么需要在主表字段上加一个@NotFound Annotation。示例如下:

    @OneToOne(optional=false)
    @JoinColumn(name="business_id")
    @NotFound(action=NotFoundAction.IGNORE)
    private Business business;

    这样,当子表中没找到数据时,主表中对应的field就是null,而不会报错了。


从hibernate源码看@NotFound Annotation:

记得NotFound(action=NotFoundAction.IGNORE)会导致Fetch无效,这样的话无论你设置fetchType是lazy还是eager都会以eager加载,一开始我开始模糊的记住,但是过不了2天就会忘,今天有个任务也是配Ignore的,索性就看看里面到底是怎么整的,下面把发现的贴出来:

  先看org.hibernate.type.ManyToOneType

[java]  view plain  copy
  1. public class ManyToOneType extends EntityType {  
  2.       
  3.     private final boolean ignoreNotFound;  
  4.   
  5.     public ManyToOneType(String className) {  
  6.         this( className, false );  
  7.     }  
  8.   
  9.     public ManyToOneType(String className, boolean lazy) {  
  10.         super( className, null, !lazy, truefalse );  
  11.         this.ignoreNotFound = false;  
  12.     }  

      这里可以清楚的看到对ManyToOneType来说 默认的是eager加载(前两个红色标注),ignoreNotFound是false,也就是说默认的NotFound不是ignore,即是Exception

好的,接着往下看org.hibernate.impl.SessionImpl的internalLoad方法:  

[java]  view plain  copy
  1. public Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) throws HibernateException {  
  2.        // todo : remove  
  3.        LoadEventListener.LoadType type = nullable ?  
  4.                LoadEventListener.INTERNAL_LOAD_NULLABLE :  
  5.                eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;  
  6.        LoadEvent event = new LoadEvent(id, entityName, truethis);  
  7.        fireLoad(event, type);  
  8.        if ( !nullable ) {  
  9.            UnresolvableObjectException.throwIfNull( event.getResult(), id, entityName );  
  10.        }  
  11.        return event.getResult();  
  12.    }  

这里假设你ManyToOne上设置的是NotFoundAction.IGNORE,ignoreNotFound这个属性就会设置为true,掉这个方法时传进来的boolean nullable就是true,这是大前提。

看红色字体的三目运算符,a?b:c,计算规则,先计算a,a为true的话计算b的值返回,a为false计算c的值返回。对a?b:c?d:e这个有点复杂,可以这样换算a?b(c?d:e).也就是说a为false的话计算c?d:e的值返回。

由于nullable是true,咱们就看INTERNAL_LOAD_NULLABLE 这几个常量的值。

下面看org.hibernate.event.LoadEventListener:   

[java]  view plain  copy
  1. public static final LoadType INTERNAL_LOAD_EAGER = new LoadType("INTERNAL_LOAD_EAGER")  
  2.             .setAllowNulls(false)  
  3.             .setAllowProxyCreation(false)  
  4.             .setCheckDeleted(false)  
  5.             .setNakedEntityReturned(false);  
  6.       
  7.     public static final LoadType INTERNAL_LOAD_LAZY = new LoadType("INTERNAL_LOAD_LAZY")  
  8.             .setAllowNulls(false)  
  9.             .setAllowProxyCreation(true)  
  10.             .setCheckDeleted(false)  
  11.             .setNakedEntityReturned(false);  
  12.       
  13.     public static final LoadType INTERNAL_LOAD_NULLABLE = new LoadType("INTERNAL_LOAD_NULLABLE")  
  14.             .setAllowNulls(true)  
  15.             .setAllowProxyCreation(false)  
  16.             .setCheckDeleted(false)  
  17.             .setNakedEntityReturned(false);  

这里看到 .setAllowProxyCreation(false) 也就是代理创建false,这样话就不是懒加载!!!看到设置为lazy的是.setAllowProxyCreation(true),设置为eager的是.setAllowProxyCreation(false),基本能说明eager不创建代理,lazy是代理加载

最后再贴段代码证明哪里调用这布尔值:

org.hibernate.event.def.DefaultLoadEventListener 

的proxyOrLoad方法:

[java]  view plain  copy
  1. if ( options.isAllowProxyCreation() ) {  
  2.                     return createProxyIfNecessary( event, persister, keyToLoad, options, persistenceContext );  
  3.                 }  
  4.                 else {  
  5.                     // return a newly loaded object  
  6.                     return load(event, persister, keyToLoad, options);  
  7.                 }  
  8.             }  

看hibernate上的注解写的很清楚,如果AllowProxyCreation这布尔值为false的话会返回一个新的加载对象!


详细错误

javax.persistence.EntityNotFoundException: Unable to find com.duona.base.bean.Business with id 1 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:1075) org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1002) org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:613) org.hibernate.type.EntityType.resolve(EntityType.java:441) 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:999) org.hibernate.loader.Loader.doQuery(Loader.java:878) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293) org.hibernate.loader.Loader.doList(Loader.java:2382) org.hibernate.loader.Loader.doList(Loader.java:2368) org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2198) org.hibernate.loader.Loader.list(Loader.java:2193) org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470) org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355) org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) org.hibernate.internal.SessionImpl.list(SessionImpl.java:1244) org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:257) com.duona.base.dao.impl.AbstractBaseDaoImpl.query(AbstractBaseDaoImpl.java:146) com.duona.base.service.impl.CashTicketServiceImpl.queryCashTickets(CashTicketServiceImpl.java:144) com.duona.base.web.admin.action.CashTicketAdminAction.searchCashTicketListUI(CashTicketAdminAction.java:82) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) com.duona.base.web.interceptor.TransactionInterceptor.intercept(TransactionInterceptor.java:17) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) com.duona.base.web.fifter.PrepareFilter.doFilter(PrepareFilter.java:67) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1770) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)
 
  

你可能感兴趣的:(Unable,to,find,xxx)