Spring Data MongoDB:使用Example Executor和Pageable查找所有子对象ID。

huangapple go评论102阅读模式
英文:

Spring data mongoDB : Find all by child object id with Example Executor and Pageable

问题

I am facing issue in querying repository with find all by child object ID, Example and Pageable.

StudentController.java

  1. @RestController
  2. @RequestMapping("/students")
  3. public class StudentController {
  4. @Autowired
  5. private StudentService studentService;
  6. @RequestMapping(method = RequestMethod.GET)
  7. public Page<Student> getStudentsPage(
  8. @PageableDefault(page = 0, size = 10) @SortDefault.SortDefaults({
  9. @SortDefault(sort = "id", direction = Direction.DESC) }) Pageable pageable,
  10. Student student) {
  11. return studentService.getStudentPage(student, pageable);
  12. }
  13. }

StudentService.java

  1. @Service
  2. public class StudentService {
  3. private static final Logger logger = LoggerFactory.getLogger(StudentService.class);
  4. @Autowired
  5. private UserService userService;
  6. @Autowired
  7. private StudentRepository studentRepository;
  8. public Page<Student> getStudentsPage(Student student, Pageable pageable) {
  9. logger.debug("Getting Students: {}, {}", student, pageable);
  10. // Gets organization Id of the student from the session
  11. String organizationId = userService.getUserMe().getOrganization().getId();
  12. Page<Student> studentPage = studentRepository.findAllByOrganizationId(organizationId, Example.of(student), pageable);
  13. logger.debug("Students: {}", studentPage.getContent());
  14. return studentPage;
  15. }
  16. }

StudentRepository.java

  1. @Repository
  2. public interface StudentRepository extends MongoRepository<Student, String> {
  3. Page<Student> findAllByOrganizationId(String organizationId, Example<Student> example, Pageable pageable);
  4. }

