Skip to content

Properly convert Map keys #1663

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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private Object getIdFrom(DbAction.WithEntity<?> idOwningAction) {
RelationalPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(idOwningAction.getEntityType());
Object identifier = persistentEntity.getIdentifierAccessor(idOwningAction.getEntity()).getIdentifier();

Assert.state(identifier != null, "Couldn't obtain a required id value");
Assert.state(identifier != null,() -> "Couldn't obtain a required id value for " + persistentEntity);

return identifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.domain.RowDocument;
import org.springframework.data.util.TypeInformation;
import org.springframework.jdbc.core.RowMapper;

/**
Expand Down Expand Up @@ -51,15 +52,20 @@ class MapEntityRowMapper<T> implements RowMapper<Map.Entry<Object, T>> {
@Override
public Map.Entry<Object, T> mapRow(ResultSet rs, int rowNum) throws SQLException {

Object key = rs.getObject(keyColumn.getReference());
return new HashMap.SimpleEntry<>(key, mapEntity(rs, key));
RowDocument document = RowDocumentResultSetExtractor.toRowDocument(rs);

Object key = document.get(keyColumn.getReference());

Class<?> qualifierColumnType = path.getRequiredLeafProperty().getQualifierColumnType();
Object convertedKey = converter.readValue(key, TypeInformation.of(qualifierColumnType));

return new HashMap.SimpleEntry<>(convertedKey, mapEntity(document, key));
}

@SuppressWarnings("unchecked")
private T mapEntity(ResultSet resultSet, Object key) throws SQLException {
private T mapEntity(RowDocument document, Object key) {

RowDocument document = RowDocumentResultSetExtractor.toRowDocument(resultSet);
return (T) converter.readAndResolve(path.getLeafEntity().getType(), document,
return (T) converter.readAndResolve(path.getRequiredLeafEntity().getType(), document,
identifier.withPart(keyColumn, key, Object.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,16 @@ void recordOfSet() {
assertThat(authors).containsExactly(tolkien);
}

@Test // GH-1656
void mapWithEnumKey() {

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);
}

private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
Function<Number, T> toConcreteNumber) {
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
Expand Down Expand Up @@ -2096,6 +2106,10 @@ record Book(String name) {

}

record EnumMapOwner(@Id Long id, String name, Map<Color, MapElement> map) {
}


@Configuration
@Import(TestConfiguration.class)
static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ DROP TABLE SET_ELEMENT;
DROP TABLE BOOK;
DROP TABLE AUTHOR;

DROP TABLE ENUM_MAP_OWNER;

CREATE TABLE LEGO_SET
(
"id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
Expand Down Expand Up @@ -406,6 +408,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -419,3 +423,9 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

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

CREATE TABLE ENUM_MAP_OWNER
(
ID SERIAL PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -379,3 +381,9 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -352,3 +354,9 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -393,3 +395,10 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

DROP TABLE ENUM_MAP_OWNER;
CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT IDENTITY PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -357,3 +359,9 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ DROP TABLE SET_ELEMENT CASCADE CONSTRAINTS PURGE;
DROP TABLE BOOK CASCADE CONSTRAINTS PURGE;
DROP TABLE AUTHOR CASCADE CONSTRAINTS PURGE;

DROP TABLE ENUM_MAP_OWNER CASCADE CONSTRAINTS PURGE;

CREATE TABLE LEGO_SET
(
"id1" NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY,
Expand Down Expand Up @@ -387,6 +389,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS NUMBER,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -400,3 +404,9 @@ CREATE TABLE BOOK
AUTHOR NUMBER,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
NAME VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ DROP TABLE SET_ELEMENT;
DROP TABLE BOOK;
DROP TABLE AUTHOR;

DROP TABLE ENUM_MAP_OWNER;

CREATE TABLE LEGO_SET
(
"id1" SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -409,6 +411,8 @@ CREATE TABLE MAP_ELEMENT
(
MULTIPLE_COLLECTIONS BIGINT,
MULTIPLE_COLLECTIONS_KEY VARCHAR(10),
ENUM_MAP_OWNER BIGINT,
ENUM_MAP_OWNER_KEY VARCHAR(10),
NAME VARCHAR(100)
);

Expand All @@ -422,3 +426,9 @@ CREATE TABLE BOOK
AUTHOR BIGINT,
NAME VARCHAR(100)
);

CREATE TABLE ENUM_MAP_OWNER
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
NAME VARCHAR(100)
);
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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-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.2.0-SNAPSHOT</version>
<version>3.2.0-1656-enum-as-map-key-SNAPSHOT</version>
</parent>

<properties>
Expand Down