V
V
Vitaly Stolyarov2017-01-10 02:08:19
Hibernate
Vitaly Stolyarov, 2017-01-10 02:08:19

JSONB in ​​Hibernate?

You need to make Hibernate friends with PostgreSQL, I do it as follows:
Define your UserType

public class JSONType implements UserType {

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.JAVA_OBJECT};
    }

    @Override
    public Class<JSONObject/*MyJson*/> returnedClass() {
        return JSONObject.class;
    }

....

}

I specify the type and the class to it
@org.hibernate.annotations.TypeDef(name = "JSONType", typeClass = JSONObject.class)

In Entity one of the fields
@Column
    @Type(type = "JSONType")
    private JSONObject data;

And finally I add the type to the dialect
public class MyPostgreSQL94Dialect extends PostgreSQL94Dialect {

    public MyPostgreSQL94Dialect() {
        this.registerColumnType(Types.JAVA_OBJECT, "jsonb");
    }
}

(of course I specify it in persistence.xml
But it turns out the following:
the table is generated with the type "byte"
b51563ce82284df487c0c4a7612b7d31.png
What could be the error?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question