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?
Printable View
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?
To answer my own question, it works to inject the standard repository object with an Autowired field in the custom repository class.
My base repo interface looks something like this:
My custom extension to the base looks something like so:Code:@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String>,
QueryDslPredicateExecutor<Employee>,
EmployeeRepositoryCustom {
Page <Employee> findByOrgId(String orgId, Pageable pageable);
}
Like you, in my implementation of EmployeeRepositoryCustom, I'd like to use support from EmployeeRepository.Code:public interface EmployeeRepositoryCustom {
Page<Employee> findByGroupId(EmployeeGroup employeeGroup, Pageable 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).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);
}
}
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-
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:
By injecting the Spring application context you can get a reference to the repository at runtime, which is essentially a reference to itself.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);
}
}
I'd love to know if there is a better solution.