简体   繁体   English

SpringBoot 将 Java 实体映射到数据库中的 Json 列

[英]SpringBoot map Java entity to Json column in database

Need help in mapping entity attribute of Spring to json column in Postgres DB using JPA hibernate.在使用 JPA hibernate 将 Spring 的实体属性映射到 Postgres DB 中的 json 列时需要帮助。 We have a column in db whose type is JSON but cant find its corresponding mapping in spring.我们在 db 中有一个类型为 JSON 的列,但在 spring 中找不到其对应的映射。

Tried with this-试过这个 -

 @Column(name = "tags", columnDefinition = "jsonb")
 @JsonProperty("tags")
 private Map<String,Object> tags = new HashMap<>();

But with this, we getting error while creating db table Any suggestions regarding this?但是有了这个,我们在创建数据库表时遇到错误关于这个有什么建议吗? Thanks.谢谢。 Any help is appreciated.任何帮助表示赞赏。

There is no out of box support for this.对此没有开箱即用的支持。 But you can use vladmihalcea library.但是您可以使用vladmihalcea库。

Gradle Dependency:摇篮依赖:

implementation 'com.vladmihalcea:hibernate-types-52:2.16.2'

Your Entity:您的实体:

import com.vladmihalcea.hibernate.type.json.JsonType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
@TypeDefs({
    @TypeDef(name = "json", typeClass = JsonType.class)
})
class YourPojo {
    
    @Type(type = "json")
    @Column(name = "tags", columnDefinition = "json")
    private Map<String,Object> tags = new HashMap<>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM