You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3+Hibernate 6.5迁移后,PostgreSQL枚举数组字段无法保存的问题求助

Spring Boot 3+Hibernate 6.5迁移后,PostgreSQL枚举数组字段无法保存的问题求助

大家好,我最近把项目迁移到了Hibernate 6.5.3.Final,现在遇到了PostgreSQL数据库里枚举数组字段无法写入的问题,想请教下各位怎么解决。

我的配置和代码情况:

  1. 实体类字段
    一开始我是这么定义的:
@Nullable
@Column(name = "test_col", columnDefinition = "test_enum_type[]")
@Enumerated(EnumType.STRING)
@JdbcTypeCode(SqlTypes.NAMED_ENUM)

private TestEnum[] testEnum = new TestEnum[0];
  1. 枚举类
public enum TestEnum  {
    AAA("fggh"),
    BBB("dffg"),
    CCC("dffg");

    private final String value;

    TestEnum(String value) {
        this.value = value;
    }
}
  1. 数据库结构
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[]
);
  1. 设置值的方法
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:23:13