2016-04-22 90 views
0

我有一個springboot應用程序,並有一個自定義實現來記錄JPA實體上的更改歷史記錄。Spring JPA有時跳過插入實體

我的主要實體是測試,它從基類擴展。基類具有以下注釋

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

我與@PostPersist@PostUpdate註解的方法自定義實體監聽器。在這些方法中,我創建了一個TestHistory的實例並調用entityManager.persist。雖然這可行,但一些歷史記錄沒有被插入。以下是調用保存時的日誌,但數據庫未更新。

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 [email protected] 
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#[email protected]]] 
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#[email protected]]] 
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 [[email protected]] 
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 

HistoryEntityUpdaterpostUpdatepostPersist方法標註有@Transactional

回答

2

是EntityListeners不應該使用EntityManager的JPA規範的狀態。 這使得它們在大多數使用情況下都無用,包括你的使用情況。

我還沒有意識到這一點。

從您的堆棧跟蹤看起來像JDBC事務已經提交併且entitymanager被刷新。 因此,至少在偵聽器運行時您沒有任何事務。這可能會導致寫入的歷史實體未寫入數據庫的情況。 EntityListener由hibernate調用,而不是由spring調用,因此您的偵聽器上的任何@Transactional註釋都不起作用。

最後有你的解決方案,使用 'Envers' 實施實體審計:

database auditing jpa