Spring Data Mongo: How to return nested object by its field?

二次信任 提交于 2020-04-13 17:55:09

问题


I have domain:

class Company {
    List<Job> jobs;
}

Is there a way to return nested object from collection like:

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

回答1:


I have to make some assumptions about the structure of your Job model, but assuming something like this:

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

... and assuming that this model is embedded in your Company model, and not represented in another collection, you will have to go the custom implementation via MongoTemplate route. The Spring Data query API is not going to be able to figure out how to get what you want, so you must implement the method yourself.

@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:


Yes it is possible, try this:

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);

}

Then you can @Autowire the repositories and invoke the methods:

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("*******************************************");

Checkout the Complete Project in my GitHub repository.



来源:https://stackoverflow.com/questions/39933469/spring-data-mongo-how-to-return-nested-object-by-its-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!