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.
1 Answer 1
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<>();
}
answered May 19, 2022 at 6:30
Sign up to request clarification or add additional context in comments.
3 Comments
Sai
It works with this.Thanks! But the Annotations are now deprecated for this hibernate version
GnanaJeyam
Cool. If it works you can upvote that will help others.
AnasSafi
not worked with jpa 6, TypeDefs removed
Explore related questions
See similar questions with these tags.
default