Announcement Announcement Module
Collapse
No announcement yet.
Call findAll from custom repository class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Call findAll from custom repository class

    Is there any way to call a method on the standard repository interface such as findAll from a custom repository class. It seems like composing methods from the standard repository with ones from your custom repository would be a common use case?

  • #2
    To answer my own question, it works to inject the standard repository object with an Autowired field in the custom repository class.

    Comment


    • #3
      My base repo interface looks something like this:

      Code:
      @Repository
      public interface EmployeeRepository extends JpaRepository<Employee, String>,
               QueryDslPredicateExecutor<Employee>, 
               EmployeeRepositoryCustom {
          Page <Employee> findByOrgId(String orgId, Pageable pageable);
      }
      My custom extension to the base looks something like so:

      Code:
      public interface EmployeeRepositoryCustom  {
          Page<Employee> findByGroupId(EmployeeGroup employeeGroup, Pageable pageable);
      }
      Like you, in my implementation of EmployeeRepositoryCustom, I'd like to use support from EmployeeRepository.

      Code:
      @Repository
      public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {
          @PersistenceContext private EntityManager em;  // I get that I can easily use the lower level entity manager
          @Autowired private EmployeeRepository employeeRepository; // but I'd also like to use this
      
          @Override
          public Page<Employee> findByGroupId(EmployeeGroup employeeGroup, Pageable pageable) {
              Predicate predicate = QEmployee.employee.employeeGroups.contains(employeeGroup);
              return employeeRepository.findAll(predicate, pageable);
          }
      }
      But doesn't this introduce a circular reference? Depending on how I fiddle things results in a NullPointerException (happens when I annotate EmployeeRepositoryImpl with @Service) or a BeanCurrentlyInCreationException (happens when I annotate EmployeeRepositoryImpl with @Repository).

      How did you manage to get this working?

      I've had a look at the docs and samples which are very good but don't seem to go over this particular usage. Although... this sample does say if I need to inject something in my custom repo implementation, I'll need to manually do so.

      Thanks,

      -Lee-

      Comment


      • #4
        Originally posted by LeeRead View Post
        But doesn't this introduce a circular reference?

        How did you manage to get this working?
        Yes, it does introduce a circular reference, I have struggled with this issue as well. It does seem like something quite common that you'd want to be able to do but I couldn't find any solution when searching, I have made use of this slightly messy solution that does work:

        Code:
        @Repository
        public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {
        
            @Autowired
            private ApplicationContext applicationContext;
        
            @Override
            public Page<Employee> findByGroupId(EmployeeGroup employeeGroup, Pageable pageable) {
                Predicate predicate = QEmployee.employee.employeeGroups.contains(employeeGroup);
                return applicationContext.getBean(EmployeeRepository.class).findAll(predicate, pageable);
            }
        }
        By injecting the Spring application context you can get a reference to the repository at runtime, which is essentially a reference to itself.

        I'd love to know if there is a better solution.

        Comment

        Working...
        X