|
16 | 16 | package org.springframework.data.jdbc.repository;
|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
| 19 | +import static org.springframework.data.jdbc.testing.AuditableNamedParameterJdbcTemplate.QueryType.*; |
19 | 20 |
|
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Objects;
|
| 23 | +import java.util.Optional; |
22 | 24 | import java.util.concurrent.atomic.AtomicLong;
|
23 | 25 |
|
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
24 | 28 | import org.junit.jupiter.api.Test;
|
25 | 29 | import org.springframework.beans.factory.annotation.Autowired;
|
26 | 30 | import org.springframework.context.annotation.Bean;
|
|
29 | 33 | import org.springframework.context.annotation.FilterType;
|
30 | 34 | import org.springframework.context.annotation.Import;
|
31 | 35 | import org.springframework.data.annotation.Id;
|
| 36 | +import org.springframework.data.annotation.PersistenceCreator; |
| 37 | +import org.springframework.data.annotation.Transient; |
| 38 | +import org.springframework.data.domain.Persistable; |
32 | 39 | import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
33 | 40 | import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
|
| 41 | +import org.springframework.data.jdbc.testing.AuditableNamedParameterJdbcTemplate; |
34 | 42 | import org.springframework.data.jdbc.testing.IntegrationTest;
|
35 | 43 | import org.springframework.data.jdbc.testing.TestConfiguration;
|
36 | 44 | import org.springframework.data.relational.core.mapping.NamingStrategy;
|
| 45 | +import org.springframework.data.relational.core.mapping.Sequence; |
37 | 46 | import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback;
|
38 | 47 | import org.springframework.data.repository.CrudRepository;
|
39 | 48 | import org.springframework.data.repository.ListCrudRepository;
|
40 | 49 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
| 50 | +import org.springframework.test.context.jdbc.Sql; |
| 51 | +import org.testcontainers.shaded.org.checkerframework.checker.index.qual.SameLen; |
41 | 52 |
|
42 | 53 | /**
|
43 | 54 | * Testing special cases for id generation with {@link SimpleJdbcRepository}.
|
44 | 55 | *
|
45 | 56 | * @author Jens Schauder
|
46 | 57 | * @author Greg Turnquist
|
| 58 | + * @author Mikhail Polivakha |
47 | 59 | */
|
48 | 60 | @IntegrationTest
|
49 | 61 | class JdbcRepositoryIdGenerationIntegrationTests {
|
50 | 62 |
|
51 |
| - @Autowired NamedParameterJdbcTemplate template; |
52 |
| - @Autowired ReadOnlyIdEntityRepository readOnlyIdRepository; |
| 63 | + @Autowired ReadOnlyIdEntityRepository readOnlyIdRepository; |
53 | 64 | @Autowired PrimitiveIdEntityRepository primitiveIdRepository;
|
54 | 65 | @Autowired ImmutableWithManualIdEntityRepository immutableWithManualIdEntityRepository;
|
55 | 66 |
|
56 |
| - @Test // DATAJDBC-98 |
| 67 | + @Autowired EntityWithSeqRepository entityWithSeqRepository; |
| 68 | + |
| 69 | + @Autowired PersistableEntityWithSeqRepository persistableEntityWithSeqRepository; |
| 70 | + |
| 71 | + @Autowired PrimitiveIdEntityWithSeqRepository primitiveIdEntityWithSeqRepository; |
| 72 | + |
| 73 | + @Autowired AuditableNamedParameterJdbcTemplate auditableNamedParameterJdbcTemplate; |
| 74 | + |
| 75 | + @BeforeEach |
| 76 | + void setUp() { |
| 77 | + auditableNamedParameterJdbcTemplate.clearRecordedStatements(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test // DATAJDBC-98 |
57 | 81 | void idWithoutSetterGetsSet() {
|
58 | 82 |
|
59 | 83 | ReadOnlyIdEntity entity = readOnlyIdRepository.save(new ReadOnlyIdEntity(null, "Entity Name"));
|
@@ -107,15 +131,126 @@ void manuallyGeneratedIdForSaveAll() {
|
107 | 131 | assertThat(immutableWithManualIdEntityRepository.findAll()).hasSize(2);
|
108 | 132 | }
|
109 | 133 |
|
| 134 | + @Test // DATAJDBC-2003 |
| 135 | + @Sql(statements = "INSERT INTO EntityWithSeq(id, name) VALUES(1, 'initial value');") |
| 136 | + void testUpdateAggregateWithSequence() { |
| 137 | + |
| 138 | + // given. |
| 139 | + EntityWithSeq entityWithSeq = new EntityWithSeq(); |
| 140 | + entityWithSeq.id = 1L; |
| 141 | + entityWithSeq.name = "New name"; |
| 142 | + |
| 143 | + // when. |
| 144 | + EntityWithSeq updated = entityWithSeqRepository.save(entityWithSeq); |
| 145 | + |
| 146 | + // then. |
| 147 | + auditableNamedParameterJdbcTemplate.assertSequenceOfStatements(UPDATE); |
| 148 | + Assertions.assertThat(updated.id).isEqualTo(1L); |
| 149 | + } |
| 150 | + |
| 151 | + @Test // DATAJDBC-2003 |
| 152 | + void whenInsertPersistableAggregateWithSequence_thenClientDefinedIdIsFavored() { |
| 153 | + |
| 154 | + // given. |
| 155 | + long initialId = 1L; |
| 156 | + PersistableEntityWithSeq entityWithSeq = PersistableEntityWithSeq.createNew(initialId, "name"); |
| 157 | + |
| 158 | + // when. |
| 159 | + PersistableEntityWithSeq saved = persistableEntityWithSeqRepository.save(entityWithSeq); |
| 160 | + |
| 161 | + // then. |
| 162 | + Assertions.assertThat(saved.getId()).isEqualTo(initialId); |
| 163 | + |
| 164 | + // We do not expect the SELECT next value from sequence in case we're doing an INSERT with ID provided by the client |
| 165 | + auditableNamedParameterJdbcTemplate.assertSequenceOfStatements(INSERT); |
| 166 | + } |
| 167 | + |
| 168 | + @Test // DATAJDBC-2003 |
| 169 | + void whenInsertAggregateWithSequenceAndPrimitiveIdUnset_thenSequenceIsQueried() { |
| 170 | + |
| 171 | + // given. |
| 172 | + PrimitiveIdEntityWithSeq entity = new PrimitiveIdEntityWithSeq(); |
| 173 | + entity.name = "some name"; |
| 174 | + |
| 175 | + // when. |
| 176 | + PrimitiveIdEntityWithSeq saved = primitiveIdEntityWithSeqRepository.save(entity); |
| 177 | + |
| 178 | + // then. |
| 179 | + Assertions.assertThat(saved.id).isEqualTo(1L); // sequence starts with 1 |
| 180 | + |
| 181 | + // 1. Select from sequence |
| 182 | + // 2. Actual INSERT |
| 183 | + auditableNamedParameterJdbcTemplate.assertSequenceOfStatements(SELECT, INSERT); |
| 184 | + } |
| 185 | + |
110 | 186 | private interface PrimitiveIdEntityRepository extends ListCrudRepository<PrimitiveIdEntity, Long> {}
|
111 | 187 |
|
112 | 188 | private interface ReadOnlyIdEntityRepository extends ListCrudRepository<ReadOnlyIdEntity, Long> {}
|
113 | 189 |
|
114 | 190 | private interface ImmutableWithManualIdEntityRepository extends ListCrudRepository<ImmutableWithManualIdEntity, Long> {}
|
115 | 191 |
|
| 192 | + private interface EntityWithSeqRepository extends ListCrudRepository<EntityWithSeq, Long> {} |
| 193 | + private interface PersistableEntityWithSeqRepository extends ListCrudRepository<PersistableEntityWithSeq, Long> {} |
| 194 | + private interface PrimitiveIdEntityWithSeqRepository extends ListCrudRepository<PrimitiveIdEntityWithSeq, Long> {} |
| 195 | + |
116 | 196 | record ReadOnlyIdEntity(@Id Long id, String name) {
|
117 | 197 | }
|
118 | 198 |
|
| 199 | + static class EntityWithSeq { |
| 200 | + |
| 201 | + @Id |
| 202 | + @Sequence(value = "entity_with_seq_seq") |
| 203 | + private Long id; |
| 204 | + |
| 205 | + private String name; |
| 206 | + } |
| 207 | + |
| 208 | + static class PersistableEntityWithSeq implements Persistable<Long> { |
| 209 | + |
| 210 | + @Id |
| 211 | + @Sequence(value = "persistable_entity_with_seq_seq") |
| 212 | + private Long id; |
| 213 | + |
| 214 | + private String name; |
| 215 | + |
| 216 | + @Transient |
| 217 | + private boolean isNew; |
| 218 | + |
| 219 | + @PersistenceCreator |
| 220 | + public PersistableEntityWithSeq() { |
| 221 | + } |
| 222 | + |
| 223 | + public PersistableEntityWithSeq(Long id, String name, boolean isNew) { |
| 224 | + this.id = id; |
| 225 | + this.name = name; |
| 226 | + this.isNew = isNew; |
| 227 | + } |
| 228 | + |
| 229 | + static PersistableEntityWithSeq createNew(Long id, String name) { |
| 230 | + return new PersistableEntityWithSeq(id, name, true); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public Long getId() { |
| 235 | + return id; |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public boolean isNew() { |
| 240 | + return isNew; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + static class PrimitiveIdEntityWithSeq { |
| 245 | + |
| 246 | + @Id |
| 247 | + @Sequence(value = "primitive_entity_with_seq_seq") |
| 248 | + private long id; |
| 249 | + |
| 250 | + private String name; |
| 251 | + |
| 252 | + } |
| 253 | + |
119 | 254 | static class PrimitiveIdEntity {
|
120 | 255 |
|
121 | 256 | @Id private long id;
|
|
0 commit comments