ConversionFailedException: DBObject is being saved but getting returns LinkedHashMap <?,?>

I store the 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 like this:

potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));

      

This persists for my mongodb and gives me an object in the DB that I can deploy to, however, when I try to restore 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 am facing 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

      

I think I need some kind of logic to handle requests from mongo. I could have used a different return class in my request findOne

, but that seems a little messy. Is there a standard approach to this issue?

+3


source to share


1 answer


your error is probably exactly what it says in your exception: a ConversionFailed Exception

caused by someone / something trying to convert from ArrayList

to LinkedHashMap

; but there is no suitable converter ( ConverterNotFoundException

) for this .

where exactly this is happening is impossible to tell since you only posted very little code. I cannot find the string "myString" in your code, but it is mentioned in the error.

Is there a standard approach to this issue?

spring data usually uses converters in the rendering process. in order to have more control over the mapping process, some people choose to implement and register their own 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

Perhaps this is already enough for you to fix the error yourself.

Edit: short comment on this line:

potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));

you pass DBObject before calling setter because setter takes DBObject. this is bad, you have to create another setter for JSON and cast there, or you will end up doing this casting all over your code; which is 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 may prefer this over inline DBObject.

+5


source







All Articles