是否可以将 Hibernate 与 PostgreSql 的 JSONB 数据类型一起使用? [英] Is it possible to use Hibernate with PostgreSql's JSONB data type?

查看:29
本文介绍了是否可以将 Hibernate 与 PostgreSql 的 JSONB 数据类型一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate 5 默认不支持 PostgreSQL jsonb 数据类型.

Hibernate 5 does not support the PostgreSQL jsonb data type by default.

有没有办法实现jsonb对Hibernate + Spring JPA的支持?

Is there any way to implement jsonb support for Hibernate + Spring JPA?

如果有办法,将 jsonb 与 Hibernate 结合使用的利弊是什么?

If there is a way, what are the pros and cons of using jsonb with Hibernate?

推荐答案

感谢 Vlad Mihalcea 我们有这样的机会!)

Thanks Vlad Mihalcea we have such opportunity! )

他创建了 hibernate-types 库:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>2.1.1</version>
</dependency> 

为 Hibernate 添加了对 'json'、'jsonb' 等类型的支持:

which adds a support of 'json', 'jsonb' and other types to Hibernate:

@Data
@NoArgsConstructor
@Entity
@Table(name = "parents")
@TypeDefs({
        @TypeDef(name = "string-array", typeClass = StringArrayType.class),
        @TypeDef(name = "int-array", typeClass = IntArrayType.class),
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class Parent implements Serializable {

    @Id
    @GeneratedValue(strategy = SEQUENCE)
    private Integer id;

    @Column(length = 32, nullable = false)
    private String name;

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Child> children;

    @Type(type = "string-array")
    @Column(columnDefinition = "text[]")
    private String[] phones;

    public Parent(String name, List<Child> children, String... phones) {
        this.name = name;
        this.children = children;
        this.phones = phones;
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child implements Serializable {
    private String name;
}

更多信息:12

这篇关于是否可以将 Hibernate 与 PostgreSql 的 JSONB 数据类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