Skip to content

Fix usage of wrong id value in related selects #1810

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.4.0-SNAPSHOT</version>
<version>3.4.0-1802-set-chain-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.4.0-SNAPSHOT</version>
<version>3.4.0-1802-set-chain-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.4.0-SNAPSHOT</version>
<version>3.4.0-1802-set-chain-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.4.0-SNAPSHOT</version>
<version>3.4.0-1802-set-chain-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
if (idDefiningParentPath.hasIdProperty()) {

Class<?> idType = idDefiningParentPath.getRequiredIdProperty().getActualType();
SqlIdentifier parentId = idDefiningParentPath.getTableInfo().idColumnName();
Object idValue = this.identifier.get(parentId);
//
RelationalPersistentProperty requiredIdProperty = idDefiningParentPath.getRequiredIdProperty();
AggregatePath idPath = idDefiningParentPath.append(requiredIdProperty);
Object idValue = delegate.getValue(idPath);

Assert.state(idValue != null, "idValue must not be null at this point");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,12 @@
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;

import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.IntStream;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -919,7 +912,7 @@ void readOnlyGetsLoadedButNotWritten() {

assertThat(
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class))
.isEqualTo("from-db");
.isEqualTo("from-db");
}

@Test
Expand Down Expand Up @@ -1258,15 +1251,16 @@ void recordOfSet() {
@Test // GH-1656
void mapWithEnumKey() {

EnumMapOwner enumMapOwner = template.save(new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));
EnumMapOwner enumMapOwner = template
.save(new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));

Iterable<EnumMapOwner> enumMapOwners = template.findAll(EnumMapOwner.class);

assertThat(enumMapOwners).containsExactly(enumMapOwner);
}

