Hibernate Envers 如何通过 EmbeddedId 的属性获取修订
Posted
技术标签:
【中文标题】Hibernate Envers 如何通过 EmbeddedId 的属性获取修订【英文标题】:Hibernate Envers how to get revision by property of EmbeddedId 【发布时间】:2017-07-28 19:15:00 【问题描述】:我需要通过 EmbeddedId 的属性获取修订。但我不知道怎么做。
@Entity
@javax.persistence.Table(name = "orders_details")
@Audited
public class OrderDetail implements Serializable
@EmbeddedId
private OrderDetailsId id;
@Embeddable
public class OrderDetailsId implements Serializable
@Basic
@Column(name = "product_name")
private String prodName;
@Basic
@ManyToOne
@JoinColumn(name = "order_id")
private Order order;
AuditReader auditReader = AuditReaderFactory.get(em);
List list = auditReader.createQuery().forRevisionsOfEntity(Order.class, false, false).
add(AuditEntity.property("id.order").eq(id)).getResultList();
GetResultList 抛出 NullPointer (. 我该怎么办?
好的)这里是堆栈跟踪
> 2017-03-08 11:48:00.548 错误 4548 --- [nio-8080-exec-1] > o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for > 带有路径 [] 的上下文中的 servlet [dispatcherServlet] 引发异常 > [请求处理失败;嵌套异常是 > java.lang.NullPointerException] 的根本原因 > java.lang.NullPointerException:在 org.hibernate.envers.internal.entities.EntitiesConfigurations.getRelationDescription(EntitiesConfigurations.java:98) 处为空 > ~[hibernate-envers-5.0.11.Final.jar:5.0.11.Final] 在 > org.hibernate.envers.query.criteria.internal.CriteriaTools.getRelatedEntity(CriteriaTools.java:44) > ~[hibernate-envers-5.0.11.Final.jar:5.0.11.Final] 在 > org.hibernate.envers.query.criteria.internal.SimpleAuditExpression.addToQuery(SimpleAuditExpression.java:45) > ~[hibernate-envers-5.0.11.Final.jar:5.0.11.Final] 在 > org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:89) > ~[hibernate-envers-5.0.11.Final.jar:5.0.11.Final] 在 > org.hibernate.envers.query.internal.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:93) > ~[hibernate-envers-5.0.11.Final.jar:5.0.11.Final] 在 > com.spring.dao.impl.OrderDetailsDAOImpl.getRevision(OrderDetailsDAOImpl.java:52) > ~[classes/:na] 在 > com.spring.dao.impl.OrderDetailsDAOImpl$$FastClassBySpringCGLIB$$$38c0db60.invoke() > ~[classes/:na] 在 > org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) > ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) > ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > com.spring.dao.impl.OrderDetailsDAOImpl$$EnhancerBySpringCGLIB$$6146d8cb.getRevision() > ~[classes/:na] 在 > com.spring.service.impl.OrderServiceImpl.getOrder(OrderServiceImpl.java:78) > ~[classes/:na] 在 > com.spring.service.impl.OrderServiceImpl$$FastClassBySpringCGLIB$$7d1c6228.invoke() > ~[classes/:na] 在 > org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) > ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) > ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) > ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) > ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) > ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > com.spring.service.impl.OrderServiceImpl$$EnhancerBySpringCGLIB$$d51254c.getOrder() > ~[classes/:na] 在 > com.spring.web.WaitersController.getOrderDetails(WaitersController.java:61) > ~[classes/:na] 在 > sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) > ~[na:1.8.0_111] 在 > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) > ~[na:1.8.0_111] 在 > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > ~[na:1.8.0_111] 在 java.lang.reflect.Method.invoke(Method.java:498) > ~[na:1.8.0_111] 在 > org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > javax.servlet.http.HttpServlet.service(HttpServlet.java:622) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) > ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > javax.servlet.http.HttpServlet.service(HttpServlet.java:729) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) > ~[tomcat-embed-websocket-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) > ~[spring-security-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在 > org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) > ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) > ~[tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) > [na:1.8.0_111] 在 > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) > [na:1.8.0_111] 在 > org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) > [tomcat-embed-core-8.5.11.jar:8.5.11] 在 > java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]【问题讨论】:
你能去掉完整的堆栈跟踪吗?它会很容易理解 【参考方案1】:您收到 NullPointerException
是因为您要求 Envers 查询 API 定位 Order
实体的修订版,而您没有将其指定为已审核。
我已经提交了一个 JIRA 问题 HHH-11558 以更有意义的回复来解决这个问题。
如果Order
被审核,它将按预期工作。
【讨论】:
感谢您的回复。但我还是不明白该怎么办。我不能再编辑嵌套键了,因为太多了。但是我不能理解一件事,为什么审计表有这个字段但我不能直接解决它。也许这一切都可以使用 RevisionEntity 进行返工? 我已经修改了答案@ЮлийЩербак。只要对Order
进行了审核,您实际尝试的操作就会奏效。很抱歉造成混乱,我为它编写的测试用例中有一个错误。
谢谢@Naros。它真的很有帮助。感谢您抽出宝贵的时间为我服务。以上是关于Hibernate Envers 如何通过 EmbeddedId 的属性获取修订的主要内容,如果未能解决你的问题,请参考以下文章
如何强制 Hibernate Envers 在 Spring @Transactional 方法中提交修订
Hibernate Envers @NotAudited 注解
Hibernate Envers:初始化 Envers 代理