5

之下的物体保持对象是一个后续问题Question 13832188含弹簧数据的MongoDB

我使用spring-data-mongodb版本1.1.1.RELEASE。如果所有成员变量都是原始类型,即使参数@PersistenceConstructor的名称与使用@Field@Value注释的成员变量的名称完全不匹配,我也可以保留该对象。

但是,当我尝试持续包含其他对象的对象时,我得到一个MappingInstantiationException。我的问题:

  • 这是一个错误spring-data-mongodb或我做错了什么?
  • 需要改变什么才能正确保存包含对象的对象?

org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.recorder.TestRecorder2$ObjectContainer]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch 
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:77) 
    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.TestRecorder2.testObjectContainer(RecorderTest2.java:63) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.recorder.TestRecorder2$ObjectContainer]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:158) 
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:75) 
    ... 34 more 
Caused by: java.lang.IllegalArgumentException: argument type mismatch 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) 
    ... 35 more 

PrimitiveContainerObjectContainer类列举如下:

@Document 
class PrimitiveContainer { 

    @Field("property") 
    private final int m_property; 

    @PersistenceConstructor 
    public PrimitiveContainer(@Value("#root.property") int a_property) { 
     m_property = a_property; 
    } 

    public int property() { 
     return m_property; 
    } 
} 

@Document 
class ObjectContainer { 

    @Field("property") 
    private final PrimitiveContainer m_property; 

    @PersistenceConstructor 
    public ObjectContainer(@Value("#root.property") PrimitiveContainer a_property) { 
     m_property = a_property; 
    } 

    public PrimitiveContainer property() { 
     return m_property; 
    } 
} 

**更新:**奇怪的是,改变字符串中的@Field注释比 “属性” 以外的东西或删除@Field注释完全为ObjectContainerm_property允许spring-data-mongo-db属性重新实例化我持久的ObjectContainer类。我不明白为什么这会起作用。有人有主意吗?

+0

什么是源文件的样子?即文档在数据库中看起来像什么? –

+0

{“_id”:ObjectId(“50ca271c4566a2b08f2d667a”),“_class”:“com.recorder.TestRecorder2 $ ObjectContainer”,“property”:{“property”:100}} –

回答

2

您似乎找到了一个错误。第一个Spel表达式评估产生的值是DBObject,实际上并没有转换为参数类型。我在本地已经有一些代码可以解决这个问题。你介意创建一个针对Spring Data MongoDB的票据来将其作为一个错误进行存档吗?

+0

我已创建[DATAMONGO-592]( https://jira.springsource.org/browse/DATAMONGO-592)来跟踪这个问题。谢谢你的帮助!! –

+0

我也发现我不能持有包含原始数组的对象。是否阻止我正确地持久保存包含对象的对象的错误,从而阻止我正确地持久保持包含原始数组的对象?在[StackOverflow 13884637](http://stackoverflow.com/questions/13884637)中查看数组问题的详细信息 –