我正在尝试使用spring-data-mongodb
version保留以下对象1.1.1.RELEASE
:
@Document
public static class TestObject {
private final int m_property;
@PersistenceConstructor
public TestObject(int a_property) {
m_property = a_property;
}
public int property() {
return m_property;
}
}
MappingException
当我尝试从数据库中读回对象时,我得到了一个(请参阅下面的完整堆栈跟踪)
我的小组使用的命名约定要求参数变量名称以 . 开头a_
,实例变量名称以 . 开头m_
。似乎spring-data-mongodb
假设构造函数参数变量名称必须与对象实例变量名称匹配。
- 为什么不
spring-data-mongodb
使用我在构造函数中定义的实例变量映射的构造函数参数? - 是否有另一种方法来定义此映射,以便
spring-data-mongodb
正确构造我的对象,或者是我打破命名约定的唯一选择?
.
Exception in thread "main" org.springframework.data.mapping.model.MappingException: No property a_property found on entity class com.recorder.TestRecorder$TestObject to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:90)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:70)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:229)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:209)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:173)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:169)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:72)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1820)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1542)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1064)
at com.recorder.TestRecorder.main(TestRecorder.java:43)