简体   繁体   中英

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. We have a column in db whose type is JSON but cant find its corresponding mapping in 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.

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<>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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