【问题标题】:Spring JPA skips inserting entity sometimesSpring JPA 有时会跳过插入实体
【发布时间】:2016-04-22 14:36:40
【问题描述】:

我有一个 springboot 应用程序,并且有一个自定义实现来记录 JPA 实体的更改历史记录。

我的主要实体是 Test,它是从 Base 类扩展而来的。基类有如下注解

@EntityListeners(HistoryEntityUpdater.class)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "object_type")

我有一个自定义实体侦听器,其方法用@PostPersist@PostUpdate 注释。在这些方法中,我创建了一个TestHistory 的实例并调用entityManager.persist。虽然这可行,但一些历史记录没有被插入。这是调用 save 时的日志,但数据库没有更新。

2016-04-22 19:57:58.428 TRACE 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Releasing statement [sql : 'update managed_object set last_updated_time=?, name=?, workflow_status=? where id=?', parameters : ['2016-04-22 19:57:26.185','tests1','ERROR',1]]
2016-04-22 19:57:58.429 TRACE 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Closing prepared statement [sql : 'update managed_object set last_updated_time=?, name=?, workflow_status=? where id=?', parameters : ['2016-04-22 19:57:26.185','tests1','ERROR',1]]
2016-04-22 19:57:58.429 TRACE 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Starting after statement execution processing [ON_CLOSE]
2016-04-22 19:57:58.429 TRACE 18060 --- [nio-9285-exec-2] o.h.internal.util.SerializationHelper    : Starting clone through serialization
2016-04-22 19:57:58.429 TRACE 18060 --- [nio-9285-exec-2] o.h.internal.util.SerializationHelper    : Starting serialization of object [2016-04-22T14:27:26.185Z]
2016-04-22 19:57:58.429 TRACE 18060 --- [nio-9285-exec-2] o.h.internal.util.SerializationHelper    : Starting deserialization of object
2016-04-22 19:57:58.433 TRACE 18060 --- [nio-9285-exec-2] o.h.internal.util.SerializationHelper    : Attempting to locate class [java.time.Ser]
2016-04-22 19:57:58.434 DEBUG 18060 --- [nio-9285-exec-2] c.b.b.m.db.history.HistoryEntityUpdater  : postUpdate com.xyz.model.db.Test@4dcfc8b8
2016-04-22 19:57:58.435 TRACE 18060 --- [nio-9285-exec-2] o.hibernate.engine.spi.IdentifierValue   : ID unsaved-value strategy UNDEFINED
2016-04-22 19:57:58.436 TRACE 18060 --- [nio-9285-exec-2] o.h.e.i.AbstractSaveEventListener        : Transient instance of: com.xyz.model.db.history.TestHistory
2016-04-22 19:57:58.436 TRACE 18060 --- [nio-9285-exec-2] o.h.e.i.DefaultPersistEventListener      : Saving transient instance
2016-04-22 19:57:58.437 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.i.AbstractSaveEventListener        : Generated identifier: component[id,revisionTime,revisionType]{revisionType=UPDATED, id=1, revisionTime=Fri Apr 22 19:57:58 IST 2016}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] o.h.e.i.AbstractSaveEventListener        : Saving [com.xyz.model.db.history.TestHistory#component[id,revisionTime,revisionType]{revisionType=UPDATED, id=1, revisionTime=Fri Apr 22 19:57:58 IST 2016}]
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.engine.spi.ActionQueue     : Adding an EntityInsertAction for [com.xyz.model.db.history.TestHistory] object
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.engine.spi.ActionQueue     : Adding insert with no non-nullable, transient entities: [EntityInsertAction[com.xyz.model.db.history.TestHistory#com.xyz.model.db.history.HistoryObject$HistoryId@4b74eea0]]
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.engine.spi.ActionQueue     : Adding resolved non-early insert action.
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] o.h.a.i.UnresolvedEntityInsertActions    : No unresolved entity inserts that depended on [[com.xyz.db.history.TestHistory#com.xyz.db.history.HistoryObject$HistoryId@4b74eea0]]
2016-04-22 19:57:58.437 TRACE 18060 --- [nio-9285-exec-2] o.h.a.i.UnresolvedEntityInsertActions    : No entity insert actions have non-nullable, transient entity dependencies.
2016-04-22 19:57:58.438 TRACE 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Starting after statement execution processing [ON_CLOSE]
2016-04-22 19:57:58.438 TRACE 18060 --- [nio-9285-exec-2] o.h.e.i.AbstractFlushingEventListener    : Post flush
2016-04-22 19:57:58.438 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.internal.SessionImpl       : before transaction completion
2016-04-22 19:57:58.440 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.t.internal.jdbc.JdbcTransaction    : committed JDBC Connection
2016-04-22 19:57:58.440 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.t.internal.jdbc.JdbcTransaction    : re-enabling autocommit
2016-04-22 19:57:58.441 TRACE 18060 --- [nio-9285-exec-2] o.h.e.t.i.TransactionCoordinatorImpl     : after transaction completion
2016-04-22 19:57:58.442 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.internal.SessionImpl       : after transaction completion
2016-04-22 19:57:58.443 TRACE 18060 --- [nio-9285-exec-2] org.hibernate.internal.SessionImpl       : Closing session
2016-04-22 19:57:58.443 TRACE 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@5be5a598]
2016-04-22 19:57:58.443 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : HHH000420: Closing un-released batch
2016-04-22 19:57:58.443 TRACE 18060 --- [nio-9285-exec-2] o.h.e.j.internal.LogicalConnectionImpl   : Closing logical connection
2016-04-22 19:57:58.443 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.j.internal.LogicalConnectionImpl   : Releasing JDBC connection
2016-04-22 19:57:58.443 DEBUG 18060 --- [nio-9285-exec-2] o.h.e.j.internal.LogicalConnectionImpl   : Released JDBC connection
2016-04-22 19:57:58.443 TRACE 18060 --- [nio-9285-exec-2] o.h.e.j.internal.LogicalConnectionImpl   : Logical connection closed

HistoryEntityUpdater 上的 postUpdatepostPersist 方法用 @Transactional 注释

【问题讨论】:

    标签: java hibernate jpa spring-boot


    【解决方案1】:

    JPA 规范规定 EntityListener 不应使用 EntityManager。 这使得它们对于大多数用例(包括您的用例)都毫无用处。

    我也不知道。

    从您的堆栈跟踪看来,JDBC 事务已经提交并且实体管理器已刷新。 所以至少当你的监听器运行时你没有任何事务。这可能会导致写入的历史实体未写入数据库的情况。 EntityListener 由 hibernate 而不是 spring 调用,因此您的侦听器上的任何 @Transactional 注释都将不起作用。

    终于有一个解决方案,使用'Envers'来实现实体审计:

    database auditing jpa

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2015-10-28
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      相关资源
      最近更新 更多