【发布时间】:2022-08-11 20:27:24
【问题描述】:
我在 Spring Boot 集成测试类中有以下代码:
@Autowired
private AddressRepository addressRepository;
// other Repositories that all extend CrudRepository
@BeforeEach
void init(){
Address address = new Address();
// Set up address
address = addressRepository.save(address); //<-- address properly persisted?
Building building = new Building();
building.setAddress(address);
buildingRepository.save(building); //<-- throws error
}
在哪里
@Entity
class Building {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
Address address;
//...
}
和 pom.xml:
//...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>//<-- upping the version breaks things
<relativePath/>
</parent>
//...
在 Spring Boot 2.6.7 上运行流畅。然而,在升级到 2.7.2 之后,保存 building 现在会抛出 org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation。如果我理解正确,Spring 认为 address 尚未持久化,因此无法将其引用存储在 building 中。但它已经坚持在init 的第二行?
我错过了什么?
-
您在 BeforeEach 中没有交易,这就是它失败的原因。但我不能告诉你为什么它以前有效。
标签: java spring-boot jpa