在事务提交后使用 spring transactionManager 和 Hibernate 实际上只有两个存储库中的一个正在提交

using spring transactionManager with Hibernate after transaction committed only one repository of two is actually being committed

我有一个 @Transactional 方法可以更改不同的、不相关的存储库的两个实体的状态。

像这样:

@Transactional
public void foo() {
  A a = repoA.findById(1);
  a.setState(s1);
  B b = repoB.findById(1);
  b.setState(s2);
  // (and I also do repoA.save(a); and repoB.save(b); although it is redundant)
} 

我还有一个事务方法 bar,它调用 foo 并发布一个被 TransactionalEventListener 捕获的事件,如下所示:

@Transactional 
public void bar() {
  foo();
  applicationEventPublisher.publishEvent(new AppEvent(123));
}

@Component
public class MyApplicationEventListener {

    @TransactionalEventListener
    public void handleAfterCommit(AppEvent appEvent){
       //do something;
    }
}

现在的问题是,在调用 handleAfterCommit 方法时,有 80% 的时间只有 (A a ) 被提交,但 (B b) 正在丢失其更改。

我需要帮助来理解这里发生了什么,我尝试调试和探索 TransactionAspectSupport.currentTransactionStatus() 但没有找到任何见解。

谢谢,
艾隆

我发现了问题,我们使用的是自定义 AttributeConverter 并且我们没有为相关的 javav 对象实现 Equals,这导致对 select 的每个脏检查都失败并进行完整更新(同时覆盖值已更改)

谢谢