Skip to content

Fix loading of 2nd level collections #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.converter.Converter;
Expand All @@ -45,6 +44,7 @@
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.domain.RowDocument;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -366,15 +366,19 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
if (property.isCollectionLike() || property.isMap()) {

Identifier identifierToUse = this.identifier;
AggregatePath idDefiningParentPath = aggregatePath.getIdDefiningParentPath();

// note that the idDefiningParentPath might not itself have an id property, but have a combination of back
// references and possibly keys, that form an id
if (idDefiningParentPath.hasIdProperty()) {

if (property.getOwner().hasIdProperty()) {
Class<?> idType = idDefiningParentPath.getRequiredIdProperty().getActualType();
SqlIdentifier parentId = idDefiningParentPath.getTableInfo().idColumnName();
Object idValue = this.identifier.get(parentId);

Object id = this.identifier.get(property.getOwner().getRequiredIdProperty().getColumnName());
Assert.state(idValue != null, "idValue must not be null at this point");

if (id != null) {
identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), id,
Object.class);
}
identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), idValue, idType);
}

Iterable<Object> allByPath = relationResolver.findAllByPath(identifierToUse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public void savesAnEntity() throws SQLException {

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

assertThat(countRowsInTable("dummy_entity", entity.getId())).isEqualTo(1);
assertThat(countRowsInTable("dummy_entity2", entity.getId())).isEqualTo(2);
assertThat(countRowsInTable("dummy_entity", entity.getId(), "ID")).isEqualTo(1);
assertThat(countRowsInTable("dummy_entity2", entity.getId(), "DUMMY_ID")).isEqualTo(2);
}

private int countRowsInTable(String name, long idValue) {
private int countRowsInTable(String name, long idValue, String idColumnName) {

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

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

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

String test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CREATE TABLE dummy_entity
);
CREATE TABLE dummy_entity2
(
id BIGINT NOT NULL,
dummy_id BIGINT NOT NULL,
ORDER_KEY BIGINT NOT NULL,
TEST VARCHAR(100),
PRIMARY KEY (id, ORDER_KEY)
PRIMARY KEY (dummy_id, ORDER_KEY)
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CREATE TABLE dummy_entity
);
CREATE TABLE dummy_entity2
(
id BIGINT,
dummy_id BIGINT,
ORDER_KEY BIGINT,
TEST VARCHAR(100),
PRIMARY KEY (id, ORDER_KEY)
PRIMARY KEY (dummy_id, ORDER_KEY)
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CREATE TABLE dummy_entity
);
CREATE TABLE dummy_entity2
(
id BIGINT,
dummy_id BIGINT,
ORDER_KEY BIGINT,
TEST VARCHAR(100),
PRIMARY KEY (id, ORDER_KEY)
PRIMARY KEY (dummy_id, ORDER_KEY)
)
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY));
CREATE TABLE dummy_entity
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
TEST VARCHAR(100),
PREFIX_TEST VARCHAR(100)
);
CREATE TABLE dummy_entity2
(
dummy_id BIGINT,
ORDER_KEY BIGINT,
TEST VARCHAR(100),
PRIMARY KEY (dummy_id, ORDER_KEY)
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
DROP TABLE IF EXISTS dummy_entity;
CREATE TABLE dummy_entity (id BIGINT IDENTITY PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
CREATE TABLE dummy_entity
(
id BIGINT IDENTITY PRIMARY KEY,
TEST VARCHAR(100),
PREFIX_TEST VARCHAR(100)
);
DROP TABLE IF EXISTS dummy_entity2;
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), CONSTRAINT dummym_entity2_pk PRIMARY KEY(id, ORDER_KEY));
CREATE TABLE dummy_entity2
(
dummy_id BIGINT,
ORDER_KEY BIGINT,
TEST VARCHAR(100),
CONSTRAINT dummym_entity2_pk PRIMARY KEY (dummy_id, ORDER_KEY)
);
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100));
CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY));
CREATE TABLE dummy_entity
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
TEST VARCHAR(100),
PREFIX_TEST VARCHAR(100)
);
CREATE TABLE dummy_entity2
(
dummy_id BIGINT,
ORDER_KEY BIGINT,
TEST VARCHAR(100),
PRIMARY KEY (dummy_id, ORDER_KEY)
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ DROP TABLE DUMMY_ENTITY CASCADE CONSTRAINTS PURGE;

CREATE TABLE DUMMY_ENTITY
(
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
TEST VARCHAR2(100),
PREFIX_TEST VARCHAR2(100)
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
TEST VARCHAR2(100),
PREFIX_TEST VARCHAR2(100)
);

CREATE TABLE DUMMY_ENTITY2
(
ID NUMBER,
ORDER_KEY NUMBER,
TEST VARCHAR2(100),
PRIMARY KEY (ID, ORDER_KEY)
DUMMY_ID NUMBER,
ORDER_KEY NUMBER,
TEST VARCHAR2(100),
PRIMARY KEY (DUMMY_ID, ORDER_KEY)
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ CREATE TABLE dummy_entity
DROP TABLE dummy_entity2;
CREATE TABLE dummy_entity2
(
"ID" BIGINT,
"DUMMY_ID" BIGINT,
"ORDER_KEY" BIGINT,
TEST VARCHAR(100),
PRIMARY KEY ("ID", "ORDER_KEY")
PRIMARY KEY ("DUMMY_ID", "ORDER_KEY")
);
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1692-collection-in-embedded-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Loading