【发布时间】: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,请告诉我。谢谢!
【问题讨论】:
-
我相信我在开始之前正在运行 flyway-core-4.2.0,因此版本 3 的步骤已经完成。
标签: java spring postgresql hibernate spring-boot