Reputation: 55
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.
Upvotes: 2
Views: 5751
Reputation: 3170
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<>();
}
Upvotes: 4