我正在使用带有kotlin和postgresql的spring boot作为db。对于我使用的json字段 hibernate-types-52
:
dependencies {
...
implementation("com.vladmihalcea:hibernate-types-52:2.10.2")
...
}
我有一个实体地址和street json字段:
@Entity
@Table(name = "addresses")
@TypeDefs(
TypeDef(name = "jsonb", typeClass = JsonBinaryType::class),
TypeDef(name = "json", typeClass = JsonStringType::class),
)
data class Address(
@Id
var id: Int? = null,
@Type(type = "jsonb")
@Column(name = "street", columnDefinition = "json", nullable = false)
var street: Map<Language, String>,
@Column(name = "zip_code" nullable = false)
var zipCode: String,
@Column(name = "created_at", nullable = false, updatable = false)
@CreationTimestamp
var createdAt: Date? = null,
@Column(name = "updated_at", nullable = false)
@UpdateTimestamp
var updatedAt: Date? = null,
}
相应的存储库是:
@Repository
interface AddressRepository : JpaRepository<Address, Int> {
fun findStreetByZipCode(zipCode: String): AddressStreetOnly?
}
其中addressstreetonly是一个接口:
interface AddressStreetOnly {
val id: Int
val street: Map<Language, String>
}
语言是一种枚举 enum class Language { EN, RU, KZ, TJ }
问题是我打电话的时候 findStreetByZipCode
基础sql语句在select查询中包含所有字段:
select
address0_.id as id1_9_,
address0_.created_at as created_2_9_,
address0_.street as street3_9_,
address0_.updated_at as updated4_9_,
address0_.zip_code as zip_cod5_9_
from
addresses
where
address0_.zip_code = ?
但如果我改变了 val street: Map<Language, String>
在addressstreetonly接口中键入 val street: Any
,一切正常:
select
address0_.id as col_0_0_,
address0_.street as col_1_0_
from
addresses
where
address0_.zip_code = ?
我认为问题出在street字段的类型上,它是map,它迫使map获取实体的所有字段。我怎样才能解决这个问题?
暂无答案!
目前还没有任何答案,快来回答吧!