Results 1 to 4 of 4

Thread: Call findAll from custom repository class

  1. #1
    Join Date
    Mar 2007
    Posts
    4

    Default 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. #2
    Join Date
    Mar 2007
    Posts
    4

    Default

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

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Default

    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-

  4. #4
    Join Date
    Dec 2012
    Posts
    1

    Default

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •