Skip to content

Commit 613b5ad

Browse files
committed
#283 - Fix Id property and column name usage in SimpleR2dbcRepository.
We now use the Id property name when using the converter to map property names to column names. In other places, where we don't use the converter, we stick with the Id column name.
1 parent 44447da commit 613b5ad

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
3131
import org.springframework.data.r2dbc.core.StatementMapper;
3232
import org.springframework.data.r2dbc.query.Criteria;
33+
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3334
import org.springframework.data.relational.core.sql.Functions;
3435
import org.springframework.data.relational.core.sql.Select;
3536
import org.springframework.data.relational.core.sql.StatementBuilder;
@@ -129,12 +130,12 @@ public Mono<T> findById(ID id) {
129130
Assert.notNull(id, "Id must not be null!");
130131

131132
List<String> columns = this.accessStrategy.getAllColumns(this.entity.getJavaType());
132-
String idColumnName = getIdColumnName();
133+
String idProperty = getIdProperty().getName();
133134

134135
StatementMapper mapper = this.accessStrategy.getStatementMapper().forType(this.entity.getJavaType());
135136
StatementMapper.SelectSpec selectSpec = mapper.createSelect(this.entity.getTableName()) //
136137
.withProjection(columns) //
137-
.withCriteria(Criteria.where(idColumnName).is(id));
138+
.withCriteria(Criteria.where(idProperty).is(id));
138139

139140
PreparedOperation<?> operation = mapper.getMappedObject(selectSpec);
140141

@@ -160,12 +161,12 @@ public Mono<Boolean> existsById(ID id) {
160161

161162
Assert.notNull(id, "Id must not be null!");
162163

163-
String idColumnName = getIdColumnName();
164+
String idProperty = getIdProperty().getName();
164165

165166
StatementMapper mapper = this.accessStrategy.getStatementMapper().forType(this.entity.getJavaType());
166167
StatementMapper.SelectSpec selectSpec = mapper.createSelect(this.entity.getTableName())
167-
.withProjection(Collections.singletonList(idColumnName)) //
168-
.withCriteria(Criteria.where(idColumnName).is(id));
168+
.withProjection(Collections.singletonList(idProperty)) //
169+
.withCriteria(Criteria.where(idProperty).is(id));
169170

170171
PreparedOperation<?> operation = mapper.getMappedObject(selectSpec);
171172

@@ -217,12 +218,12 @@ public Flux<T> findAllById(Publisher<ID> idPublisher) {
217218
}
218219

219220
List<String> columns = this.accessStrategy.getAllColumns(this.entity.getJavaType());
220-
String idColumnName = getIdColumnName();
221+
String idProperty = getIdProperty().getName();
221222

222223
StatementMapper mapper = this.accessStrategy.getStatementMapper().forType(this.entity.getJavaType());
223224
StatementMapper.SelectSpec selectSpec = mapper.createSelect(this.entity.getTableName()) //
224225
.withProjection(columns) //
225-
.withCriteria(Criteria.where(idColumnName).in(ids));
226+
.withCriteria(Criteria.where(idProperty).in(ids));
226227

227228
PreparedOperation<?> operation = mapper.getMappedObject(selectSpec);
228229

@@ -238,7 +239,7 @@ public Mono<Long> count() {
238239

239240
Table table = Table.create(this.entity.getTableName());
240241
Select select = StatementBuilder //
241-
.select(Functions.count(table.column(getIdColumnName()))) //
242+
.select(Functions.count(table.column(getIdProperty().getColumnName()))) //
242243
.from(table) //
243244
.build();
244245

@@ -260,7 +261,7 @@ public Mono<Void> deleteById(ID id) {
260261
return this.databaseClient.delete() //
261262
.from(this.entity.getJavaType()) //
262263
.table(this.entity.getTableName()) //
263-
.matching(Criteria.where(getIdColumnName()).is(id)) //
264+
.matching(Criteria.where(getIdProperty().getName()).is(id)) //
264265
.fetch() //
265266
.rowsUpdated() //
266267
.then();
@@ -285,7 +286,7 @@ public Mono<Void> deleteById(Publisher<ID> idPublisher) {
285286
return this.databaseClient.delete() //
286287
.from(this.entity.getJavaType()) //
287288
.table(this.entity.getTableName()) //
288-
.matching(Criteria.where(getIdColumnName()).in(ids)) //
289+
.matching(Criteria.where(getIdProperty().getName()).in(ids)) //
289290
.fetch() //
290291
.rowsUpdated();
291292
}).then();
@@ -339,12 +340,11 @@ public Mono<Void> deleteAll() {
339340
return this.databaseClient.delete().from(this.entity.getTableName()).then();
340341
}
341342

342-
private String getIdColumnName() {
343+
private RelationalPersistentProperty getIdProperty() {
343344

344345
return this.converter //
345346
.getMappingContext() //
346347
.getRequiredPersistentEntity(this.entity.getJavaType()) //
347-
.getRequiredIdProperty() //
348-
.getColumnName();
348+
.getRequiredIdProperty();
349349
}
350350
}

0 commit comments

Comments
 (0)