如何通过 Spring Data JDBC 使用数组

ste*_*oss 5 spring-data-jdbc

我正在尝试将 Spring Data JDBC 用于我的 PostgreSQL 数据库。我定义了以下bean

@Data
class Report {

    @Id
    private Long id;

    private String name;

    private Set<Dimension> dimensions;

}

@Data
class Dimension {

    private String name;

    private Long[] filterIds;

}
Run Code Online (Sandbox Code Playgroud)

以及相应的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
);
Run Code Online (Sandbox Code Playgroud)

然后我尝试插入报告

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);
Run Code Online (Sandbox Code Playgroud)

其中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
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式告诉 Spring Data JDBC 如何映射数组类型吗?

P44*_*44T 5

随着 Spring Data JDBC 1.1.0 的发布,这成为可能。请参阅此处的文档:

目前支持以下类型的属性:

  • 所有基本类型及其装箱类型(int、float、Integer、Float 等)

  • 枚举被映射到他们的名字。

  • 细绳

  • java.util.Date、java.time.LocalDate、java.time.LocalDateTime 和 java.time.LocalTime

  • 如果您的数据库支持,则上述类型的数组和集合可以映射到数组类型的列。

  • ...