Skip to content

Commit b22ccad

Browse files
committed
Fix loading of 2nd level collections.
Construction of the back reference assumed that the table holding the parent of the foreign key is the actual parent property. This is now corrected by using the correct API to identify the ancestor which holds the id. Closes: #1692 Original pull request: #1773
1 parent 65bf64e commit b22ccad

10 files changed

+71
-32
lines changed

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
4545
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
4646
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
47+
import org.springframework.data.relational.core.sql.SqlIdentifier;
4748
import org.springframework.data.relational.domain.RowDocument;
4849
import org.springframework.data.util.TypeInformation;
4950
import org.springframework.lang.Nullable;
@@ -365,15 +366,19 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
365366
if (property.isCollectionLike() || property.isMap()) {
366367

367368
Identifier identifierToUse = this.identifier;
369+
AggregatePath idDefiningParentPath = aggregatePath.getIdDefiningParentPath();
368370

369-
if (property.getOwner().hasIdProperty()) {
371+
// note that the idDefiningParentPath might not itself have an id property, but have a combination of back
372+
// references and possibly keys, that form an id
373+
if (idDefiningParentPath.hasIdProperty()) {
370374

371-
Object id = this.identifier.get(property.getOwner().getRequiredIdProperty().getColumnName());
375+
Class<?> idType = idDefiningParentPath.getRequiredIdProperty().getActualType();
376+
SqlIdentifier parentId = idDefiningParentPath.getTableInfo().idColumnName();
377+
Object idValue = this.identifier.get(parentId);
372378

373-
if (id != null) {
374-
identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), id,
375-
Object.class);
376-
}
379+
Assert.state(idValue != null, "idValue must not be null at this point");
380+
381+
identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), idValue, idType);
377382
}
378383

