代码之家  ›  专栏  ›  技术社区  ›  stevecross

如何将数组与Spring数据JDBC一起使用

  •  1
  • stevecross  · 技术社区  · 7 年前

    我正在尝试将SpringDataJDBC用于我的PostgreSQL数据库。我定义了以下豆子

    @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
    

    我能告诉SpringDataJDBC如何映射数组类型吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jens Schauder    7 年前

    目前不可能。这是有问题的。一个起点是: https://jira.spring.io/browse/DATAJDBC-259

    推荐文章