持久化到Mongo数据库时的NumberFormatException

时间:2011-08-05 19:10:37

标签: java exception morphia

我正在创建一个简单的VO对象,然后尝试将其持久保存到Mongo数据库中{0}最糟糕的部分是异常不会从对象本身抛出,因为现在我没有设置任何属性对象,这个例外是杀了我,我不知道如何处理它。

以下是抛出异常的代码:

NumberFormatException

以下是例外情况:

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

我已经调试了这个问题一段时间了,还没有找到起点,有什么建议吗?

这是持久化方法:

public void testAgenda(){
    ItemVO item = new ItemVO();
    try {
        item.persist();
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        e.getCause();
        e.getMessage();
    }

更新

public void persist() {
this.morphiaDatastore.save(this);
}

4 个答案:

答案 0 :(得分:4)

如果您想使用Long值,则需要在保存之前指定id字段!

如果要使用自动生成的标识符,请在id字段中使用ObjectId类型。

如果没有为@Id字段设置Long值,那么服务器将为该字段创建一个ObjectId,并且它与您在类中声明的Long数据类型不兼容。因此,当您阅读文档/实体时,它会抱怨数据类型转换错误。

答案 1 :(得分:0)

似乎this不是可以转换为数字的东西。从Stacktrace的外观来看,因为它是十六进制的,无论是转换它还是寻找十进制。你知道它试图转换的是什么吗?

答案 2 :(得分:0)

如果我不得不猜测,我会说Id字段是自动生成的,因为那里有@Id注释,并且该字段的类型需要不是{{1 }}

只需浏览morphia文档,如果您希望Mongo自动生成它,您似乎必须将Id字段定义为ObjectId类型,否则您必须自己设置它。

http://code.google.com/p/morphia/wiki/EntityAnnotation

答案 3 :(得分:0)

由于id是自动生成的,id字段应设置为某个数字或将数据类型更改为字符串。