379384
Iterable<Object> allByPath = relationResolver.findAllByPath(identifierToUse,

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public void savesAnEntity() throws SQLException {
6969

7070
DummyEntity entity = repository.save(createDummyEntity());
7171

72-
assertThat(countRowsInTable("dummy_entity", entity.getId())).isEqualTo(1);
73-
assertThat(countRowsInTable("dummy_entity2", entity.getId())).isEqualTo(2);
72+
assertThat(countRowsInTable("dummy_entity", entity.getId(), "ID")).isEqualTo(1);
73+
assertThat(countRowsInTable("dummy_entity2", entity.getId(), "DUMMY_ID")).isEqualTo(2);
7474
}
7575

76-
private int countRowsInTable(String name, long idValue) {
76+
private int countRowsInTable(String name, long idValue, String idColumnName) {
7777

78-
SqlIdentifier id = SqlIdentifier.quoted("ID");
78+
SqlIdentifier id = SqlIdentifier.quoted(idColumnName);
7979
String whereClause = id.toSql(dialect.getIdentifierProcessing()) + " = " + idValue;
8080

8181
return JdbcTestUtils.countRowsInTableWhere(template.getJdbcOperations(), name, whereClause);
@@ -273,7 +273,8 @@ public void setEmbeddable(Embeddable embeddable) {
273273
}
274274

275275
private static class Embeddable {
276-
@MappedCollection(idColumn = "ID", keyColumn = "ORDER_KEY") List<DummyEntity2> list = new ArrayList<>();
276+
@MappedCollection(idColumn = "DUMMY_ID", keyColumn = "ORDER_KEY")
277+
List<DummyEntity2> list = new ArrayList<>();
277278

278279
String test;
279280

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-db2.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ CREATE TABLE dummy_entity
99
);
1010
CREATE TABLE dummy_entity2
1111
(
12-
id BIGINT NOT NULL,
12+
dummy_id BIGINT NOT NULL,
1313
ORDER_KEY BIGINT NOT NULL,
1414
TEST VARCHAR(100),
15-
PRIMARY KEY (id, ORDER_KEY)
15+
PRIMARY KEY (dummy_id, ORDER_KEY)
1616
)

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-h2.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CREATE TABLE dummy_entity
66
);
77
CREATE TABLE dummy_entity2
88
(
9-
id BIGINT,
9+
dummy_id BIGINT,
1010
ORDER_KEY BIGINT,
1111
TEST VARCHAR(100),
12-
PRIMARY KEY (id, ORDER_KEY)
12+
PRIMARY KEY (dummy_id, ORDER_KEY)
1313
)

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-hsql.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CREATE TABLE dummy_entity
66
);
77
CREATE TABLE dummy_entity2
88
(
9-
id BIGINT,
9+
dummy_id BIGINT,
1010
ORDER_KEY BIGINT,
1111
TEST VARCHAR(100),
12-
PRIMARY KEY (id, ORDER_KEY)
12+
PRIMARY KEY (dummy_id, ORDER_KEY)
1313
)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
2-
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY));
1+
CREATE TABLE dummy_entity
2+
(
3+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
4+
TEST VARCHAR(100),
5+
PREFIX_TEST VARCHAR(100)
6+
);
7+
CREATE TABLE dummy_entity2
8+
(
9+
dummy_id BIGINT,
10+
ORDER_KEY BIGINT,
11+
TEST VARCHAR(100),
12+
PRIMARY KEY (dummy_id, ORDER_KEY)
13+
);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
DROP TABLE IF EXISTS dummy_entity;
2-
CREATE TABLE dummy_entity (id BIGINT IDENTITY PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
2+
CREATE TABLE dummy_entity
3+
(
4+
id BIGINT IDENTITY PRIMARY KEY,
5+
TEST VARCHAR(100),
6+
PREFIX_TEST VARCHAR(100)
7+
);
38
DROP TABLE IF EXISTS dummy_entity2;
4-
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), CONSTRAINT dummym_entity2_pk PRIMARY KEY(id, ORDER_KEY));
9+
CREATE TABLE dummy_entity2
10+
(
11+
dummy_id BIGINT,
12+
ORDER_KEY BIGINT,
13+
TEST VARCHAR(100),
14+
CONSTRAINT dummym_entity2_pk PRIMARY KEY (dummy_id, ORDER_KEY)
15+
);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
2-
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY));
1+
CREATE TABLE dummy_entity
2+
(
3+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
4+
TEST VARCHAR(100),
5+
PREFIX_TEST VARCHAR(100)
6+
);
7+
CREATE TABLE dummy_entity2
8+
(
9+
dummy_id BIGINT,
10+
ORDER_KEY BIGINT,
11+
TEST VARCHAR(100),
12+
PRIMARY KEY (dummy_id, ORDER_KEY)
13+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-oracle.sql

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ DROP TABLE DUMMY_ENTITY CASCADE CONSTRAINTS PURGE;
33

44
CREATE TABLE DUMMY_ENTITY
55
(
6-
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
7-
TEST VARCHAR2(100),
8-
PREFIX_TEST VARCHAR2(100)
6+
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
7+
TEST VARCHAR2(100),
8+
PREFIX_TEST VARCHAR2(100)
99
);
1010

1111
CREATE TABLE DUMMY_ENTITY2
1212
(
13-
ID NUMBER,
14-
ORDER_KEY NUMBER,
15-
TEST VARCHAR2(100),
16-
PRIMARY KEY (ID, ORDER_KEY)
13+
DUMMY_ID NUMBER,
14+
ORDER_KEY NUMBER,
15+
TEST VARCHAR2(100),
16+
PRIMARY KEY (DUMMY_ID, ORDER_KEY)
1717
)

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-postgres.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ CREATE TABLE dummy_entity
88
DROP TABLE dummy_entity2;
99
CREATE TABLE dummy_entity2
1010
(
11-
"ID" BIGINT,
11+
"DUMMY_ID" BIGINT,
1212
"ORDER_KEY" BIGINT,
1313
TEST VARCHAR(100),
14-
PRIMARY KEY ("ID", "ORDER_KEY")
14+
PRIMARY KEY ("DUMMY_ID", "ORDER_KEY")
1515
);

0 commit comments

Comments
 (0)