如何将数组与 Spring 数据一起使用 JDBC
How to use arrays with Spring Data JDBC
我正在尝试为我的 PostgreSQL 数据库使用 Spring 数据 JDBC。我定义了以下 beans
@Data
class Report {
@Id
private Long id;
private String name;
private Set<Dimension> dimensions;
}
@Data
class Dimension {
private String name;
private Long[] filterIds;
}
和对应的DDL
CREATE TABLE report (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE dimension (
id bigserial PRIMARY KEY ,
report bigint,
name text,
filter_ids bigint[],
FOREIGN KEY (report) REFERENCES report(id) ON DELETE CASCADE ON UPDATE CASCADE
);
然后我尝试插入一个报告
final Dimension dimension = new Dimension();
dimension.setName("xyz");
dimension.setFilterIds(new Long[]{ 1L, 2L, 3L });
final Report report = new Report();
report.setName("xyz");
report.setDimensions(Collections.singleton(dimension));
repository.save(report);
其中 repository
只是一个 CrudRepository<Report, Long>
.
这给了我以下错误
org.postgresql.util.PSQLException: ERROR: column "filter_ids" is of type bigint[] but expression is of type bigint
Hinweis: You will need to rewrite or cast the expression.
Position: 116
我能以某种方式告诉 Spring 数据 JDBC 如何映射数组类型吗?
因为 这应该从 Spring 数据 JDBC 的 1.1 版本开始工作,就像您使用它一样。
原回答
目前无法。这有问题。起点是这个:https://jira.spring.io/browse/DATAJDBC-259
随着 Spring Data JDBC 1.1.0 的发布,这成为可能。请参阅文档 here:
The properties of the following types are currently supported:
All primitive types and their boxed types (int, float, Integer, Float, and so on)
Enums get mapped to their name.
String
java.util.Date, java.time.LocalDate, java.time.LocalDateTime, and java.time.LocalTime
Arrays and Collections of the types mentioned above can be mapped to columns of array type if your database supports that.
...
我正在尝试为我的 PostgreSQL 数据库使用 Spring 数据 JDBC。我定义了以下 beans
@Data
class Report {
@Id
private Long id;
private String name;
private Set<Dimension> dimensions;
}
@Data
class Dimension {
private String name;
private Long[] filterIds;
}
和对应的DDL
CREATE TABLE report (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE dimension (
id bigserial PRIMARY KEY ,
report bigint,
name text,
filter_ids bigint[],
FOREIGN KEY (report) REFERENCES report(id) ON DELETE CASCADE ON UPDATE CASCADE
);
然后我尝试插入一个报告
final Dimension dimension = new Dimension();
dimension.setName("xyz");
dimension.setFilterIds(new Long[]{ 1L, 2L, 3L });
final Report report = new Report();
report.setName("xyz");
report.setDimensions(Collections.singleton(dimension));
repository.save(report);
其中 repository
只是一个 CrudRepository<Report, Long>
.
这给了我以下错误
org.postgresql.util.PSQLException: ERROR: column "filter_ids" is of type bigint[] but expression is of type bigint
Hinweis: You will need to rewrite or cast the expression.
Position: 116
我能以某种方式告诉 Spring 数据 JDBC 如何映射数组类型吗?
因为
原回答
目前无法。这有问题。起点是这个:https://jira.spring.io/browse/DATAJDBC-259
随着 Spring Data JDBC 1.1.0 的发布,这成为可能。请参阅文档 here:
The properties of the following types are currently supported:
All primitive types and their boxed types (int, float, Integer, Float, and so on)
Enums get mapped to their name.
String
java.util.Date, java.time.LocalDate, java.time.LocalDateTime, and java.time.LocalTime
Arrays and Collections of the types mentioned above can be mapped to columns of array type if your database supports that.
...