ConversionFailedException:保留DBObject但检索返回LinkedHashMap <?,?>. [英] ConversionFailedException: Persisting a DBObject but retrieving returns a LinkedHashMap<?, ?>
问题描述
我要保留一个对象:
@Document
public class PotentialCandidates {
@Id
private String jobid;
@CreatedDate
private DateTime created;
@LastModifiedDate
private DateTime modified;
private DBObject potentialcandidates;
public String getJobid() {
return this.jobid;
}
public void setJobid(String jobid) {
this.jobid = jobid;
}
public DBObject getPotentialcandidates() {
return this.potentialcandidates;
}
public void setPotentialcandidates(DBObject potentialcandidates) {
this.potentialcandidates = potentialcandidates;
}
}
其中potentialCandidates
是通过JSON字符串设置的,如下所示:
where potentialCandidates
are set from a JSON string as so:
potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));
这对于我的mongodb仍然有效,并在我可以深入到的DB上给了我一个对象,但是当我尝试检索我的db对象时:
This persists fine to my mongodb and gives me an object on the DB I can drill down into, however when I try to retrieve my db object:
public PotentialCandidates getPotentialCandidatesByJobid(String jobid) throws NoSuchPotentialCandidatesException , SystemException{
PotentialCandidates Jobid = null;
try {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(jobid));
Jobid = mongoTemplateJobs.findOne(query, PotentialCandidates.class,
COLLECTION_NAME);
return Jobid;
} catch (Exception ex) {
throw new SystemException(ex);
} finally {
if (Jobid == null) {
throw new NoSuchPotentialCandidatesException("No User with jobid: "
+ jobid + "found..");
}
}
}
我遇到以下错误:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.util.ArrayList<?> to type com.mongodb.DBObject for value 'myString'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.util.LinkedHashMap<?, ?> to type com.mongodb.DBObject
因此,似乎我需要某种逻辑来处理从mongo进行的检索.我可以在findOne
查询中使用其他返回类,但这似乎有些混乱.有标准的处理方法吗?
So it would seem I need some sort of logic to handle retrieves from mongo. I could use a different return class in my findOne
query but that seems a little messy. Is there a standard approach to dealing with this?
推荐答案
您的错误可能恰恰是您在例外情况中所说的:ConversionFailed Exception
是由某人/某人试图将ArrayList
转换为LinkedHashMap
引起的;但是没有适合的转换器(ConverterNotFoundException
).
your error is probably exactly what it says in your exception: a ConversionFailed Exception
caused by someone/something trying to convert from ArrayList
to a LinkedHashMap
; but there is just no fitting converter for that (ConverterNotFoundException
).
确切地说这是发生在哪里,因为您只发布了很少的代码.我在您的代码中找不到字符串"myString",但在错误中提到了它.
where exactly this is happening is impossible to say since you only posted very little code. i can not find the String "myString" in your code, yet it is mentioned in the error.
有没有标准的方法来处理这个问题?
Is there a standard approach to dealing with this?
spring数据通常在其映射过程中使用转换器.为了更好地控制映射过程,有些人更喜欢为其类实现和注册自定义转换器.
spring data usually uses converters in its mapping process. to have more control over the mapping process some people prefer to implement and register a custom converter for their classes.
您可以在此处了解有关转换器的信息
you can read about converters here
在这里
也许这已经足够您自己修复错误.
maybe this will already be enough for you to fix the error yourself.
关于此行的简短评论:
potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));
您将在调用setter之前强制转换为DBObject,因为setter需要一个DBObject.这很不好,您应该为JSON创建另一个setter并在那里进行转换,否则最终将在代码中的任何地方执行该转换操作;那不是很干.
you are casting to DBObject before calling the setter, because the setter takes a DBObject. this is bad, you should create another setter for JSON and do the casting there, or you will end up doing that casting operation everywhere in your code; that's not very DRY.
spring数据中还有一个叫做DBRefs的东西:
The mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.
您可能更喜欢嵌入式DBObject.
there is also something called DBRefs in spring data:
The mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.
you might prefer this over a embedded DBObject.
这篇关于ConversionFailedException:保留DBObject但检索返回LinkedHashMap <?,?>.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!