【问题标题】:Postgresql enum broke during springboot 1.5->2.0 migration在 springboot 1.5->2.0 迁移期间 Postgresql 枚举中断
【发布时间】:2018-05-04 15:30:40
【问题描述】:

我正在尝试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;

枚举声明

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
);

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

【问题讨论】:

标签: java spring postgresql hibernate spring-boot


【解决方案1】:

进一步挖掘后,我意识到不仅仅是我的枚举类型有问题,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

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2010-10-16
    • 1970-01-01
    • 2020-04-18
    • 2018-10-18
    相关资源
    最近更新 更多