java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

Spring JIRA | Tyler Van Gorder | 6 months ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    I am upgrading our software to use Spring Boot 1.4 + Spring 4.3. As part of this upgrade, we are also using Spring Data Mongo 1.9.2.RELEASE. I am assuming that is 1.9.2 (Hopper SR2)? We have a situation where we have a model object that looks like the following: {code:java} Map<Integer, Map<Platform, String>> descriptions = new HashMap<>(); {code} Where "Platform is Enum", although this is not the core issue. When we execute the following: {code:java} public void setAlternateDescriptionMap(int compositeId, Map<Integer, Map<Platform, String>> alternateDescriptionsMap) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(compositeId)); Update update = new Update(); update.set("alternateDescriptionMap", alternateDescriptionsMap); coreMongoTemplate.updateFirst(query, update, "product"); } {code} We end up getting the following exception: {noformat} java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:222) at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:199) at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:223) at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:199) at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:131) at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:62) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) at com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:253) at com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:205) at com.mongodb.connection.UpdateMessage.encodeMessageBodyWithMetadata(UpdateMessage.java:80) at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160) at com.mongodb.connection.WriteProtocol.execute(WriteProtocol.java:89) at com.mongodb.connection.UpdateProtocol.execute(UpdateProtocol.java:67) at com.mongodb.connection.UpdateProtocol.execute(UpdateProtocol.java:42) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168) at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289) at com.mongodb.connection.DefaultServerConnection.update(DefaultServerConnection.java:88) at com.mongodb.operation.UpdateOperation.executeProtocol(UpdateOperation.java:66) at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:144) at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:134) at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:232) at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:223) at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:134) at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:61) at com.mongodb.Mongo.execute(Mongo.java:827) at com.mongodb.Mongo$2.execute(Mongo.java:810) at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333) at com.mongodb.DBCollection.updateImpl(DBCollection.java:495) at com.mongodb.DBCollection.update(DBCollection.java:455) at com.mongodb.DBCollection.update(DBCollection.java:432) at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1153) at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1132) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:462) at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:1132) at org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:1110) at com.build.dao.product.ProductStorageDaoImpl.setAlternateDescriptionMap(ProductStorageDaoImpl.java:1170) at com.build.dao.product.ProductStorageDaoImpl$$FastClassBySpringCGLIB$$4e03147e.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) at com.build.dao.product.ProductStorageDaoImpl$$EnhancerBySpringCGLIB$$8602f8b4.setAlternateDescriptionMap(<generated>) at com.build.dao.product.ProductStorageDaoIT.testSaveAlternateDescriptionsToCacheAndFetch(ProductStorageDaoIT.java:335) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) {noformat} We do not get this exception in spring-data-mongodb-1.9.1-RELEASE. After a lot of debugging, It appears to be related to the code in MappingMongoConverter.convertMongoType(). It looks like the Issue DATAMONGO-1423 may have introduced this issue: https://github.com/spring-projects/spring-data-mongodb/commit/0e60630393980cf2bb4634c8a9c1a5a50407c471 I am going to work on just overriding this default method with a custom mapper. I suspect this code will also break in other cases where the key is mapped into anything other than a string. Let me know if you need any further input.

    Spring JIRA | 6 months ago | Tyler Van Gorder
    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

    Root Cause Analysis

    1. java.lang.ClassCastException

      java.lang.Integer cannot be cast to java.lang.String

      at com.mongodb.DBObjectCodec.encodeMap()
    2. MongoDB Java Driver
      DBCollection.update
      1. com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:222)
      2. com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:199)
      3. com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:223)
      4. com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:199)
      5. com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:131)
      6. com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:62)
      7. org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
      8. org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
      9. com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:253)
      10. com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:205)
      11. com.mongodb.connection.UpdateMessage.encodeMessageBodyWithMetadata(UpdateMessage.java:80)
      12. com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
      13. com.mongodb.connection.WriteProtocol.execute(WriteProtocol.java:89)
      14. com.mongodb.connection.UpdateProtocol.execute(UpdateProtocol.java:67)
      15. com.mongodb.connection.UpdateProtocol.execute(UpdateProtocol.java:42)
      16. com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
      17. com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
      18. com.mongodb.connection.DefaultServerConnection.update(DefaultServerConnection.java:88)
      19. com.mongodb.operation.UpdateOperation.executeProtocol(UpdateOperation.java:66)
      20. com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:144)
      21. com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:134)
      22. com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:232)
      23. com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:223)
      24. com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:134)
      25. com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:61)
      26. com.mongodb.Mongo.execute(Mongo.java:827)
      27. com.mongodb.Mongo$2.execute(Mongo.java:810)
      28. com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333)
      29. com.mongodb.DBCollection.updateImpl(DBCollection.java:495)
      30. com.mongodb.DBCollection.update(DBCollection.java:455)
      31. com.mongodb.DBCollection.update(DBCollection.java:432)
      31 frames
    3. Spring Data MongoDB - Core
      MongoTemplate.updateFirst
      1. org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1153)
      2. org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1132)
      3. org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:462)
      4. org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:1132)
      5. org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:1110)
      5 frames
    4. com.build.dao
      ProductStorageDaoImpl$$FastClassBySpringCGLIB$$4e03147e.invoke
      1. com.build.dao.product.ProductStorageDaoImpl.setAlternateDescriptionMap(ProductStorageDaoImpl.java:1170)
      2. com.build.dao.product.ProductStorageDaoImpl$$FastClassBySpringCGLIB$$4e03147e.invoke(<generated>)
      2 frames
    5. Spring Core
      MethodProxy.invoke
      1. org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
      1 frame
    6. Spring AOP
      ReflectiveMethodInvocation.proceed
      1. org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
      2. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      2 frames
    7. Spring Tx
      PersistenceExceptionTranslationInterceptor.invoke
      1. org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      1 frame
    8. Spring AOP
      CglibAopProxy$DynamicAdvisedInterceptor.intercept
      1. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      2. org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
      2 frames
    9. com.build.dao
      ProductStorageDaoIT.testSaveAlternateDescriptionsToCacheAndFetch
      1. com.build.dao.product.ProductStorageDaoImpl$$EnhancerBySpringCGLIB$$8602f8b4.setAlternateDescriptionMap(<generated>)
      2. com.build.dao.product.ProductStorageDaoIT.testSaveAlternateDescriptionsToCacheAndFetch(ProductStorageDaoIT.java:335)
      2 frames
    10. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      4. java.lang.reflect.Method.invoke(Method.java:497)
      4 frames
    11. JUnit
      InvokeMethod.evaluate
      1. org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      2. org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      3. org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      4. org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      4 frames
    12. Spring TestContext
      SpringRepeat.evaluate
      1. org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      2. org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      3. org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      3 frames
    13. JUnit
      ParentRunner.runLeaf
      1. org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      1 frame
    14. Spring TestContext
      SpringJUnit4ClassRunner.runChild
      1. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
      2. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
      2 frames
    15. JUnit
      ParentRunner$2.evaluate
      1. org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      2. org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      3. org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      4. org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      5. org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      5 frames
    16. Spring TestContext
      RunAfterTestClassCallbacks.evaluate
      1. org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      2. org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      2 frames
    17. JUnit
      ParentRunner.run
      1. org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      1 frame
    18. Spring TestContext
      SpringJUnit4ClassRunner.run
      1. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
      1 frame
    19. JUnit4 Runner
      JUnit4TestReference.run
      1. org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      1 frame
    20. JUnit3 Runner
      RemoteTestRunner.main
      1. org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      2. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      3. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
      4. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      5. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
      5 frames