Java spring与mongoDB DBref无法获取填充的数据

时间:2016-01-11 12:08:59

标签: spring spring-mvc maven-plugin mongodb-java spring-mongodb

我正在尝试在mongo db collection中获取一行,该行具有一个object id数组,它是其他collection中数据行的ObjectID。我想从当前集合中为每个数据记录从ther集合中获取填充的数据行。

我的收藏品就像这样

工作   - ObjectID   - [ObjectIds(候选对象)]

考生 -ObjectID -名称 -age

我正在使用

import org.springframework.data.mongodb.core.mapping.DBRef;

在我的模型类中指示引用。

@Document(collection = "jobcreations")
public class Job {

    @Id
    private String id;

    @DBRef 
    private List<Candidate> _candidates;

    private String jobID;

    public String getjobID() {
        return jobID;
    }

    public void setjobID(String jobid) {
        this.jobID = jobid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public JobCreation() {
        super();
    }


    public List<Candidate> get_candidates() {
        return _candidates;
    }

    public void set_candidates(List<Candidate> _candidates) {
        this._candidates = _candidates;
    }

    public JobCreation(String jobID) {
        super();
        this.jobID = jobID;
    }

    @Override
    public String toString() {
        return "JobCreation [id=" + id + ", _candidates=" + _candidates + ", jobID=" + jobID + "]";
    }   

}

我的候选模特课就像这样

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;


@Document(collection = "candidate")
public class Candidate {

    @Id
    private String _id;

    private String name;

    private int age;


    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

        /**
     * @return the age
     */
    public String getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    public Candidate() {
        super();
        // TODO Auto-generated constructor stub
    }


}

我调用数据库的主要方法是

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

List<JobCreation> listJobCreation = mongoOperation.findAll(JobCreation.class);
    for(JobCreation jc : listJobCreation){
        System.out.println(jc.toString());
    }

我在这里得到错误

Exception in thread "main" org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type com.mkyong.model.CandidateProfile
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:172)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:154)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:673)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:751)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1006)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$100(MappingMongoConverter.java:75)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:957)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:713)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$2.doWithAssociation(MappingMongoConverter.java:257)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:252)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:254)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:212)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:176)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:172)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:75)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1840)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1536)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1057)
at com.mkyong.core.App.main(App.java:29)

请帮我解决这个错误。

提前致谢

0 个答案:

没有答案