ConversionFailedException:持久化DBObject但检索返回LinkedHashMap <?,?>

Wil*_*ill 3 java jdbc mongodb

我坚持一个对象:

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

其中,potentialCandidates从一个JSON字符串作为这样设置:

potentialCandidatesObj.setPotentialcandidates((DBObject)JSON.parse(valStr));
Run Code Online (Sandbox Code Playgroud)

这对我的mongodb很好,并且在我可以向下钻取的DB上给我一个对象,但是当我尝试检索我的db对象时:

    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..");
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

我遇到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

所以我似乎需要某种逻辑来处理来自mongo的检索.我可以在findOne查询中使用不同的返回类,但这看起来有点混乱.有没有一个标准的方法来处理这个?

Sei*_*oid 5

你的错误可能正是它在你的异常中所说的:ConversionFailed Exception由某人/某事物试图转换ArrayList为a引起的错误LinkedHashMap; 但是那个(ConverterNotFoundException)没有合适的转换器.

究竟发生了这种情况是不可能的,因为你只发布了很少的代码.我在你的代码中找不到字符串"myString",但错误中提到了它.

有没有一个标准的方法来处理这个?

spring数据通常在映射过程中使用转换器.为了更好地控制映射过程,有些人更喜欢为他们的类实现和注册自定义转换器.

你可以在这里阅读转换器

http://docs.spring.io/spring-data/data-mongo/docs/current/reference/html/mongo.core.html#mongo.custom-converters

和这里

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#core-convert

也许这已经足够你自己修复错误了.

编辑:关于此行的简短评论:

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

您在调用setter之前转换为DBObject,因为setter采用DBObject.这很糟糕,您应该为JSON创建另一个setter并在那里进行转换,否则您最终会在代码中的任何位置执行该转换操作; 那不是很干.

在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更喜欢它.