Spring Data Mongo:如何通过其字段返回嵌套对象?

时间:2016-10-08 14:18:59

标签: java spring spring-data spring-data-mongodb

我有域名:

class Company {
    List<Job> jobs;
}

有没有办法从集合中返回嵌套对象,如:

@Repository
public interface CompanyRepository extends MongoRepository<Company, String>{
    Job findByJobId(String jobId);
}

3 个答案:

答案 0 :(得分:5)

是的,有可能,试试这个:

<强> Company.class

@Document
public class Company {

    @Id
    private String id;

    @Field("name")
    private String Name;

    @DBRef
    List<Job> job;

    // Getters and Setters
}

<强> Job.class

@Document
public class Job {

    @Id
    private String id;

    @Field("name")
    private String name;

    // Getters and Setters
}

<强> CompanyRepository.class

public interface CompanyRepository extends MongoRepository<Company, String> {

    Company findOneByJobId(String id);

    List<Company> findByJobId(String id);
}

<强> JobRepository.class

public interface JobRepository extends MongoRepository<Job, String>{

    Job findOneByName(String name);

}

然后你可以@Autowire存储库并调用方法:

Job java = new Job("Core Java Developer");
Job spring = new Job("Spring Web Developer");
Job cSharp = new Job("C# Developer");
Job dotNet = new Job(".Net Web Developer");
List<Job> allJobs = Arrays.asList(java,cSharp,spring, dotNet);
// Save All Jobs
jobRepository.save(allJobs);

// Create Companies
Company oracle = new Company("Oracle", Arrays.asList(java));
Company microsoft = new Company("Microsoft", Arrays.asList(cSharp, dotNet));
Company pivotal = new Company("Pivotal", Arrays.asList(java, spring));
// Save all companies
companyRepository.save(Arrays.asList(oracle,microsoft,pivotal));

// Find job by name - C#
Job cSharpJob = jobRepository.findOneByName("C# Developer");
System.out.println("*******************Found Job by Name************************");
System.out.println(cSharpJob);
System.out.println("*******************************************");

// Find One Company having Job with Job Id - C#
Company companyWithcSharpJob = companyRepository.findOneByJobId(cSharpJob.getId());
System.out.println("********************Company having C# Job found using Job Id: "+ cSharpJob.getId() +"***********************");
System.out.println(companyWithcSharpJob.getName());
System.out.println("*******************************************");
  

检查我的GitHub存储库中的Complete Project

答案 1 :(得分:0)

我必须对你的Job模型的结构做一些假设,但假设是这样的:

public class Job {
    private String id;
    // other attributes and methods
}

...并且假设此模型嵌入在您的Company模型中,而未在其他集合中表示,则必须通过MongoTemplate路由进行自定义实施。 Spring Data查询API无法弄清楚如何获得所需内容,因此您必须自己实现该方法。

@Repository
public interface CompanyRepository extends CompanyOperations, MongoRepository<Company, String>{
} 

public interface CompanyOperations {
    Job findByJobId(String jobId);
}

public class CompanyRepositoryImpl implements CompanyOperations {
    @Autowired private MongoTemplate mongoTemplate;

    @Override
    public Job findByJobId(String jobId){
        Company company = mongoTemplate.findOne(new Query(Criteria.where("jobs.id").is(jobId)), Company.class);
        return company.getJobById(jobId); //implement this method in `Company` and save yourself some trouble.
    }
}

答案 2 :(得分:0)

你必须使用@Query注解在嵌套对象字段的基础上返回数据。
@Repository public interface CompanyRepository extends MongoRepository<Company, String>{ @Query("{'jobs.$jobId' : ?0}") List<Job> findCompanyByJobId(String jobId);}