Student.java

  1. @Document(collection = "students")
  2. public class Student {
  3. @Id
  4. private String id;
  5. private String firstName;
  6. private String lastName;
  7. @DBRef(db = "organizations")
  8. private Organization organization;
  9. public String getId() {
  10. return id;
  11. }
  12. // ... (other getters and setters)
  13. }
  14. Organization.java
  15. ```java
  16. @Document(collection = "organizations")
  17. public class Organization {
  18. @Id
  19. private String id;
  20. private String name;
  21. // ... (getters and setters)
  22. }

The GET API request:
http://localhost:8080/students?page=0&size=10&sort=id,desc&lastName=xyz

The repository method findAllByOrganizationId(String organizationId, Example<Student> example, Pageable pageable) should return all the records with matching organizationId and lastName=xyz, but it returns the records that match with the organization Id along with all the students irrespective of the lastName.

Is something wrong with the code?

英文:

I am facing issue in querying repository with find all by child object ID, Example and Pageable.

StudentController.java

  1. @RestController
  2. @RequestMapping(&quot;/students&quot;)
  3. public class StudentController {
  4. @Autowired
  5. private StudentService studentService;
  6. @RequestMapping(method = RequestMethod.GET)
  7. public Page&lt;Student&gt; getStudentsPage(
  8. @PageableDefault(page = 0, size = 10) @SortDefault.SortDefaults({
  9. @SortDefault(sort = &quot;id&quot;, direction = Direction.DESC) }) Pageable pageable,
  10. Student student) {
  11. return studentService.getStudentPage(student, pageable);
  12. }
  13. }

StudentService.java

  1. @Service
  2. public class StudentService {
  3. private static final Logger logger = LoggerFactory.getLogger(StudentService.class);
  4. @Autowired
  5. private UserService userService;
  6. @Autowired
  7. private StudentRepository studentRepository;
  8. public Page&lt;Student&gt; getStudentsPage(Student student, Pageable pageable) {
  9. logger.debug(&quot;Getting Students : {}, {}&quot;, student, pageable);
  10. //gets organization Id of the student from the session
  11. String organizationId = userService.getUserMe().getOrganization().getId();
  12. Page&lt;Student&gt; studentPage = studentRepository.findAllByOrganizationId(organizationId, Example.of(student), pageable);
  13. logger.debug(&quot;Students: {}&quot;, studentPage.getContent());
  14. return studentPage;
  15. }
  16. }

StudentRepository.java

  1. @Repository
  2. public interface StudentRepository extends MongoRepository&lt;Student, String&gt; {
  3. Page&lt;Student&gt; findAllByOrganizationId(String organizationId, Example&lt;Student&gt; example, Pageable pageable);
  4. }

Student.java

  1. @Document(collection = &quot;students&quot;)
  2. public class Student {
  3. @Id
  4. private String id;
  5. private String firstName;
  6. private String lastName;
  7. @DBRef(db = &quot;organizations&quot;)
  8. private Organization organization;
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getFirstName() {
  16. return firstName;
  17. }
  18. public void setFirstName(String firstName) {
  19. this.firstName = firstName;
  20. }
  21. public String getLastName() {
  22. return lastName;
  23. }
  24. public void setLastName(String lastName) {
  25. this.lastName = lastName;
  26. }
  27. public Organization getOrganization() {
  28. return organization;
  29. }
  30. public void setOrganization(Organization organization) {
  31. this.organization = organization;
  32. }
  33. @Override
  34. public String toString() {
  35. return &quot;Student [id=&quot; + id + &quot;, firstName=&quot; + firstName + &quot;, lastName=&quot; + lastName + &quot;, organization=&quot;
  36. + organization + &quot;]&quot;;
  37. }
  38. }

Organization.java

  1. @Document(collection = &quot;organizations&quot;)
  2. public class Organization {
  3. @Id
  4. private String id;
  5. private String name;
  6. public String getId() {
  7. return id;
  8. }
  9. public void setId(String id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. @Override
  19. public String toString() {
  20. return &quot;Organization [id=&quot; + id + &quot;, name=&quot; + name + &quot;]&quot;;
  21. }
  22. }

The GET API request:
http://localhost:8080/students?page=0&size=10&sort=id,desc&lastName=xyz

the repository method
Page<Student> findAllByOrganizationId(String organizationId, Example<Student> example, Pageable pageable);
should return all the records with matching organizationId and lastName=xyz, But it returns the records that match with the organization Id along with all the students irrespective of the lastName.

Is something wrong with the code?

答案1

得分: 0

Finally found answer.
findBy and Example Executor doesn't work together.

通过设置子对象的过滤器并按示例执行器进行查询,如下所示。

  1. public Page<Student> getStudentsPage(Student student, Pageable pageable) {
  2. logger.debug("Getting Students : {}, {}", student, pageable);
  3. // 从会话中获取学生的组织Id
  4. String organizationId = userService.getUserMe().getOrganization().getId();
  5. student.setOrganization(new Organization(organizationId));
  6. Page<Student> studentPage = studentRepository.findAll(Example.of(student), pageable);
  7. logger.debug("Students: {}", studentPage.getContent());
  8. return studentPage;
  9. }

这按预期工作!!

谢谢!!

英文:

Finally found answer.
findBy and Example Executor doesn't work together.

By setting the child object filter and query by Example Executor as shown below.

  1. public Page&lt;Student&gt; getStudentsPage(Student student, Pageable pageable) {
  2. logger.debug(&quot;Getting Students : {}, {}&quot;, student, pageable);
  3. //gets organization Id of the student from the session
  4. String organizationId = userService.getUserMe().getOrganization().getId();
  5. student.setOrganization(new Organization(organizationId));
  6. Page&lt;Student&gt; studentPage = studentRepository.findAll(Example.of(student), pageable);
  7. logger.debug(&quot;Students: {}&quot;, studentPage.getContent());
  8. return studentPage;
  9. }

This works as expected!!

Thanks!!

huangapple
  • 本文由 发表于 2020年8月13日 13:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63388842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定