|
32 | 32 | import org.springframework.data.domain.PageRequest;
|
33 | 33 | import org.springframework.data.domain.Sort;
|
34 | 34 | import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
|
| 35 | +import org.springframework.data.jdbc.core.convert.Identifier; |
35 | 36 | import org.springframework.data.jdbc.core.convert.JdbcConverter;
|
36 | 37 | import org.springframework.data.jdbc.core.convert.MappingJdbcConverter;
|
37 | 38 | import org.springframework.data.jdbc.core.convert.RelationResolver;
|
38 | 39 | import org.springframework.data.mapping.callback.EntityCallbacks;
|
| 40 | +import org.springframework.data.relational.core.conversion.IdValueSource; |
39 | 41 | import org.springframework.data.relational.core.conversion.MutableAggregateChange;
|
40 | 42 | import org.springframework.data.relational.core.mapping.Column;
|
41 | 43 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
|
46 | 48 | import org.springframework.data.relational.core.mapping.event.BeforeDeleteCallback;
|
47 | 49 | import org.springframework.data.relational.core.mapping.event.BeforeSaveCallback;
|
48 | 50 |
|
| 51 | +import java.util.List; |
| 52 | + |
49 | 53 | /**
|
50 | 54 | * Unit tests for {@link JdbcAggregateTemplate}.
|
51 | 55 | *
|
@@ -333,6 +337,38 @@ void deleteAllByIdWithEmptyListDoesNothing() {
|
333 | 337 | template.deleteAllById(emptyList(), SampleEntity.class);
|
334 | 338 | }
|
335 | 339 |
|
| 340 | + @Test // GH-1502 |
| 341 | + void saveThrowsExceptionWhenIdIsNotSet() { |
| 342 | + |
| 343 | + SampleEntity alfred = new SampleEntity(null, "Alfred"); |
| 344 | + when(callbacks.callback(any(), any(), any(Object[].class))).thenReturn(alfred); |
| 345 | + |
| 346 | + when(dataAccessStrategy.insert(eq(alfred), any(Class.class), any(Identifier.class), any(IdValueSource.class))) |
| 347 | + .thenReturn(null); |
| 348 | + |
| 349 | + assertThatIllegalArgumentException().isThrownBy(() -> template.save(alfred)) |
| 350 | + .withMessage("After saving the identifier must not be null"); |
| 351 | + } |
| 352 | + |
| 353 | + @Test // GH-1502 |
| 354 | + void saveThrowsExceptionWhenIdDoesNotExist() { |
| 355 | + |
| 356 | + NoIdEntity alfred = new NoIdEntity("Alfred"); |
| 357 | + |
| 358 | + assertThatIllegalStateException().isThrownBy(() -> template.save(alfred)) |
| 359 | + .withMessage("Required identifier property not found for class %s".formatted(NoIdEntity.class.getName())); |
| 360 | + } |
| 361 | + |
| 362 | + @Test // GH-1502 |
| 363 | + void saveThrowsExceptionWhenIdDoesNotExistOnSaveAll() { |
| 364 | + |
| 365 | + NoIdEntity alfred = new NoIdEntity("Alfred"); |
| 366 | + NoIdEntity berta = new NoIdEntity("Berta"); |
| 367 | + |
| 368 | + assertThatIllegalStateException().isThrownBy(() -> template.saveAll( List.of(alfred, berta))) |
| 369 | + .withMessage("Required identifier property not found for class %s".formatted(NoIdEntity.class.getName())); |
| 370 | + } |
| 371 | + |
336 | 372 | private static class SampleEntity {
|
337 | 373 |
|
338 | 374 | @Column("id1")
|
@@ -451,4 +487,7 @@ public long getVersion() {
|
451 | 487 | return this.version;
|
452 | 488 | }
|
453 | 489 | }
|
| 490 | + |
| 491 | + record NoIdEntity(String name) { |
| 492 | + } |
454 | 493 | }
|
0 commit comments