Home > Back-end >  How to use a jsonb column with H2, JPA, and Hibernate
How to use a jsonb column with H2, JPA, and Hibernate

Time:12-10

Using Spring, when I try running my integration tests don't work because the H2 database can't create the table. This is the error:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table foo (id binary not null, bar jsonb, primary key (id))" via JDBC Statement

My entity being defined as follow :

@Entity(name = "foo")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Foo {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private Object bar;
}

My type being defined through com.vladmihalcea:hibernate-types-52:2.14.0.

And my h2 version being com.h2database:h2:2.0.202.

If I remove the bar property, it works fine.

Any clue please ?

CodePudding user response:

jsonb is a PostgreSQL data type, not available in H2. It differs from PG's json data type in that internally it is stored in a binary structure that is more suitable for some operations and indexing.

H2 does have a json format, though https://www.h2database.com/html/datatypes.html#json_type

  • Related