Reputation: 1171
In Jpa Spring Data when using saveAll
for entities with the same ids - saveAll
successfully saves all the enitities into the database. For example
public interface JpaRepository extends JpaRepository<Entity, String> {}
...
...
Entity entity1 = Entity.build()
.version(1L)
.id(123)
.status("NEW")
.build;
...
Entity entity2 = Entity.build()
.version(1L) <---- has the same version as in entity1
.id(123) <---- has the same id as in entity1
.status("IN_PROGRESS")
.build;
...
...
jpaRepository.saveAll(List.of(entity1, entity2));
This jpaRepository.saveAll(List.of(entity1, entity2))
works successfully with no issues. The result of operation will be the record with version=2L
and status="IN_PROGRESS"
.
If we use r2dbc repository:
public interface R2dbcRepository extends ReactiveCrudRepository<Entity, String><Entity, String> {}
...
r2dbcRepository.saveAll(List.of(entity1, entity2)).blockLast();
The operation of r2dbcRepository.saveAll(List.of(entity1, entity2)).blockLast()
fails with an exception:
Failed to update table [entity]; Version does not match for row with Id [123] org.springframework.dao.OptimisticLockingFailureException: Failed to update table [entity]; Version does not match for row with Id [123] at org.springframework.data.r2dbc.core.R2dbcEntityTemplate.lambda$doUpdate$12(R2dbcEntityTemplate.java:637)
Why those JpaRepository
and R2dbcRepository
repositories works differently when saving a list of entities with the same ids? Is there any documentation how they treat entities with the same ids in saveAll
operation.
Java 21.0.2; SpringBoot 3.2.2; postgresql 42.2.4; r2dbc 1.0.7.RELEASE
Upvotes: 0
Views: 47