首页 > 解决方案 > 如何将 LocalDate 作为 JSON 属性存储在带有 Hibernate JPA 的 postgres jsonb 列中?

问题描述

我在 postgres 表中有一个 jsonb 列。对应的 JPA 实体字段是具有 LocalDate 属性的 Java DTO。LocalDate 字段作为列表存储在 postgres 中。

JAVA实体:

@Entity
@Table(name = "my_entity")
public class MyEntity
{
    @EmbeddedId
    private EmbeddedId embeddedId;

    @Type(type = "jsonb")
    @Column(name = "jsonb_column", columnDefinition = "jsonb", nullable = false)
    private JavaDto javaDto;
}

JavaDto.java:

@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class JavaDto
{
    private LocalDate localDate;
    private String type;
    private String id;
}

my_tablepostgres 表中的示例行:

id                  | 90
other_col           | OTHER_COL_VAL
jsonb_column        | {"id": "123-some-id", "type": "someType", "localDate": [2021, 10, 13]}
creation_time       | 2020-11-02 15:44:42.134+05:30
last_modified_time  | 2020-11-02 15:44:42.134+05:30

存储 localDate 的格式会产生问题,以使用 where 子句运行读取查询。任何帮助,将不胜感激。

我使用 Hibernate 作为 JPA 实现。

标签: javapostgresqlhibernatejpajackson

解决方案


推荐阅读