我最近一直在一起使用Kmongo library和Kotlin,但是我在Kmongo上提出了一个问题,但我不确定这是否与这个库有关。
我正在尝试将我的数据持久化到我的mongo数据库(4.2.2版)上。
@Serializable
data class Person(val firstname: String, val lastname: String){
val fullName
get() = "$lastname $firstname"
}当我插入数据时,我只发送了一个像这样的对象:val person = Person("John", "Doe"),但是当我检查我的mongo数据库时
db.persons.find()
> { "_id" : ObjectId("5e2da298159243f9894d3834"), "firstname" : "John", "lastname" : "Doe", "fullName" : "Doe John" }如何防止将fullName保存到数据库中?
编辑:
我试图在我的变量上使用@Transient注释,但它不起作用,并且我得到了一条检查消息:Property does not have backing field which makes it non-serializable and therefore @Transient is redundant
发布于 2020-01-27 21:34:02
在这一天,Kmongo库在后台使用Jackson,以避免我必须使用的解析属性:https://www.concretepage.com/jackson-api/jackson-jsonignore-jsonignoreproperties-and-jsonignoretype
因此,我的数据类的代码现在是:
@Serialiable
@JsonIgnoreProperties("fullName")
data class Person(...发布于 2020-01-27 01:37:55
使用@Transient注释:
@Transient
val fullName
get() = "$lastname $firstname"https://stackoverflow.com/questions/59919436
复制相似问题