|
37 | 37 | import java.util.function.Consumer;
|
38 | 38 | import java.util.stream.Stream;
|
39 | 39 |
|
| 40 | +import org.assertj.core.api.Assertions; |
40 | 41 | import org.junit.jupiter.api.BeforeEach;
|
41 | 42 | import org.junit.jupiter.api.Test;
|
42 | 43 | import org.junit.jupiter.params.ParameterizedTest;
|
|
50 | 51 | import org.springframework.context.annotation.Configuration;
|
51 | 52 | import org.springframework.context.annotation.Import;
|
52 | 53 | import org.springframework.core.io.ClassPathResource;
|
| 54 | +import org.springframework.dao.DuplicateKeyException; |
53 | 55 | import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
54 | 56 | import org.springframework.data.annotation.Id;
|
| 57 | +import org.springframework.data.annotation.Transient; |
55 | 58 | import org.springframework.data.domain.Example;
|
56 | 59 | import org.springframework.data.domain.ExampleMatcher;
|
57 | 60 | import org.springframework.data.domain.Limit;
|
58 | 61 | import org.springframework.data.domain.Page;
|
59 | 62 | import org.springframework.data.domain.PageRequest;
|
60 | 63 | import org.springframework.data.domain.Pageable;
|
| 64 | +import org.springframework.data.domain.Persistable; |
61 | 65 | import org.springframework.data.domain.ScrollPosition;
|
62 | 66 | import org.springframework.data.domain.Slice;
|
63 | 67 | import org.springframework.data.domain.Sort;
|
@@ -113,6 +117,8 @@ public class JdbcRepositoryIntegrationTests {
|
113 | 117 |
|
114 | 118 | @Autowired NamedParameterJdbcTemplate template;
|
115 | 119 | @Autowired DummyEntityRepository repository;
|
| 120 | + |
| 121 | + @Autowired ProvidedIdEntityRepository providedIdEntityRepository; |
116 | 122 | @Autowired MyEventListener eventListener;
|
117 | 123 | @Autowired RootRepository rootRepository;
|
118 | 124 |
|
@@ -193,6 +199,18 @@ public void findAllFindsAllSpecifiedEntities() {
|
193 | 199 | .containsExactlyInAnyOrder(entity.getIdProp(), other.getIdProp());
|
194 | 200 | }
|
195 | 201 |
|
| 202 | + @Test // DATAJDBC-611 |
| 203 | + public void testDuplicateKeyExceptionIsThrownInCaseOfUniqueKeyViolation() { |
| 204 | + |
| 205 | + // given. |
| 206 | + ProvidedIdEntity first = ProvidedIdEntity.newInstance(1L, "name"); |
| 207 | + ProvidedIdEntity second = ProvidedIdEntity.newInstance(1L, "other"); |
| 208 | + |
| 209 | + // when/then |
| 210 | + Assertions.assertThatCode(() -> providedIdEntityRepository.save(first)).doesNotThrowAnyException(); |
| 211 | + Assertions.assertThatThrownBy(() -> providedIdEntityRepository.save(second)).isInstanceOf(DuplicateKeyException.class); |
| 212 | + } |
| 213 | + |
196 | 214 | @Test // DATAJDBC-97
|
197 | 215 | public void countsEntities() {
|
198 | 216 |
|
@@ -1421,6 +1439,10 @@ interface DummyProjectExample {
|
1421 | 1439 | String getName();
|
1422 | 1440 | }
|
1423 | 1441 |
|
| 1442 | + interface ProvidedIdEntityRepository extends CrudRepository<ProvidedIdEntity, Long> { |
| 1443 | + |
| 1444 | + } |
| 1445 | + |
1424 | 1446 | interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, QueryByExampleExecutor<DummyEntity> {
|
1425 | 1447 |
|
1426 | 1448 | @Lock(LockMode.PESSIMISTIC_WRITE)
|
@@ -1526,6 +1548,11 @@ DummyEntityRepository dummyEntityRepository() {
|
1526 | 1548 | return factory.getRepository(DummyEntityRepository.class);
|
1527 | 1549 | }
|
1528 | 1550 |
|
| 1551 | + @Bean |
| 1552 | + ProvidedIdEntityRepository providedIdEntityRepository() { |
| 1553 | + return factory.getRepository(ProvidedIdEntityRepository.class); |
| 1554 | + } |
| 1555 | + |
1529 | 1556 | @Bean
|
1530 | 1557 | RootRepository rootRepository() {
|
1531 | 1558 | return factory.getRepository(RootRepository.class);
|
@@ -1839,6 +1866,39 @@ private static DummyEntity createEntity(String entityName, Consumer<DummyEntity>
|
1839 | 1866 | return entity;
|
1840 | 1867 | }
|
1841 | 1868 |
|
| 1869 | + |
| 1870 | + static class ProvidedIdEntity implements Persistable { |
| 1871 | + |
| 1872 | + @Id |
| 1873 | + private Long id; |
| 1874 | + |
| 1875 | + private String name; |
| 1876 | + |
| 1877 | + @Transient |
| 1878 | + private boolean isNew; |
| 1879 | + |
| 1880 | + private ProvidedIdEntity(Long id, String name, boolean isNew) { |
| 1881 | + this.id = id; |
| 1882 | + this.name = name; |
| 1883 | + this.isNew = isNew; |
| 1884 | + } |
| 1885 | + |
| 1886 | + private static ProvidedIdEntity newInstance(Long id, String name) { |
| 1887 | + return new ProvidedIdEntity(id, name, true); |
| 1888 | + } |
| 1889 | + |
| 1890 | + @Override |
| 1891 | + public Object getId() { |
| 1892 | + return id; |
| 1893 | + } |
| 1894 | + |
| 1895 | + @Override |
| 1896 | + public boolean isNew() { |
| 1897 | + return isNew; |
| 1898 | + } |
| 1899 | + } |
| 1900 | + |
| 1901 | + |
1842 | 1902 | static class DummyEntity {
|
1843 | 1903 |
|
1844 | 1904 | String name;
|
|
0 commit comments