0

我正在使用spring-data-mongodb版本1.1.1.RELEASE。如果所有成员变量都是原始类型,即使参数的名称与使用和注释 @PersistenceConstructor的成员变量的名称不完全匹配,我也能够持久化一个对象。@Field("foo")@Value("#root.foo")

但是,MappingInstantiationException当我尝试持久化包含原始数组的对象时,我得到一个(构造函数的非法参数)。无论原始数组是否包含原始数组或对象,这都是正确的。这是一个错误spring-data-mongodb还是我做错了什么?

我在下面包含了三个依赖于junitand的单元测试spring-data-mongodb。测试显示了三件事:

  • 包含对象集合的持久对象按预期工作
  • 包含基元数组的持久对象无法按预期工作
  • 包含对象数组的持久对象无法按预期工作

.

public class SpringSourceTest {

    private static final String DATABASE_HOST = "localhost";
    private static final String DATABASE_NAME = "testdb";
    private static Mongo s_mongo = null;
    private static MongoOperations s_mongoOperations;

    @BeforeClass
    public static void setUpBeforeClass() {
        try {
            s_mongo = new Mongo(DATABASE_HOST);
        }
        catch (UnknownHostException e) {
            e.printStackTrace();
        }
        assertNotNull(s_mongo);
        s_mongoOperations = new MongoTemplate(s_mongo, DATABASE_NAME);
        assertNotNull(s_mongoOperations);
    }

    @AfterClass
    public static void tearDownAfterClass() {
        s_mongo.dropDatabase(DATABASE_HOST);
        s_mongo.close();
    }

    @Test
    public void testCollectionContainer() {
        System.out.println("---------------testCollectionContainer-----------------");
        List<Integer> intList = new ArrayList<Integer>();
        intList.add(1);
        intList.add(2);
        CollectionContainer collectionContainer = new CollectionContainer(intList);
        testObject(collectionContainer);
    }

    @Test
    public void testArrayContainer() {
        int[] properties = new int[]{100, 50};
        System.out.println("----------------testArrayContainer----------------");
        ArrayContainer arrayContainer = new ArrayContainer(properties);
        testObject(arrayContainer);
    }

    @Test
    public void testArrayObjectContainer() {
        System.out.println("----------------testArrayObjectContainer----------------");
        Integer[] properties = new Integer[]{new Integer(100), new Integer(50)};
        ArrayObjectContainer arrayObjectContainer = new ArrayObjectContainer(properties);
        testObject(arrayObjectContainer);
    }

    private void testObject(Object a_object) {
        assertNotNull(a_object);
        System.out.println("OLD OBJECT: " + a_object);
        Class<?> objectClass = a_object.getClass();
        s_mongoOperations.dropCollection(objectClass);
        s_mongoOperations.createCollection(objectClass);
        s_mongoOperations.insert(a_object);
        List<?> createdObjects = null;
        try {
            createdObjects = s_mongoOperations.findAll(objectClass);
            for (Object createdObject : createdObjects) {
                System.out.println("NEW OBJECT: " + createdObject);
                assertNotNull(createdObject);
                assertEquals(a_object, createdObject); 
            }   
        }
        catch (Exception e){
            System.out.println("FAILED TO RETRIEVE: " + objectClass);
            System.out.println(e.getMessage().toString());
            fail();
        }
        s_mongoOperations.dropCollection(objectClass);
    }


    @Document
    public static class CollectionContainer {
        @Field("property") private final List<Integer> m_property;

        @PersistenceConstructor
        public CollectionContainer(@Value("#root.property") List<Integer> a_property) {
            validateNotNull(a_property);
            m_property = a_property;
        }

        private void validateNotNull(List<Integer> a_property) {
            assertNotNull(a_property);
        }

        public List<Integer> property() {
            return m_property;
        }

        @Override
        public boolean equals(Object a_obj) {
            if (this == a_obj) {
                return true;
            }
            if ((a_obj == null) || getClass() != a_obj.getClass()) {
                return false;
            }
            final CollectionContainer other = (CollectionContainer) a_obj;
            for (int i = 0; i < m_property.size(); i++) {
                if (!m_property.get(i).equals(other.property().get(i))) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + m_property.hashCode();
            return result;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("CollectionContainer={")
                    .append("m_property=").append(m_property)
                    .append("}").toString(); 
            return sb.toString();
        }
    }

    @Document
    public static class ArrayContainer {
        @Field("property") private final int[] m_property;

        @PersistenceConstructor
        public ArrayContainer(@Value("#root.property") int[] a_property) {
            validateNotNull(a_property);
            m_property = a_property;
        }

        private void validateNotNull(int[] a_property) {
            assertNotNull(a_property);
        }

        public int[] property() {
            return m_property;
        }

        @Override
        public boolean equals(Object a_obj) {
            if (this == a_obj) {
                return true;
            }
            if ((a_obj == null) || getClass() != a_obj.getClass()) {
                return false;
            }
            final ArrayContainer other = (ArrayContainer) a_obj;
            for (int i = 0; i < m_property.length; i++) {
                if (m_property[i] != other.property()[i]) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + m_property.hashCode();
            return result;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("ArrayContainer={");
            sb.append("m_property=[");
            for (int i = 0; i < m_property.length; i++) {
                sb.append(m_property[i]).append(",");
            }
            sb.append("]}").toString(); 
            return sb.toString();
        }
    }

    @Document
    public static class ArrayObjectContainer {
        @Field("property") private final Integer[] m_property;

        @PersistenceConstructor
        public ArrayObjectContainer(@Value("#root.property") Integer[] a_property) {
            validateNotNull(a_property);
            m_property = a_property;
        }

        private void validateNotNull(Integer[] a_property) {
            assertNotNull(a_property);
        }

        public Integer[] property() {
            return m_property;
        }

        @Override
        public boolean equals(Object a_obj) {
            if (this == a_obj) {
                return true;
            }
            if ((a_obj == null) || getClass() != a_obj.getClass()) {
                return false;
            }
            final ArrayObjectContainer other = (ArrayObjectContainer) a_obj;
            for (int i = 0; i < m_property.length; i++) {
                if (!m_property[i].equals(other.property()[i])) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + m_property.hashCode();
            return result;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("ArrayObjectContainer={");
            sb.append("m_property=[");
            for (int i = 0; i < m_property.length; i++) {
                sb.append(m_property[i]).append(",");
            }
            sb.append("]}").toString(); 
            return sb.toString();
        }
    }
}

我得到的异常的堆栈跟踪也在下面列出。

org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.recorder.SpringSourceTest$ArrayContainer]: 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.SpringSourceTest.testObject(SpringSourceTest.java:82)
        at com.recorder.SpringSourceTest.testArrayContainer(SpringSourceTest.java:62)
        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.SpringSourceTest$ArrayContainer]: 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)
        ... 35 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)
        ... 36 more
4

0 回答 0