最近发现一个让人很纠结的问题,用Mybatis从Mysql数据中将数据读入到Javabean集合里面,然后遍历这个集合想将数据转存到MongoDB,结果发现无法序列化,但如果New一个对象,手工将数据Set进去,又能进行存储,Why?Tel me Why????
注:用的是Spring MongoDB
报错信息如下:
java.lang.IllegalArgumentException: can't serialize class java.lang.Class
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:190)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:142)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
at com.mongodb.DBCollection.update(DBCollection.java:165)
at com.mongodb.DBCollection.save(DBCollection.java:736)
at com.mongodb.DBCollection.save(DBCollection.java:703)
at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:791)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:369)
at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:785)
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:721)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:708)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:704)
at com.minephone.mongodb.common.dao.impl.BaseDaoImpl.save(BaseDaoImpl.java:42)
at com.minephone.community.biz.impl.CountyBizImpl.save(CountyBizImpl.java:26)
at com.minephone.community.biz.impl.CountyBizImpl.save(CountyBizImpl.java:1)
at com.minephone.community.CountyTest.mytest(CountyTest.java:26)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)