0

I am using spring data in my project with Postgres as the Database. I have an entity that has a JSON field and I want to keep it as a String in the java context, but a jsonb in the database. Whenever I want to persist an instance of my entity the problem arises since hibernate is treating the JSON property as a Basic type and the underlying query does not change the String type to jsonb type.

@Entity
@Table(name = "a", catalog = "")
@TypeDef(name = "jsonb", typeClass = String.class)
public class A {
    @Type(type = "jsonb")
    @Column(name = "json-data", columnDefinition = "jsonb")
    private String myJsonData;
}

I find the solution for this problem here, but it needs to change the parameters of the underlying PreparedStatement query. So, I need to change the query and add ::jsonb to make Postgres cast the parameter to jsonb. Is there any Annotation related to @Type to make it work?

MHM
  • 854
  • 1
  • 9
  • 17

1 Answers1

0

You can use @ColumnTransformer for this purpose.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58