如何使用 Jsonb 库从 Postgres 读取 jsonb

How to read jsonb from Postgres using the Jsonb library

我正在尝试从 postgre table 中读取一个 jsonb 字段。我尝试通过以下方式使用 Jsonb 库将其作为 Jsonb 类型读取:

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

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

)

但显然,它没有构建给我以下错误:

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]

我找到了一些帖子和答案。但是 none 显然在使用 this api

首先,PostgreSQL jsonb 类型与您正在使用的 JSR-367 Java API 不对应。那一个序列化工具。您需要的是 JSR-353 javax.json:javax.json-api, the reference implementation of which is org.glassfish:javax.json.

意思代替

val candidates: Jsonb = JsonbBuilder.create()

你应该使用

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

让 Hibernate(错误消息显示您正在使用)5.1、5.2 或 5.3 识别 JSR-353 对象的最简单方法是将 this type-contributor library 添加到您的项目依赖项中。 Hibernate 5.1 是最低要求的版本,从 6.0(或可能早至 5.4)开始,类型系统将被重新设计并变得不兼容。

哦,你不需要 @Type 注释。