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

SpringBoot 3.x迁移后Hibernate中JsonNode映射失败求助

问题:Spring Boot 3.x(Hibernate 6.x)中正确注册JsonNode类型读取PostgreSQL jsonb数据

在Hibernate 5.x中,我曾通过自定义PostgreSQLJsonDialect注册Hibernate类型,代码如下:

import java.sql.Types;

import org.hibernate.dialect.PostgreSQL10Dialect;

import io.hypersistence.utils.hibernate.type.array.IntArrayType;
import io.hypersistence.utils.hibernate.type.array.StringArrayType;
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
import io.hypersistence.utils.hibernate.type.json.JsonNodeBinaryType;
import io.hypersistence.utils.hibernate.type.json.JsonNodeStringType;
import io.hypersistence.utils.hibernate.type.json.JsonStringType;

public class PostgreSQLJsonDialect extends PostgreSQL10Dialect {
    public PostgreSQLJsonDialect() {
        super();
        this.registerHibernateType(Types.OTHER, StringArrayType.class.getName());
        this.registerHibernateType(Types.OTHER, IntArrayType.class.getName());
        this.registerHibernateType(Types.OTHER, JsonStringType.class.getName());
        this.registerHibernateType(Types.OTHER, JsonBinaryType.class.getName());
        this.registerHibernateType(Types.OTHER, JsonNodeBinaryType.class.getName());
        this.registerHibernateType(Types.OTHER, JsonNodeStringType.class.getName());
    }
}

但迁移至Spring Boot 3.x(对应Hibernate 6.x)后,这种注册方式不再生效,方言代码简化为:

import org.hibernate.dialect.PostgresPlusDialect;

public class PostgreSQLJsonDialect extends PostgresPlusDialect {
    public PostgreSQLJsonDialect() {
        super();
    }
}

我的Work实体原配置:

import com.fasterxml.jackson.databind.JsonNode;
import io.hypersistence.utils.hibernate.type.json.JsonType;
// ...
@TypeDef(
        name = "jsonb",
        typeClass = JsonBinaryType.class
)
public class Work {
    @Column(name = "output", columnDefinition = "jsonb")
    @JsonProperty("output")
    @Type(type = "jsonb")
    private JsonNode output;
    // ...
}

迁移后修改为:

import com.fasterxml.jackson.databind.JsonNode;
import io.hypersistence.utils.hibernate.type.json.JsonType;
// ...
public class Work {
    @Column(name = "output", columnDefinition = "jsonb")
    @JsonProperty("output")
    @Type(JsonType.class)
    private JsonNode output;
    // ...
}

仓库代码:

public interface WorkRepository extends JpaRepository<Task, UUID> {
    @Query(value = "SELECT output FROM work WHERE job_id = :jobId AND type = :type ORDER BY ended_at DESC LIMIT 1",
            nativeQuery = true)
    Optional<JsonNode> getLatestOutput(String type, UUID jobId);
}

我希望能以JsonNode格式获取数据,尝试通过重写resolveSqlTypeDescriptor方法配置方言:

import org.hibernate.dialect.PostgresPlusDialect;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
import io.hypersistence.utils.hibernate.type.json.JsonNodeBinaryType;
import io.hypersistence.utils.hibernate.type.json.JsonNodeStringType;
import io.hypersistence.utils.hibernate.type.json.JsonStringType;
import io.hypersistence.utils.hibernate.type.json.JsonType;

public class PostgreSQLJsonDialect extends PostgresPlusDialect {
    public PostgreSQLJsonDialect() {
        super();
        final TypeConfiguration typeConfiguration = new TypeConfiguration();
        final JdbcTypeRegistry jdbcTypeRegistry = new JdbcTypeRegistry(typeConfiguration);
        this.resolveSqlTypeDescriptor(new JsonBinaryType().getName(), 0, 0, 0, jdbcTypeRegistry);
        this.resolveSqlTypeDescriptor(new JsonNodeBinaryType().getName(), 0, 0, 0, jdbcTypeRegistry);
        this.resolveSqlTypeDescriptor(new JsonType().getName(), 0, 0, 0, jdbcTypeRegistry);
        this.resolveSqlTypeDescriptor(new JsonStringType().getName(), 0, 0, 0, jdbcTypeRegistry);
        this.resolveSqlTypeDescriptor(new JsonNodeStringType().getName(), 0, 0, 0, jdbcTypeRegistry);
    }

    @Override
    public JdbcType resolveSqlTypeDescriptor(final String columnTypeName, int jdbcTypeCode, final int precision, final int scale, final JdbcTypeRegistry jdbcTypeRegistry) {
        jdbcTypeCode = SqlTypes.OTHER;
        return super.resolveSqlTypeDescriptor(columnTypeName, jdbcTypeCode, precision, scale, jdbcTypeRegistry);
    }
}

但仍报错,求问如何正确注册JsonNode以实现数据的JsonNode格式读取。

内容的提问来源于stack exchange,提问作者Anmol Binani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 04:44:55