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

时间:2014-08-26 17:29:28

标签: java mongodb jdbc

我坚持一个对象:

@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字符串设置为:

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

这对我的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..");
            }
        }
}

我遇到以下错误:

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查询中使用不同的返回类,但这看起来有点混乱。有没有一种标准的方法来解决这个问题?

1 个答案:

答案 0 :(得分:5)

您的错误可能与您在例外中的错误完全相同:ConversionFailed Exception由某人/某物尝试从ArrayList转换为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。