Skip to content

Commit 8b783d2

Browse files
schaudermp911de
authored andcommitted
Add the appropriate SQL type representation for floats in Postgres arrays.
Note that there is a separate problem with loading arrays of floats. Original pull request #1037 See #1046
1 parent 21d8330 commit 8b783d2

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/JdbcPostgresDialect.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public JdbcArrayColumns getArraySupport() {
3838
static class JdbcPostgresArrayColumns extends PostgresArrayColumns implements JdbcArrayColumns {
3939
@Override
4040
public String getSqlTypeRepresentation(JDBCType jdbcType) {
41-
return jdbcType == JDBCType.DOUBLE ? "FLOAT8" : jdbcType.getName();
41+
42+
if (jdbcType == JDBCType.DOUBLE) {
43+
return "FLOAT8";
44+
}
45+
if (jdbcType == JDBCType.REAL) {
46+
return "FLOAT4";
47+
}
48+
49+
return jdbcType.getName();
4250
}
4351
}
4452
}

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

+27
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,25 @@ public void saveAndLoadAnEntityWithListOfDouble() {
573573
assertThat(reloaded.digits).isEqualTo(Arrays.asList(1.2, 1.3, 1.4));
574574
}
575575

576+
@Test // GH-1033
577+
@EnabledOnFeature(SUPPORTS_ARRAYS)
578+
public void saveAndLoadAnEntityWithListOfFloat() {
579+
580+
FloatListOwner floatListOwner = new FloatListOwner();
581+
final List<Float> values = Arrays.asList(1.2f, 1.3f, 1.4f);
582+
floatListOwner.digits.addAll(values);
583+
584+
FloatListOwner saved = template.save(floatListOwner);
585+
586+
assertThat(saved.id).isNotNull();
587+
588+
FloatListOwner reloaded = template.findById(saved.id, FloatListOwner.class);
589+
590+
assertThat(reloaded).isNotNull();
591+
assertThat(reloaded.id).isEqualTo(saved.id);
592+
593+
}
594+
576595
@Test // DATAJDBC-259
577596
@EnabledOnFeature(SUPPORTS_ARRAYS)
578597
public void saveAndLoadAnEntityWithSet() {
@@ -929,6 +948,7 @@ private static class ListOwner {
929948

930949
List<String> digits = new ArrayList<>();
931950
}
951+
932952
@Table("ARRAY_OWNER")
933953
private static class SetOwner {
934954
@Id Long id;
@@ -943,6 +963,13 @@ private static class DoubleListOwner {
943963
List<Double> digits = new ArrayList<>();
944964
}
945965

966+
private static class FloatListOwner {
967+
968+
@Id Long id;
969+
970+
List<Float> digits = new ArrayList<>();
971+
}
972+
946973
@Data
947974
static class LegoSet {
948975

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

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ CREATE TABLE DOUBLE_LIST_OWNER
5858
DIGITS ARRAY[10]
5959
);
6060

61+
CREATE TABLE FLOAT_LIST_OWNER
62+
(
63+
ID SERIAL PRIMARY KEY,
64+
DIGITS ARRAY[10]
65+
);
66+
6167
CREATE TABLE CHAIN4
6268
(
6369
FOUR SERIAL PRIMARY KEY,

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

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ CREATE TABLE DOUBLE_LIST_OWNER
6060
DIGITS DOUBLE PRECISION ARRAY[10]
6161
);
6262

63+
CREATE TABLE FLOAT_LIST_OWNER
64+
(
65+
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
66+
DIGITS FLOAT ARRAY[10]
67+
);
68+
6369
CREATE TABLE CHAIN4
6470
(
6571
FOUR BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 40) PRIMARY KEY,

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

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ CREATE TABLE DOUBLE_LIST_OWNER
6767
DIGITS DOUBLE PRECISION[10]
6868
);
6969

70+
CREATE TABLE FLOAT_LIST_OWNER
71+
(
72+
ID SERIAL PRIMARY KEY,
73+
DIGITS FLOAT[10]
74+
);
75+
7076
CREATE TABLE BYTE_ARRAY_OWNER
7177
(
7278
ID SERIAL PRIMARY KEY,

0 commit comments

Comments
 (0)