Kotlin getter 覆盖 + MongoDB

Kotlin getter override + MongoDB

我是 Kotlin 开发的新手,我不知道如何处理这个问题。我将以下 Kotlin 数据 class 映射到 MongoDB 集合(Spring 数据 MongoDB):

@Document(collection = "orders")
data class OrderEntity
@PersistenceConstructor
constructor(@Id val id: ObjectId? = null, val place: String, var date: Date,
            val closed: Boolean = false, val price: Int = 0)

我想覆盖默认 ID getter 和 return 字符串而不是 ObjectId。似乎无法更改 "id" 字段名称,因为我收到消息 "Customizing field name for id property not allowed! Custom name will not be considered!" 所以我无法使用始终建议的 _id 解决方案。

如何才能做到这一点?我错过了什么吗?

我没有跟上最新和伟大的 spring-data-mongo 变化,但是如果您只是将您的 id 更改为 String 类型?而不是 ObjectId and 您的字符串值恰好是 ObjectId 的 "string" 十六进制代码表示,spring 数据将在保存到时自动将其转换为 ObjectId数据库并在将其读回 bean 时自动将 ObjectId 转换为字符串。

基本上 spring-data-mongo 为您施展魔法。我怀疑他们从 1.x 天起就改变了这种行为,但我可能错了。