如何在Hibernate中将JSONBMap到投影字段上?

rvpgvaaj  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(122)

我在PostgreSQL中有一个表,其字段类型为JSONB,实体如下所示:

@Entity
@Table(name = "test_table")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
class TestEntity(

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "test_id")
    var main: Main? = null,

    @Column(name = "id")
    val id: String,

    @Column(name = "fallback_id")
    var fallbackId: String? = null,

    @Column(name = "properties", columnDefinition = "jsonb")
    @Type(type = "jsonb")
    val properties: MutableMap<String, Any> = mutableMapOf(),

字符串
projection是这样的:

interface TestProjection {
    val id: String
    val fallbackId: String
    val properties: MutableMap<String, Any>
}


我想从数据库中检索并Map到这个projection(使用JSONB字段作为Map),但是当我尝试使用这个repository这样做时:

@Repository
interface TestRepository : JpaRepository<TestEntity, UUID> {
fun findByFallbackIdIn(fallbackIds: Set<String>): Set<TestEntity>

@Query(
        nativeQuery = true,
        value = """
               SELECT 
                     cast(t.id as varchar) as id,
                     cast(t.fallback_id as varchar) as fallback_id,
                     t.properties as properties,
               FROM test_table t
               AND t.id < :last_uuid
               ORDER by t.id DESC
               LIMIT :batch_size
            """
    )
    fun findTestForSomeLogic(
        @Param("last_uuid") lastUuid: UUID,
        @Param("batch_size") batchSize: Int,
    ): List<TestProjection>
}


我从hibernate得到了一个异常:

No Dialect mapping for JDBC type: 1111; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111

那么如何将JSONBMap到投影的字段?

cbwuti44

cbwuti441#

如何德米特里波波夫建议我附上文章的一部分,可以帮助你,问题是在方言的一部分,你可以扩展你目前的方言在文章


的数据

需要提供自定义的hibernate方言:



文章:hibernate-no-dialect-mapping-for-jdbc-type

相关问题