【发布时间】:2019-01-20 06:51:47
【问题描述】:
环境: 春季启动 2.1.2.RELEASE, 弹簧数据2.1.4.RELEASE, 科特林 1.2.x ~ 1.3.x, MongoDB。
我定义了一个模型类,如:
@Document
class MeAccount : Setting() {
lateinit var id: String
val accountEntries = listOf<BankAccount>()
}
当我试图从 mongodb 读取这个模型时,我得到了异常堆栈跟踪:
java.lang.UnsupportedOperationException: No accessor to set property private final java.util.List com.xxx.MeCustodianAccount.accountEntries!
at com.xxx.MeCustodianAccount_Accessor_fs514j.setProperty(Unknown Source)
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:61)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:378)
请注意,该代码适用于 spring-boot 1.5.x 和 spring-data 1.x。
我能做些什么来解决这个问题? 下面似乎有很多类似的异常报告:
Spring boot 2.1.0 security change with kotlin data class?
https://github.com/arangodb/spring-data/issues/123
https://github.com/spring-projects/spring-boot/issues/15698
[已解决] 回退到 Spring-boot 2.0.x 和 spring-data-commons 2.0.x 后工作。 将在未来的升级计划中排除 2.1。
【问题讨论】:
-
val属性没有设置器。将其更改为var -
@Strelok 这是完全不能接受的,val 是我在 kotlin 中令人钦佩的特性。该代码适用于 spring-data 1.12.x。
-
Welp,对不起,但这是事实。主体中的 Vals 需要在 init 块、构造函数或内联中初始化。他们没有二传手
-
您是否考虑过将其设为
data类并将属性移动到构造函数中?正如你现在所拥有的,没有任何东西(包括 Spring Data MongoDB)可以设置accountEntries的值。 -
正如我所说,'val' 是我们最想要的 kotlin 功能,而代码失败是因为 spring-data 2.1.x 破坏了兼容性。 @Mark B 该代码在以前的 spring-data 版本中运行良好。
标签: mongodb kotlin spring-data spring-data-mongodb