简体   繁体   中英

How to solve hibernate auto create issue on PostgreSQL with array field?

One of my entity includes an array field, I mapped using hibernate-types-52 library:

@TypeDef(name = "string-array", typeClass = StringArrayType.class)
@Table(name = "some_table")
public class SomeEntity implements Synchronizable {
    @Id
    @SequenceGenerator(name = "some_table_id", sequenceName = "some_table_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "some_table_id_seq")
    @Column(name = "id")
    private Long id;

    @Column(name = "values")
    @Type(type = "string-array")
    private String[] values;
}

application.yml:

spring:
  jpa:
    database: POSTGRESQL
    show-sql: true
    hibernate:
      ddl-auto: create
      show-sql: true
    properties:
      hibernate:
        dialect: my.package.PostgreSQL95ArrayDialect

PostgreSQL95ArrayDialect:

import org.hibernate.dialect.PostgreSQL95Dialect;

import java.sql.Types;

public class PostgreSQL95ArrayDialect
        extends PostgreSQL95Dialect {

    public PostgreSQL95ArrayDialect() {
        super();
        this.registerColumnType(Types.ARRAY, "array");
        this.registerColumnType(Types.OTHER, "jsonb");
    }
}

Issue:

o.h.t.s.i.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : 
GenerationTarget encountered exception accepting command : 
Error executing DDL "create table some_table (id int8 not null, values array)" via JDBC Statement ... 

So, SQL part "values array" looks very weird. How should I change hibernate options or my code to fix it?

You'd have to specify the columnDefinition for proper table DDL, like so:

@TypeDef(name = "string-array", typeClass = StringArrayType.class)
@Table(name = "some_table")
public class SomeEntity implements Synchronizable {
    @Id
    @SequenceGenerator(name = "some_table_id", sequenceName = "some_table_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "some_table_id_seq")
    @Column(name = "id")
    private Long id;

    @Column(name = "values")
    @Type(type = "string-array", columnDefinition = "text[]")
    private String[] values;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM