我正在使用JPA作为一个项目的持久性API。它包含一个实体,该实体具有Postgres jsonb
列,如下所示:
@Entity
@TypeDefs(value = [TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)])
data class Post(
//... irrelevant columns
@Type("jsonb") @Column(columnDefinition = "jsonb") val postDefinitions: List<Map<String, Any>>
)
这在从该实体获取所有数据时非常有效。但是现在我不需要返回所有的,只需要返回一些字段,包括jsonb
。这就是事情变得丑陋。我有一个投影对象,我使用下面的本地查询来检索数据,但是JPA无法Map它。
interface PostProjection {
val id: UUID
val postDefinitions: List<Map<String, Any>>
}
@Query("SELECT CAST(id AS VARCHAR) AS id, jsonb_array_elements(post_definitions) AS postDefinitions " +
"FROM post WHERE id = :postId", nativeQuery = true)
fun getPostDefinitionsById(val id: UUID): List<PostProjection>
它已经有了getters
和setters
。我尝试用@TypeDefs
注解接口,用@Type
注解列,并将其从interface
更改为data class
。
没有成功。找不到任何关于这个的信息。我不想检索String
,然后将其Map到相应的数据类型。
有人翻过吗?
1条答案
按热度按时间nzk0hqpo1#
我遇到了同样的问题,并找到了解决方案:只需要用对象创建构造函数,而不是你的构造函数,然后在里面强制转换:这是我的投影
这是我的问题