0

I'm using spring-data-mongodb version 1.1.1.RELEASE. I am able to persist an object if all the member variables are primitive types, even if the names of the @PersistenceConstructor arguments don't match exactly the names of the member variables by using the @Field("foo") and @Value("#root.foo") annotations.

However, I get a MappingInstantiationException (Illegal arguments for constructor) when I try to persist objects that contain primitive arrays. This holds true regardless of whether the primitive arrays contain primitives or Objects. Is this a bug in spring-data-mongodb or am I doing something wrong?

I've included three unit tests below that depend on junit and spring-data-mongodb. The tests show three things:

  • Persisting objects containing Collections of objects works as expected
  • Persisting objects containing arrays of primitives does not work as expected
  • Persisting objects containing arrays of objects does not work as expected

.

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();
        }
    }
}

The stack trace of the exception I get is also listed below.

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
Ben Baumgold
  • 307
  • 1
  • 4
  • 12
  • It has been found that there is a bug in `spring-data-mongodb` that prevents properly persisting objects containing objects. I don't know whether the issue above is related or not. See the details of the objects containing objects issue in [StackOverflow 13884637](http://stackoverflow.com/questions/13854753) or [DATAMONGO-592](https://jira.springsource.org/browse/DATAMONGO-592) – Ben Baumgold Dec 14 '12 at 18:58
  • I created [DATAMONGO-593](https://jira.springsource.org/browse/DATAMONGO-593) for this bug. – Ben Baumgold Dec 14 '12 at 23:39
  • This has nothing to do with the object type but the current constructor parameter resolution mechanism not properly massaging the values retrieved through the SpEL expression lookup into the parameter type. So the two tickets probably boil down to the same issue. I'll push the fix for DATAMONGO-592 in a bit an we can then check if that solves the other issue for you as well. – Oliver Drotbohm Dec 16 '12 at 12:51

0 Answers0