【问题标题】:hibernate json mapping with psql database休眠 json 映射与 psql 数据库
【发布时间】:2021-08-27 13:49:28
【问题描述】:

使用 JPA,我尝试将 @Entity 类的对象映射到 postgres DB json 列。

@Enity
public class MyEntity{

@NotNull
@ToString.Exclude
@Convert(converter = JsonSerializationConverter.class)
private Object value;
}
public class JsonSerializationConverter implements AttributeConverter<Object, String> {

    private static final ObjectMapper mapper = new ObjectMapper();

    @SneakyThrows
    @Override
    public String convertToDatabaseColumn(final Object attribute) {
        return mapper.writeValueAsString(attribute);
    }

在本地运行时,它的工作原理就像一个魅力,但由于未知原因,它在 k8n pod 上的天蓝色云中运行失败:

SchemaManagementException: Schema-validation: wrong column type encountered in column [value] in table [preference] found [json (Types#OTHER)], but expecting [varchar(255)

主要是其他stackoverflow issue参考这个:postgre bug并建议设置 数据源属性:stringtype=unspecified

不幸的是,这并不能解决 azure kn8 ubuntu pod 中的问题。 我都试过了:

spring.datasource.url: jdbc:postgresql://xyz:5432/dbname?stringtype=unspecified
spring.datasource.hikari.data-source-properties.stringtype: unspecified

【问题讨论】:

    标签: java postgresql hibernate


    【解决方案1】:

    它在本地起作用的原因是 spring.jpa.hibernate.ddl-auto:无(并在 K8n 中验证)

    但是对于所有 ENV,这解决了问题:

    @Enity
    public class MyEntity{
    
    @NotNull
    @ToString.Exclude
    //this resolved the issue
    @Column(name = "value", columnDefinition = "json")
    @Convert(converter = JsonSerializationConverter.class)
    private Object value;
    }
    

    只是不适用于 zonky 嵌入式 postgre

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多