创建测试数据时遇到 NullPointerException

Envers NullPointerException when creating test data

我是 Envers 的新手 - 从今天开始。我正在使用 Envers 扩展现有的 Spring Boot 应用程序,支持审计。我注释了所有 @Entity 类 并进行了一些更改,如此处所述 所有数据库表都是完美创建的,但是当我使用 CommandLineRunner 在数据库中生成测试数据时,出现以下错误。

ERROR 17:50 o.s.b.SpringApplication.reportFailure:821: Application run failed 
java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:782)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:763)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:318)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
    at com.agiletunes.productmanager.ProdMgrApp.init(ProdMgrApp.java:59)
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication9(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:541)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.agiletunes.productmanager.services.DatabaseTestInitializationService$$EnhancerBySpringCGLIB$d47d4b.createTestData(<generated>)
    at com.agiletunes.productmanager.ProdMgrApp.lambda(ProdMgrApp.java:129)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)
    ... 8 common frames omitted
Caused by: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:81)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:107)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:532)
    ... 18 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.hibernate.envers.event.spi.BaseEnversEventListener.addCollectionChangeWorkUnit(BaseEnversEventListener.java:109)
    at org.hibernate.envers.event.spi.BaseEnversEventListener.generateBidirectionalCollectionChangeWorkUnits(BaseEnversEventListener.java:76)
    at org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl.onPostInsert(EnversPostInsertEventListenerImpl.java:49)
    at org.hibernate.action.internal.EntityInsertAction.postInsert(EntityInsertAction.java:168)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:135)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3290)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2486)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access0(JdbcResourceLocalTransactionCoordinatorImpl.java:39)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104)
    ... 19 common frames omitted
 INFO 17:50 o.s.o.j.AbstractEntityManagerFactoryBean.destroy:597: Closing JPA EntityManagerFactory for persistence unit 'default' 
 INFO 17:50 c.z.h.HikariDataSource.close:350: HikariPool-1 - Shutdown initiated... 
 INFO 17:50 c.z.h.HikariDataSource.close:352: HikariPool-1 - Shutdown completed. 

在我添加 Envers 和缩短字符串之前,如上面其他 Whosebug 条目中所述,我可以完美地生成测试数据。

问题在于,我在整个堆栈跟踪中看不到任何有用的问题来源信息。如何找出根本原因?

有趣的问题:

How to find out what is the root cause?

堆栈跟踪的最后一部分表示抛出的原始异常:

Caused by: java.lang.NullPointerException: null
    at org.hibernate.envers.event.spi.BaseEnversEventListener.addCollectionChangeWorkUnit(BaseEnversEventListener.java:109)

第一个疯狂的猜测是它可能与集合 null 有关。 如果您的实体模型很小并且您可以轻松检查,那么这种猜测很有用。

如果不能解决问题,下一步就是限制范围。

删除大约一半的实体并检查问题是否仍然存在。

如果重复。

如果不试试另一半

通过这种方式将您的模型缩小到非常小的尺寸。

现在以相同的方式删除属性,直到您有一个小模型来重现该问题。

现在重复你对模型所做的事情,直到你只剩下很少的互动。

一旦您的程序如此简单,问题往往就会变得很明显。 如果不是,这是在这里提问的一个很好的基础。 或者用 Envers 报告错误。

我尝试了很多东西,但是当我在这里和那里进行更改时,我偶然发现了一个缺少 @Audited 注释的超类。现在它运行良好。