0

I am trying to read a jsonb field from a postgre table. I have tried to read it as a Jsonb type using the Jsonb library in the following way:

@Entity
@Table(name = "test")
data class Test(
    @Id
    val id: UUID = UUID.randomUUID(),

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    val candidates: Jsonb = JsonbBuilder.create(),

)

But apparently, it doesnt build giving me the following error:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

I have found a few posts and answer. But none of them are apparently using this api

1 Answer 1

2

Firstly the PostgreSQL jsonb type does not correspond to the JSR-367 Java API you're using. That one serialization tooling. The one you need is JSR-353 javax.json:javax.json-api, the reference implementation of which is org.glassfish:javax.json.

Meaning instead of

val candidates: Jsonb = JsonbBuilder.create()

You should use

val candidates: JsonObject = Json.createObjectBuilder().build()

And the simplest way to make Hibernate (which the error message reveals you are using) 5.1, 5.2 or 5.3 recognize JSR-353 objects, is to add this type-contributor library to your project dependencies. Hibernate 5.1 is the minimal required version, where as as of 6.0 (or possibly as early as 5.4) the type system will be redesigned and made incompatible.

Oh, and you won't need the @Type annotation with this.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. Now it builds. But it doesn't read. Following is the error message nested exception is org.springframework.orm.jpa.JpaSystemException: Could not set field value [[]] value by reflection. the data stored in the field is an Array of String.
Can not set final javax.json.JsonObject field to org.glassfish.json.JsonArrayBuilderImpl$JsonArrayImpl
Well, then use javax.json.JsonArray. Also, it shouldn't be final, because once you create the JsonArray you can't alter it. And since you're using this as a workaround for arrays, I would recommend my array type-contributor instead, which allows you to use string arrays and non-primitive number arrays. That is, unless you need JSON for other reasons.
Oh, and that package covers the default JVM classes in java.lang.* and java.time.*. However it is extensible, if you follow the example of the with-arrays branch of the json contributor github.com/mopano/hibernate-json-type/blob/with-arrays/…
JsonArray works perfectly. I tried that before but did not have the correct dependency. Thanks a lot :)

Your Answer

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.