使用Postgres JSONB类型时,带有R2DBC的Kotlin Micronaut数据失败

发布于 2025-02-06 14:50:00 字数 2560 浏览 3 评论 0原文

我正在尝试使用Postgres设置Micronaut Data R2DBC,但它会以


    io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: [42804] column blob is of type jsonb but expression is of type character varying

micronautversion = 3.5.1 失败。 kotlinversion = 1.6.10 javaversion = 17

build.gradle.kts


    val coroutinesVersion = "1.6.0"
    dependencies {
        kapt("io.micronaut.data:micronaut-data-processor")
        // kapt("io.micronaut:micronaut-http-validation")
        // implementation("io.micronaut:micronaut-http-client")
        implementation("io.micronaut:micronaut-jackson-databind")
        implementation("io.micronaut.data:micronaut-data-r2dbc")
        implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
        implementation("jakarta.annotation:jakarta.annotation-api")
        implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:${coroutinesVersion}")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:${coroutinesVersion}")
        runtimeOnly("ch.qos.logback:logback-classic")
        runtimeOnly("org.postgresql:r2dbc-postgresql")
        implementation("io.micronaut:micronaut-validation")
        implementation("io.micronaut:micronaut-runtime")
        implementation("io.micronaut:micronaut-jackson-databind")
        runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    }

postgres schema


    CREATE TABLE entity (
        entity_id          TEXT PRIMARY KEY,
        entity_type        TEXT NOT NULL,
        blob               JSONB NOT NULL,
        CREATED_DATE       TIMESTAMP(3) DEFAULT now() NOT NULL
    );

对应的kotlin数据类未能使


    @MappedEntity
    data class Entity(
      @field:Id val entityId: String,
      val entityType: String,
      @TypeDef(type = DataType.JSON) val blob: Any,
      val createdDate: Instant
    )

失败的测试


    @Test
    fun testInsert() = runBlocking {
        val now = Instant.now()
        val entity = Entity("entity-uuid-1", "test-entity-type", "{}", now)
        Assertions.assertEquals(entity, entityRepository.save(entity))
    }

完整项目 为在这里

看起来要么缺少将字符串转换为JSON所需的依赖项,要么需要自定义转换器。有人可以帮忙吗?

I am trying to set up Micronaut Data r2dbc with Postgres but it fails with


    io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: [42804] column blob is of type jsonb but expression is of type character varying

micronautVersion=3.5.1
kotlinVersion=1.6.10
javaVersion=17

build.gradle.kts


    val coroutinesVersion = "1.6.0"
    dependencies {
        kapt("io.micronaut.data:micronaut-data-processor")
        // kapt("io.micronaut:micronaut-http-validation")
        // implementation("io.micronaut:micronaut-http-client")
        implementation("io.micronaut:micronaut-jackson-databind")
        implementation("io.micronaut.data:micronaut-data-r2dbc")
        implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
        implementation("jakarta.annotation:jakarta.annotation-api")
        implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:${coroutinesVersion}")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:${coroutinesVersion}")
        runtimeOnly("ch.qos.logback:logback-classic")
        runtimeOnly("org.postgresql:r2dbc-postgresql")
        implementation("io.micronaut:micronaut-validation")
        implementation("io.micronaut:micronaut-runtime")
        implementation("io.micronaut:micronaut-jackson-databind")
        runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    }

Postgres schema


    CREATE TABLE entity (
        entity_id          TEXT PRIMARY KEY,
        entity_type        TEXT NOT NULL,
        blob               JSONB NOT NULL,
        CREATED_DATE       TIMESTAMP(3) DEFAULT now() NOT NULL
    );

Corresponding Kotlin data class


    @MappedEntity
    data class Entity(
      @field:Id val entityId: String,
      val entityType: String,
      @TypeDef(type = DataType.JSON) val blob: Any,
      val createdDate: Instant
    )

The test that fails


    @Test
    fun testInsert() = runBlocking {
        val now = Instant.now()
        val entity = Entity("entity-uuid-1", "test-entity-type", "{}", now)
        Assertions.assertEquals(entity, entityRepository.save(entity))
    }

The complete project is here

It looks like either it's missing a required dependency to convert string to json or need a custom converter for this. Can someone please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2025-02-13 14:50:00

我通过使用

    implementation("io.r2dbc:r2dbc-postgresql:0.8.12.RELEASE")

而不是

    implementation("org.postgresql:r2dbc-postgresql")

数据类来更新以使用io.r2dbc.postgresql.codec.json而不是任何

    @MappedEntity
    data class Entity(
      @field:Id val entityId: String,
      val entityType: String,
      @TypeDef(type = DataType.JSON) val blob: Json,
      val createdDate: Instant
    )

I got this working by using

    implementation("io.r2dbc:r2dbc-postgresql:0.8.12.RELEASE")

instead of

    implementation("org.postgresql:r2dbc-postgresql")

The data class was updated to use io.r2dbc.postgresql.codec.Json instead of Any

    @MappedEntity
    data class Entity(
      @field:Id val entityId: String,
      val entityType: String,
      @TypeDef(type = DataType.JSON) val blob: Json,
      val createdDate: Instant
    )
沐歌 2025-02-13 14:50:00

编辑:看来我误会了,实际问题是映射:

而不是:

@TypeDef(type = DataType.JSON)

应该是:

@field:TypeDef(type = DataType.JSON)

看起来R2DBC Postgres驱动程序不允许String用于JSONB,这很奇怪,因为JDBC很好。您可能需要尝试创建attributeconverter将其转换为byte []

EDIT: It looks like I was mistaken and the actual problem is with the mapping:

Instead of:

@TypeDef(type = DataType.JSON)

It should be:

@field:TypeDef(type = DataType.JSON)

It looks like R2DBC Postgres driver doesn’t allow a String to be used for JSONB, which is strange because JDBC is fine. You might want to try creating AttributeConverter to convert it into byte[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文