本文主要是介绍Hibernate中通过传入实体的主键id来使用get方法获取整个实体的内容问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
做老项目的一个需求变更,遇到一个问题,我直接调用封装好的dao中的get方法,传入参数这个实体的主键,结果报下面的错误:
八月 31, 2018 11:14:49 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springServlet] in context with path [/JKGL2] threw exception [Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: java.lang.Object] with root cause
org.hibernate.MappingException: Unknown entity: java.lang.Objectat org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1131)at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2484)at org.hibernate.internal.SessionImpl.access$1900(SessionImpl.java:174)at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2426)at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2430)at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2417)at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1064)at org.hibernate.internal.SessionImpl.get(SessionImpl.java:987)at com.wonders.ph.common.persistence.AbstractDao.get(AbstractDao.java:185)at com.wonders.ph.jksite.modules.mg.service.MgUpdateYypbSelectService.queryData(MgUpdateYypbSelectService.java:39)at com.wonders.ph.jksite.modules.mg.service.MgUpdateYypbSelectService$$FastClassByCGLIB$$2b6629d1.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)at com.wonders.ph.jksite.modules.mg.service.MgUpdateYypbSelectService$$EnhancerByCGLIB$$877fe5d4.queryData(<generated>)at com.wonders.ph.jksite.modules.mg.service.MgUpdateYypbSelectService$$FastClassByCGLIB$$2b6629d1.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)at com.wonders.ph.jksite.modules.mg.service.MgUpdateYypbSelectService$$EnhancerByCGLIB$$a880b7aa.queryData(<generated>)at com.wonders.ph.jksite.modules.mg.MgUpdateYypbSelectController.init(MgUpdateYypbSelectController.java:36)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:606)at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at com.wonders.ph.jksite.modules.common.filter.XssFilter.doFilter(XssFilter.java:33)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:744)
我原来代码是这样写的:
entity po = (entity)dao.get(id);
查了一下出错原因:
(1)系统封装好的dao继承了basedao,而basedao的泛型类型为Objec
(2)我直接传进去的是一个字符串id,也就是该实体的主键,但是看里层的代码,是getSession().get(Entity.class,id);
,所以如果使用系统的dao,且只传字符串id,那么是无法识别该Entity,所以会报unknown Entity异常
(3)上网查了一下,有的人说是配置文件,映射有问题,我检查了不存在问题;有人说是因为需要将那个值set到实体,传进去一个对象,我试了也不可以,还是报unknown Entity异常
解决办法:
我没有使用dao.get(id)的形式,如果要使用这个形式,那么dao应该是我自定义,不是使用系统封装好的dao,并且我定义dao需要继承basedao且泛型参数为我的对应实体,因为我一次需要操作三张表,所以我没有自定义DAO,还是使用封装好的,只不过使用方法有些区别:
dao.getSession.get(Entity.class,id);
这样的话就不再报unknown Entity的异常了。
这篇关于Hibernate中通过传入实体的主键id来使用get方法获取整个实体的内容问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!