在springboot 1.5-> 2.0迁移期间,Postgresql枚举崩溃了

时间:2018-05-04 15:30:40

标签: java spring postgresql hibernate spring-boot

我正在尝试migrate a project from springboot 1.5 to 2.0。 到目前为止,我所做的只是rename some crud repo methods并更新我的gradle构建文件。现在,在运行我的应用程序时,我在尝试保存到数据库时收到此错误:

| 2018-05-02 16:39:10.581 -DEBUG 20748 [Scheduler-1] org.hibernate.SQL              : insert into table_a (enum_column) values (?)
| 2018-05-02 16:39:10.581 -TRACE 20748 [Scheduler-1] org.hibernate.type.EnumType    : Binding [OPTIONA] to parameter: [1]
| 2018-05-02 16:39:10.620 - WARN 20748 [Scheduler-1] ne.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42804
| 2018-05-02 16:39:10.634 -ERROR 20748 [Scheduler-1] ne.jdbc.spi.SqlExceptionHelper : ERROR: column "enum_column" is of type custom_enum but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.
  Position: 121

在我尝试迁移到较新的springboot版本之前,一切正常。我还没有在迁移指南中看到任何有关此问题的内容。是否有人知道会导致此问题的其中一个(或其他常见软件包)的更改?

  • spring-core-4.3.12 - > 5.0.5
  • spring-data-jpa-1.11.8 - > 2.0.6
  • spring-boot-starter-data-jpa-1.5.8 - > 2.0.1
  • flyway-core-4.2.0 - > 5.0.7
  • 的PostgreSQL-42.2.2
  • hibernate-core-5.0.12 - > 5.2.16

以下是相关枚举代码的精简版本:

从我的模型中,使用JPA注释

@Entity
@Table(name = "table_a")
public class CustomTable {
    @Enumerated(EnumType.STRING)
    @Column(name = "enum_column", nullable = false)
    private CustomEnum enumField;

Enum声明

public enum CustomEnum {
    OPTIONA, OPTIONB
}

Flyway数据库迁移

CREATE TYPE custom_enum AS ENUM('OPTIONA', 'OPTIONB'); 
CREATE TABLE table_a (
    id                  bigserial PRIMARY KEY,
    enum_column         custom_enum NOT NULL
);

如果您需要其他软件包版本号或其他代码段,请告诉我们。谢谢!

1 个答案:

答案 0 :(得分:3)

在进一步挖掘之后,我意识到这不仅仅是我的枚举类型存在问题,jsonb类型的列也存在类似的问题。 这使我开始研究更广泛的数据库问题,而且我的application.properties中的以下属性确实不再有效:

spring.datasource.tomcat.connection-properties = stringtype=unspecified

这是有道理的,因为我使用的是默认的JDBC连接池,它在SpringBoot 2.0中从tomcat更改为hikari。 要解决这个问题,我所做的就是将tomcat连接属性行替换为:

spring.datasource.hikari.data-source-properties = stringtype=unspecified