2

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.

asked May 19, 2022 at 5:49

1 Answer 1

4

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

It works with this.Thanks! But the Annotations are now deprecated for this hibernate version
Cool. If it works you can upvote that will help others.
not worked with jpa 6, TypeDefs removed

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.