Spring Boot 3+Hibernate 6.5迁移后,PostgreSQL枚举数组字段无法保存的问题求助
Spring Boot 3+Hibernate 6.5迁移后,PostgreSQL枚举数组字段无法保存的问题求助
大家好,我最近把项目迁移到了Hibernate 6.5.3.Final,现在遇到了PostgreSQL数据库里枚举数组字段无法写入的问题,想请教下各位怎么解决。
我的配置和代码情况:
- 实体类字段
一开始我是这么定义的:
@Nullable @Column(name = "test_col", columnDefinition = "test_enum_type[]") @Enumerated(EnumType.STRING) @JdbcTypeCode(SqlTypes.NAMED_ENUM) private TestEnum[] testEnum = new TestEnum[0];
- 枚举类
public enum TestEnum { AAA("fggh"), BBB("dffg"), CCC("dffg"); private final String value; TestEnum(String value) { this.value = value; } }
- 数据库结构
create type test_enum_type as enum ('AA', 'BB', 'VV'); create table test_table ( id integer default nextval('seq'::regclass) not null primary key, test_col test_enum_type[] );
- 设置值的方法
objTest.setReceivedData( new ReceivedDataBuilder() .testEnum(new TestEnum[] {TestEnum.AAA, TestEnum.BBB}) .build() );
遇到的错误:
- 用
@JdbcTypeCode(SqlTypes.NAMED_ENUM)的时候,报错:Cannot read the array length because "values" is null - 改成
@JdbcTypeCode(SqlTypes.ARRAY)之后,又报错:
Caused by: org.postgresql.util.PSQLException: ERROR: column 'test_col' is of type test_enum_type[] and expression is of character varying[] Hint: Rewrite the expression or convert its type.
我尝试找过相关的讨论,但还没找到有效的解决办法,有没有大佬知道怎么正确配置才能把枚举数组的值保存到PostgreSQL里?
备注:内容来源于stack exchange,提问作者Mike Sonarov
相关产品推荐
相关产品推荐

