Java 持久化到Mongo数据库时出现NumberFormatException
我正在创建一个简单的Java 持久化到Mongo数据库时出现NumberFormatException,java,exception,morphia,Java,Exception,Morphia,我正在创建一个简单的VO对象,然后尝试将其持久化到Mongo数据库中。我得到NumberFormatException最糟糕的部分是,由于我现在没有设置对象的任何属性,所以没有从对象本身抛出异常,这个异常正在杀死我,并且不确定如何处理它 下面是引发异常的代码片段: public void testAgenda(){ ItemVO item = new ItemVO(); try { item.persist(); } catch (NumberFormat
VO
对象,然后尝试将其持久化到Mongo数据库中。我得到NumberFormatException
最糟糕的部分是,由于我现在没有设置对象的任何属性,所以没有从对象本身抛出异常,这个异常正在杀死我,并且不确定如何处理它
下面是引发异常的代码片段:
public void testAgenda(){
ItemVO item = new ItemVO();
try {
item.persist();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getCause();
e.getMessage();
}
以下是例外情况:
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 class ItemVO {
@Id
private Long id;
private String name;
private double marketCap;
private Long analystId;
private Date dateAdded;
private boolean onResearch;
}
似乎
这个
不是可以转换成数字的东西。从Stacktrace的外观来看,这是因为它是十六进制的,无论转换什么,它都在寻找十进制。你知道它在试图转换什么吗?看起来好像这个
不是可以转换成数字的东西。从Stacktrace的外观来看,这是因为它是十六进制的,无论转换什么,它都在寻找十进制。你知道它试图转换什么吗?如果我不得不猜测的话,我会说它是自动生成的Id
字段,因为你有一个@Id
注释,并且该字段的类型需要不是Long
只需对morphia文档进行润色,如果您希望Mongo自动生成Id字段,或者您必须自己设置它,那么您必须将Id字段定义为类型ObjectId
如果非猜不可,我会说是自动生成的
Id
字段,因为这里有一个@Id
注释,并且该字段的类型必须不是长
只需对morphia文档进行润色,如果您希望Mongo自动生成Id字段,或者您必须自己设置它,那么您必须将Id字段定义为类型ObjectId
如果要使用长值,则需要在保存之前分配id字段 如果要使用自动生成的标识符,请在id字段中使用ObjectId类型
如果没有为@Id字段设置Long值,那么服务器将为该字段创建ObjectId,并且它与类中声明的Long数据类型不兼容。因此,当您阅读文档/实体时,它会抱怨数据类型转换错误。如果要使用长值,则需要在保存之前分配id字段 如果要使用自动生成的标识符,请在id字段中使用ObjectId类型
如果没有为@Id字段设置Long值,那么服务器将为该字段创建ObjectId,并且它与类中声明的Long数据类型不兼容。因此,当您阅读文档/实体时,它会抱怨数据类型转换错误。由于id是自动生成的,所以id字段应该设置一些数字或将数据类型更改为字符串。由于id是自动生成的,所以id字段应该设置一些数字或将数据类型更改为字符串。我没有关于Mongo/Morphia部分的想法,但您似乎试图持久化一个
String
字段,该字段在数据库列中保存一个只能保存数值的十六进制字符串。不管是十六进制还是非十六进制,字符串都不是数字。你能发布ItemVO
类定义吗?我想看看所有的字段、方法和注释。@BalusC-你能详细解释一下你的解释吗。看起来你正在生成一种与预期不同的密钥。@BalusC-他正在使用MongoDB,一种文档存储;没有“列”或数据类型之类的东西。我不知道Mongo/Morphia部分,但您似乎试图持久化一个String
字段,该字段在数据库列中保存一个只能保存数值的十六进制字符串。不管是十六进制还是非十六进制,字符串都不是数字。你能发布ItemVO
类定义吗?我想看看所有的字段、方法和注释。@BalusC-你能详细解释一下你的解释吗。看起来你正在生成一种与预期不同的密钥。@BalusC-他正在使用MongoDB,一种文档存储;没有“列”或数据类型之类的东西。我不确定我是否正确理解了你。我理解了。您收到一个数字格式异常,这是由于此
没有在整数中设置正确的字段,而是在十六进制中设置。在我的ItemVO中,我没有将任何内容设置为十六进制,但所有内容都是字符串或双精度。我不确定是否正确理解了您的意思。我有。您得到一个数字格式异常,这是由于此
没有在整数中设置正确的字段,而是在十六进制中设置。在我的ItemVO中,我没有将任何内容设置为十六进制,但所有内容都是字符串或双精度。是的,它是在ObjectId中定义的,但是NumberFormatException呢?@Rachel--修改ItemVO对象,将ObjectId用作id成员变量的字段类型,而不是Long。你的意思是,如果使用ObjectId,则无法编译它?是的,这是正确的,现在,我的vo类正在扩展基类,该基类具有以下注释,Id JsonIgnore EqualsInclude protected ObjectId\u Id;好啊如果它已经在基类中并且基类已经编译了,那么我看不出问题出在哪里。你可以把身份证一笔勾销。或者,您可以显式导入org.bson.types.ObjectId;是的,它是在ObjectId中定义的,但是NumberFormatException呢?@Rachel--修改您的ItemVO对象,将ObjectId用作id成员变量的字段类型,而不是Long。您的意思是,如果使用ObjectId,您无法编译它吗?是的,这是正确的,现在我的vo类正在扩展基类,它具有以下注释,Id JsonIgnore equals包括受保护的ObjectId\u Id;好啊如果它已经在基类中并且基类已经编译了,那么我看不出问题出在哪里。你可以把身份证一笔勾销。或者,您可以显式导入org.bson.types.ObjectId;我编辑了Scott的答案,为您提供了有关使用ObjectId
类型的更多详细信息
public class ItemVO {
@Id
private Long id;
private String name;
private double marketCap;
private Long analystId;
private Date dateAdded;
private boolean onResearch;
}