Skip to content

Commit 8a94345

Browse files
schaudermp911de
authored andcommitted
Fix access of root property instead of child property.
When the child of a one-to-one relationship has an id, the value for that id gets read in the wrong way. We get the column name for that id use that to access the value in the RowDocument. This results in either no value at all being found or even worse, the value of a root entity with a property of same name being accessed. This is fixed by using the full AggregatePath instead of just the property for accessing that value. Closes #1684 Original pull request: #1775
1 parent 30e4ddd commit 8a94345

11 files changed

+146
-8
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider dele
327327
this.accessor = accessor;
328328
this.context = context;
329329
this.identifier = path.isEntity()
330-
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(), delegate::getPropertyValue)
330+
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(), property -> delegate.getValue(path.append(property)))
331331
: identifier;
332332
}
333333

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

+14
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,16 @@ void mapWithEnumKey() {
12651265
assertThat(enumMapOwners).containsExactly(enumMapOwner);
12661266
}
12671267

1268+
@Test // GH-1684
1269+
void oneToOneWithIdenticalIdColumnName(){
1270+
1271+
WithOneToOne saved = template.insert(new WithOneToOne("one", new Referenced(23L)));
1272+
1273+
WithOneToOne reloaded = template.findById(saved.id, WithOneToOne.class);
1274+
1275+
assertThat(reloaded).isEqualTo(saved);
1276+
}
1277+
12681278
private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
12691279
Function<Number, T> toConcreteNumber) {
12701280
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
@@ -2086,6 +2096,10 @@ record Book(String name) {
20862096
record EnumMapOwner(@Id Long id, String name, Map<Color, MapElement> map) {
20872097
}
20882098

2099+
record WithOneToOne(@Id String id,@MappedCollection(idColumn = "renamed") Referenced referenced){}
2100+
2101+
record Referenced(@Id Long id) {
2102+
}
20892103

20902104
@Configuration
20912105
@Import(TestConfiguration.class)

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

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.time.ZonedDateTime;
3131
import java.util.Date;
3232
import java.util.List;
33+
import java.util.Map;
3334
import java.util.UUID;
3435

3536
import org.assertj.core.api.SoftAssertions;
@@ -39,8 +40,10 @@
3940
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4041
import org.springframework.data.jdbc.core.mapping.JdbcValue;
4142
import org.springframework.data.jdbc.support.JdbcUtil;
43+
import org.springframework.data.relational.core.mapping.MappedCollection;
4244
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
4345
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
46+
import org.springframework.data.relational.domain.RowDocument;
4447
import org.springframework.data.util.TypeInformation;
4548

4649
/**
@@ -139,6 +142,17 @@ void conversionOfPrimitiveArrays() {
139142
assertThat(typeFactory.arraySource).containsExactly(1, 2, 3, 4, 5);
140143
}
141144

145+
@Test // GH-1684
146+
void accessesCorrectValuesForOneToOneRelationshipWithIdenticallyNamedIdProperties() {
147+
148+
RowDocument rowdocument = new RowDocument(Map.of("ID", "one", "REFERENCED_ID", 23));
149+
150+
WithOneToOne result = converter.readAndResolve(WithOneToOne.class, rowdocument);
151+
152+
assertThat(result).isEqualTo(new WithOneToOne("one", new Referenced(23L)));
153+
}
154+
155+
142156
private void checkConversionToTimestampAndBack(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity,
143157
String propertyName, Object value) {
144158

@@ -284,4 +298,10 @@ public Array createArray(Object[] value) {
284298
return mock(Array.class);
285299
}
286300
}
301+
302+
record WithOneToOne(@Id String id,@MappedCollection(idColumn = "renamed") Referenced referenced){}
303+
304+
record Referenced(@Id Long id) {
305+
}
306+
287307
}

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ DROP TABLE AUTHOR;
5252

5353
DROP TABLE ENUM_MAP_OWNER;
5454

55+
DROP TABLE REFERENCED;
56+
DROP TABLE WITH_ONE_TO_ONE;
57+
58+
5559
CREATE TABLE LEGO_SET
5660
(
5761
"id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
@@ -428,4 +432,15 @@ CREATE TABLE ENUM_MAP_OWNER
428432
(
429433
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
430434
NAME VARCHAR(100)
431-
);
435+
);
436+
437+
CREATE TABLE WITH_ONE_TO_ONE
438+
(
439+
ID VARCHAR(100)
440+
);
441+
442+
CREATE TABLE REFERENCED
443+
(
444+
"renamed" VARCHAR(100),
445+
ID BIGINT
446+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql

+12-1
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,15 @@ CREATE TABLE ENUM_MAP_OWNER
385385
(
386386
ID SERIAL PRIMARY KEY,
387387
NAME VARCHAR(100)
388-
);
388+
);
389+
390+
CREATE TABLE WITH_ONE_TO_ONE
391+
(
392+
ID VARCHAR(100)
393+
);
394+
395+
CREATE TABLE REFERENCED
396+
(
397+
"renamed" VARCHAR(100),
398+
ID BIGINT
399+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql

+12
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,15 @@ CREATE TABLE ENUM_MAP_OWNER
387387
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
388388
NAME VARCHAR(100)
389389
);
390+
391+
392+
CREATE TABLE WITH_ONE_TO_ONE
393+
(
394+
ID VARCHAR(100)
395+
);
396+
397+
CREATE TABLE REFERENCED
398+
(
399+
"renamed" VARCHAR(100),
400+
ID BIGINT
401+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql

+12-1
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,15 @@ CREATE TABLE ENUM_MAP_OWNER
359359
(
360360
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
361361
NAME VARCHAR(100)
362-
);
362+
);
363+
364+
CREATE TABLE WITH_ONE_TO_ONE
365+
(
366+
ID VARCHAR(100)
367+
);
368+
369+
CREATE TABLE REFERENCED
370+
(
371+
`renamed` VARCHAR(100),
372+
ID BIGINT
373+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql

+16-1
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,19 @@ CREATE TABLE ENUM_MAP_OWNER
401401
(
402402
ID BIGINT IDENTITY PRIMARY KEY,
403403
NAME VARCHAR(100)
404-
);
404+
);
405+
406+
407+
DROP TABLE REFERENCED;
408+
DROP TABLE WITH_ONE_TO_ONE;
409+
410+
CREATE TABLE WITH_ONE_TO_ONE
411+
(
412+
ID VARCHAR(100)
413+
);
414+
415+
CREATE TABLE REFERENCED
416+
(
417+
"renamed" VARCHAR(100),
418+
ID BIGINT
419+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql

+13-1
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,16 @@ CREATE TABLE ENUM_MAP_OWNER
364364
(
365365
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
366366
NAME VARCHAR(100)
367-
);
367+
);
368+
369+
370+
CREATE TABLE WITH_ONE_TO_ONE
371+
(
372+
ID VARCHAR(100)
373+
);
374+
375+
CREATE TABLE REFERENCED
376+
(
377+
`renamed` VARCHAR(100),
378+
ID BIGINT
379+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-oracle.sql

+15-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ DROP TABLE AUTHOR CASCADE CONSTRAINTS PURGE;
4242

4343
DROP TABLE ENUM_MAP_OWNER CASCADE CONSTRAINTS PURGE;
4444

45+
DROP TABLE REFERENCED CASCADE CONSTRAINTS PURGE;
46+
DROP TABLE WITH_ONE_TO_ONE CASCADE CONSTRAINTS PURGE;
47+
4548
CREATE TABLE LEGO_SET
4649
(
4750
"id1" NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY,
@@ -409,4 +412,15 @@ CREATE TABLE ENUM_MAP_OWNER
409412
(
410413
ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
411414
NAME VARCHAR(100)
412-
);
415+
);
416+
417+
CREATE TABLE WITH_ONE_TO_ONE
418+
(
419+
ID VARCHAR(100)
420+
);
421+
422+
CREATE TABLE REFERENCED
423+
(
424+
"renamed" VARCHAR(100),
425+
ID NUMBER
426+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-postgres.sql

+15-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ DROP TABLE AUTHOR;
4545

4646
DROP TABLE ENUM_MAP_OWNER;
4747

48+
DROP TABLE REFERENCED;
49+
DROP TABLE WITH_ONE_TO_ONE;
50+
4851
CREATE TABLE LEGO_SET
4952
(
5053
"id1" SERIAL PRIMARY KEY,
@@ -431,4 +434,15 @@ CREATE TABLE ENUM_MAP_OWNER
431434
(
432435
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
433436
NAME VARCHAR(100)
434-
);
437+
);
438+
439+
CREATE TABLE WITH_ONE_TO_ONE
440+
(
441+
ID VARCHAR(100)
442+
);
443+
444+
CREATE TABLE REFERENCED
445+
(
446+
"renamed" VARCHAR(100),
447+
ID BIGINT
448+
);

0 commit comments

Comments
 (0)