Spring Boot 3.4.0 让 JPA/Hibernate 的集成测试失败

问题描述 投票:0回答:1

我们已从 Spring Boot 3.3.6 更新到版本 3.4.0。现在我们的集成测试(注释

@SpringBootTest
)与 Hibernate/JPA 交互(我们正在使用 spring-data-jpa)突然失败了。

@Test
void retrieveAllFahrzeugbewegungenByMuster() throws Exception {
    // Given
    var musterEntity = MusterMetadatenGenerator.createDefaultMuster();
    var musterEntitySaved = musterMetadatenRepository.saveAndFlush(musterEntity); // crashes here
...
}

例外情况是:

org.springframework.orm.ObjectOptimisticLockingFailureException:行 已被另一笔交易更新或删除(或未保存的值) 映射不正确): [de.muster.jpa.entity.MusterMetadaten#40add996-a1f9-43f7-a157-cf5692e5ea65] 导致:org.hibernate.StaleObjectStateException:行已更新或 被另一个事务删除(或者未保存的值映射被删除) 不正确): [de.muster.jpa.entity.MusterMetadaten#40add996-a1f9-43f7-a157-cf5692e5ea65]

Spring Boot 3.4.0 附带了 Hibernate 6.6.x,之前是 Hibernate 6.5.x。可能有重大变化吗?

java spring-boot hibernate spring-data-jpa
1个回答
0
投票

你能检查一下新实体的

@Id
属性吗?

Spring Boot 3.4.0 升级到 Hibernate 6.6.2.Final #43100

DefaultMergeEventListener 有变化。

休眠 6.5.3.Final(启动 3.3.5)

if ( result == null ) {
  //TODO: we should throw an exception if we really *know* for sure
  //      that this is a detached instance, rather than just assuming
  //throw new StaleObjectStateException(entityName, id);

休眠 6.6.2.Final(启动 3.4.0)

if ( result == null ) {
  LOG.trace( "Detached instance not found in database" );
  // we got here because we assumed that an instance
  // with an assigned id and no version was detached,
  // when it was really transient (or deleted)
  final Boolean knownTransient = persister.isTransient( entity, source );
  if ( knownTransient == Boolean.FALSE ) {
    // we know for sure it's detached (generated id
    // or a version property), and so the instance
    // must have been deleted by another transaction
    throw new StaleObjectStateException( entityName, id );

因此您可能会遇到“行已被另一笔交易更新或删除”的消息。

此更改可以遵循以下问题:

如果您想修复测试代码,请首先显示您的 MusterMetadatenGenerator.createDefaultMuster() 代码。

© www.soinside.com 2019 - 2024. All rights reserved.