我創建一個簡單的VO
對象,然後試圖將其持久化到Mongo數據庫中得到NumberFormatException
最糟糕的部分是,異常不是從對象本身拋出,因爲現在沒有設置任何屬性的對象,這個例外是殺了我,我不知道如何處理它。NumberFormatException持續到Mongo數據庫
下面是一段代碼,拋出異常:
public void testAgenda(){
ItemVO item = new ItemVO();
try {
item.persist();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getCause();
e.getMessage();
}
這裏是例外strace的:
java.lang.RuntimeException: java.lang.NumberFormatException: For input string: "4e3c3da5fbb7d7b41ce9e394" at com.google.code.morphia.mapping.Mapper.updateKeyInfo(Mapper.java:194) at com.google.code.morphia.DatastoreImpl.postSaveOperations(DatastoreImpl.java:742) at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:645) at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:685) at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:679) at com.williamblair.im.research.domain.vo.BaseDocument.persist(BaseDocument.java:68) at com.williamblair.im.research.service.TestListService.testGetResearchAgenda_aroundBody2(TestListService.java:31) at com.williamblair.im.research.service.TestListService$AjcClosure3.run(TestListService.java:1) at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149) at com.williamblair.im.research.system.aop.profiling.ProfilingAspect.doProfiling(ProfilingAspect.java:36) at com.williamblair.im.research.service.TestListService.testGetResearchAgenda(TestListService.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) 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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 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.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:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 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: java.lang.NumberFormatException: For input string: "4e3c3da5fbb7d7b41ce9e394" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Long.parseLong(Long.java:419) at java.lang.Long.parseLong(Long.java:468) at com.google.code.morphia.converters.LongConverter.decode(LongConverter.java:22) at com.google.code.morphia.converters.TypeConverter.decode(TypeConverter.java:45) at com.google.code.morphia.converters.DefaultConverters.decode(DefaultConverters.java:144) at com.google.code.morphia.mapping.Mapper.setIdValue(Mapper.java:390) at com.google.code.morphia.mapping.Mapper.updateKeyInfo(Mapper.java:174) ... 39 more
我一直在調試這個問題了一會兒,還沒有發現還有什麼建議嗎?
這裏是persist方法:
public void persist() {
this.morphiaDatastore.save(this);
}
更新
public class ItemVO {
@Id
private Long id;
private String name;
private double marketCap;
private Long analystId;
private Date dateAdded;
private boolean onResearch;
}
我不知道Mongo/Morphia部分,但你似乎試圖堅持一個字符串字段,該字段在只能保存數字值的數據庫列中包含一個十六進制字符串。十六進制或不是,字符串不是數字。 – BalusC
你可以發佈'ItemVO'類的定義嗎?我希望看到所有的字段,方法和註釋。 –
@BalusC - 你能詳細解釋一下你的解釋嗎? – Rachel