开发者

NumberFormatException when Persisting to Mongo Database

I am creating a simple VO object and then trying to persist it into Mongo Database am getting NumberFormatException worst part is that exception is not being thrown from object itself as right now am not setting any properties of the object, this exception is killing me and am not sure how to deal it.

Here is the piece of code that is throwing exception:

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

Here is the exception 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.开发者_如何学JAVAinternal.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

I have been debugging this issue for a while now and have not found the starting point yet, any suggestions?

Here is the persist method:

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

Update

public class ItemVO {

    @Id
    private Long id;

    private String name;

    private double marketCap;
    private Long analystId;

    private Date dateAdded;

    private boolean onResearch;

}


If you want to use a Long value then you need to assign the id field before saving!

If you want to use autogenerated identifiers, use ObjectId type in the id field.

If you don't set the Long value for the @Id field then the server will create an ObjectId for the field, and it is not compatible with the Long datatype declared in your class. So when you read the document/entity it will complain about the data type conversion error.


It would seem as though this is not something that can be converted to a number. From the look of the Stacktrace it's because it's in Hex and whatever is converting it is looking for decimal. Do you know what it's attempting to convert?


If I had to guess, I'd say its the Id field thats being autogenerated since you have an @Id annotation there and they type of that field needs to be something other than Long.

Just glossing over the morphia documentation, it looks like you have to have the Id field defined as type ObjectId if you want Mongo to autogenerate it or else you have to set it yourself.

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


As the id is auto generated either the id field should be set with some number or change the data type to string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