@Test // GH-1684
void oneToOneWithIdenticalIdColumnName(){
void oneToOneWithIdenticalIdColumnName() {

WithOneToOne saved = template.insert(new WithOneToOne("one", new Referenced(23L)));

Expand All @@ -1275,6 +1269,37 @@ void oneToOneWithIdenticalIdColumnName(){
assertThat(reloaded).isEqualTo(saved);
}

@Test // GH-1802
void singleEntitySetChain() {

First first1 = template.insert( //
new First(1L, "first-1", //
new Sec(2L, "second-1-2", Set.of( //
new Third("third-1-2-0"), //
new Third("third-1-2-1"), //
new Third("third-1-2-3")) //
) //
) //
);
First first2 = template.insert( //
new First(2L, "first-2", //
new Sec(3L, "second-2-3", Set.of( //
new Third("third-2-3-0"), //
new Third("third-2-3-1"), //
new Third("third-2-3-3")) //
) //
) //
);

First first1Reloaded = template.findById(first1.id, First.class);
First first2Reloaded = template.findById(first2.id, First.class);

SoftAssertions.assertSoftly(softly ->{
softly.assertThat(first1Reloaded).isEqualTo(first1);
softly.assertThat(first2Reloaded).isEqualTo(first2);
});
}

private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
Function<Number, T> toConcreteNumber) {
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
Expand Down Expand Up @@ -2096,11 +2121,21 @@ record Book(String name) {
record EnumMapOwner(@Id Long id, String name, Map<Color, MapElement> map) {
}

record WithOneToOne(@Id String id,@MappedCollection(idColumn = "renamed") Referenced referenced){}
record WithOneToOne(@Id String id, @MappedCollection(idColumn = "renamed") Referenced referenced) {
}

record Referenced(@Id Long id) {
}

record First(@Id Long id, String name, Sec sec) {
}

record Sec(@Id Long id, String name, Set<Third> thirds) {
}

record Third(String name) {
}

@Configuration
@Import(TestConfiguration.class)
static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ DROP TABLE ENUM_MAP_OWNER;
DROP TABLE REFERENCED;
DROP TABLE WITH_ONE_TO_ONE;

DROP TABLE THIRD;
DROP TABLE SEC;
DROP TABLE FIRST;

CREATE TABLE LEGO_SET
(
Expand Down Expand Up @@ -444,3 +447,24 @@ CREATE TABLE REFERENCED
"renamed" VARCHAR(100),
ID BIGINT
);

CREATE TABLE FIRST
(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(20) NOT NULL
);

CREATE TABLE SEC
(
ID BIGINT NOT NULL PRIMARY KEY,
FIRST BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (FIRST) REFERENCES FIRST (ID)
);

CREATE TABLE THIRD
(
SEC BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (SEC) REFERENCES SEC (ID)
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ CREATE TABLE LEGO_SET
);
CREATE TABLE MANUAL
(
"id2" SERIAL PRIMARY KEY,
LEGO_SET BIGINT,
"id2" SERIAL PRIMARY KEY,
LEGO_SET BIGINT,
"alternative" BIGINT,
CONTENT VARCHAR(2000)
CONTENT VARCHAR(2000)
);

ALTER TABLE MANUAL
Expand All @@ -34,17 +34,17 @@ CREATE TABLE LIST_PARENT

CREATE TABLE SIMPLE_LIST_PARENT
(
ID SERIAL PRIMARY KEY,
NAME VARCHAR(100)
ID SERIAL PRIMARY KEY,
NAME VARCHAR(100)
);

CREATE TABLE element_no_id
(
content VARCHAR(100),
content VARCHAR(100),
SIMPLE_LIST_PARENT_key BIGINT,
SIMPLE_LIST_PARENT INTEGER,
LIST_PARENT_key BIGINT,
LIST_PARENT INTEGER
LIST_PARENT_key BIGINT,
LIST_PARENT INTEGER
);

CREATE TABLE "ARRAY_OWNER"
Expand All @@ -62,14 +62,14 @@ CREATE TABLE BYTE_ARRAY_OWNER

CREATE TABLE DOUBLE_LIST_OWNER
(
ID SERIAL PRIMARY KEY,
DIGITS DOUBLE ARRAY[10]
ID SERIAL PRIMARY KEY,
DIGITS DOUBLE ARRAY[10]
);

CREATE TABLE FLOAT_LIST_OWNER
(
ID SERIAL PRIMARY KEY,
DIGITS FLOAT ARRAY[10]
ID SERIAL PRIMARY KEY,
DIGITS FLOAT ARRAY[10]
);

CREATE TABLE CHAIN4
Expand Down Expand Up @@ -338,36 +338,36 @@ CREATE TABLE WITH_ID_ONLY

CREATE TABLE WITH_INSERT_ONLY
(
ID SERIAL PRIMARY KEY,
ID SERIAL PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);

CREATE TABLE MULTIPLE_COLLECTIONS
(
ID SERIAL PRIMARY KEY,
ID SERIAL PRIMARY KEY,
NAME VARCHAR(100)
);

CREATE TABLE SET_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
NAME VARCHAR(100)
NAME VARCHAR(100)
);

CREATE TABLE LIST_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY INT,
NAME VARCHAR(100)
NAME VARCHAR(100)
);

CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

CREATE TABLE AUTHOR
Expand All @@ -378,12 +378,12 @@ CREATE TABLE AUTHOR
CREATE TABLE BOOK
(
AUTHOR BIGINT,
NAME VARCHAR(100)
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID SERIAL PRIMARY KEY,
ID SERIAL PRIMARY KEY,
NAME VARCHAR(100)
);

Expand All @@ -395,5 +395,26 @@ CREATE TABLE WITH_ONE_TO_ONE
CREATE TABLE REFERENCED
(
"renamed" VARCHAR(100),
ID BIGINT
ID BIGINT
);

CREATE TABLE FIRST
(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(20) NOT NULL
);

CREATE TABLE SEC
(
ID BIGINT NOT NULL PRIMARY KEY,
FIRST BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (FIRST) REFERENCES FIRST (ID)
);

CREATE TABLE THIRD
(
SEC BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (SEC) REFERENCES SEC (ID)
);
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,24 @@ CREATE TABLE REFERENCED
"renamed" VARCHAR(100),
ID BIGINT
);

CREATE TABLE FIRST
(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(20) NOT NULL
);

CREATE TABLE SEC
(
ID BIGINT NOT NULL PRIMARY KEY,
FIRST BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (FIRST) REFERENCES FIRST (ID)
);

CREATE TABLE THIRD
(
SEC BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (SEC) REFERENCES SEC (ID)
);
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,24 @@ CREATE TABLE REFERENCED
`renamed` VARCHAR(100),
ID BIGINT
);

CREATE TABLE FIRST
(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(20) NOT NULL
);

CREATE TABLE SEC
(
ID BIGINT NOT NULL PRIMARY KEY,
FIRST BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (FIRST) REFERENCES FIRST (ID)
);

CREATE TABLE THIRD
(
SEC BIGINT NOT NULL,
NAME VARCHAR(20) NOT NULL,
FOREIGN KEY (SEC) REFERENCES SEC (ID)
);
Loading
Loading