HibernateTransactionManager 回滚事务

HibernateTransactionManager rollback transaction

我正在尝试更新一个实体

@Test
@Transactional
public void test(){
    ChatState chatState = chatStateDAO.read(1L);
    chatState.setState(2);
    chatStateDAO.update(chatState);
    chatState.setState(2);
}

更新方法正在调用休眠会话的 update()。这是我日志中的内容

2016-07-03 17:36:28 TRACE SessionImpl:219 - Opened session at timestamp: 14675565886
2016-07-03 17:36:28 TRACE SessionImpl:1493 - Setting flush mode to: ALWAYS
2016-07-03 17:36:28 TRACE DefaultSaveOrUpdateEventListener:210 - Updating detached instance
2016-07-03 17:36:28 TRACE DefaultSaveOrUpdateEventListener:275 - Updating [ru.jeak.telegram.model.ChatState#1]
2016-07-03 17:36:28 TRACE DefaultSaveOrUpdateEventListener:322 - Updating [ru.jeak.telegram.model.ChatState#1]
2016-07-03 17:36:28 TRACE MyHibernateTransactionManager:943 - Triggering beforeCompletion synchronization
2016-07-03 17:36:28 DEBUG MyHibernateTransactionManager:851 - Initiating transaction rollback
2016-07-03 17:36:28 DEBUG MyHibernateTransactionManager:597 - Rolling back Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]
2016-07-03 17:36:28 DEBUG TransactionImpl:86 - rolling back
2016-07-03 17:36:28 TRACE AbstractLogicalConnectionImplementor:114 - Preparing to rollback transaction via JDBC Connection.rollback()
2016-07-03 17:36:28 TRACE AbstractLogicalConnectionImplementor:117 - Transaction rolled-back via JDBC Connection.rollback()
2016-07-03 17:36:28 TRACE AbstractLogicalConnectionImplementor:54 - LogicalConnection#afterTransaction
2016-07-03 17:36:28 TRACE ResourceRegistryStandardImpl:286 - Releasing JDBC resources

MyHibernateTransactionManager 默认为 HibernateTransactionManager。我不明白为什么这个事务会在没有任何错误消息或其他信息的情况下被回滚?

您无需调用 update() 即可使更改持久化。对受管实体的更改会自动保存。 update() 用于将 分离的 实体附加到会话。您的实体已附加。

关于回滚,Spring在测试结束时测试automatically rollback,除非你告诉Spring不要这样做。