I am persisting a object:
@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;
}
}
where potentialCandidates are set from a JSON string as so:
potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));
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..");
}
}
}
I encounter the following error:
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
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?
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).
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 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
http://docs.spring.io/spring-data/data-mongo/docs/current/reference/html/mongo.core.html#mongo.custom-converters
and here
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#core-convert
maybe this will already be enough for you to fix the error yourself.
Edit: a short comment about this line:
potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With