Reputation: 421
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
Upvotes: 0
Views: 2074
Reputation: 5095
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.
Upvotes: 2