Skip to content

Commit 09f9da5

Browse files
viktorardeleanschauder
authored andcommitted
Improved error message on missing back reference.
Closes #833 Original pull request 1384
1 parent d0a8223 commit 09f9da5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ private Condition buildConditionForBackReference(Identifier parentIdentifier, Ta
260260

261261
Condition condition = null;
262262
for (SqlIdentifier backReferenceColumn : parentIdentifier.toMap().keySet()) {
263-
263+
if (SqlIdentifier.EMPTY.equals(backReferenceColumn)){
264+
throw new UnsupportedOperationException(
265+
"An empty SqlIdentifier can't be used in condition. Make sure that all composite primary keys are defined in the query.");
266+
}
264267
Condition newCondition = table.column(backReferenceColumn).isEqualTo(getBindMarker(backReferenceColumn));
265268
condition = condition == null ? newCondition : condition.and(newCondition);
266269
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,17 @@ public void findAllByPropertyAvoidsDuplicateColumns() {
443443

444444
}
445445

446+
@Test // DATAJDBC-613
447+
void findAllByPropertyWithEmptyBackrefColumn() {
448+
449+
assertThatThrownBy(() -> {
450+
sqlGenerator.getFindAllByProperty(Identifier.of(EMPTY, 0, Object.class),
451+
unquoted("key-column"),
452+
false);
453+
}).isInstanceOf(UnsupportedOperationException.class)
454+
.hasMessageContaining("An empty SqlIdentifier can't be used in condition. Make sure that all composite primary keys are defined in the query.");
455+
}
456+
446457
@Test // DATAJDBC-219
447458
void updateWithVersion() {
448459

0 commit comments

Comments
 (0)