I'm setting up a new Spring Boot API with 100+ entities (using JHipster) and my question is: given I have a set of repository-layer methods I want all my repositories to be able to call these methods.
I've already tried to make all .*Repository interfaces to extend .*RepositoryQuery ('RepositoryQuery' is my default custom interface name suffix), then implemented those interfaces with an entity-specific .*RepositoryQueryImpl class. Note that all the .*RepositoryQueryImpl classes extends a generic-implementation class, named BaseRepositoryQueryImpl.
Please note that the '.*' in given regexes stands for any entity in my persistent entity set.
Shown code below with key classes and interfaces:
- My Super interface
public interface BaseRepositoryQuery<T, PK> {
public List<T> retrieveByCriteria(T searchCriteria);
// other methods go here ...
}
- My Super implementation
public class BaseRepositoryQueryImpl<T, PK> implements BaseRepositoryQuery<T, PK> {
@PersistenceContext
private EntityManager em;
private Class<T> businessClass;
protected BaseRepositoryQueryImpl(Class<T> businessClass) {
this.businessClass = businessClass;
}
public List<T> retrieveByCriteria(T searchCriteria) {
// ...
}
// other methods go here ...
}
- An entity's
RepositoryQueryinterface:
public interface SomeEntityRepositoryQuery extends BaseRepositoryQuery<SomeEntity, Long> {}
- An entity's repository implementation:
public class SomeEntityRepositoryQueryImpl extends BaseRepositoryQueryImpl<SomeEntity, Long> implements SomeEntityRepositoryQuery {
public SomeEntityRepositoryQueryImpl(Class<SomeEntity> businessClass) {
super(businessClass);
}
public SomeEntityRepositoryQueryImpl() {
super(SomeEntity.class);
}
}
- An entity
Repositoryinterface:
@Repository
public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
// other methods go here...
}
- Then, the idea is that I'd inject some entity repository bean like this (in an Spring controller or service):
@Autowired
private SomeEntityRepository someEntityRepository;
Please note that 'SomeEntity' could be any entity of my set of persistent entities (sorry for being so obvious). Furthermore, I've already set my configuration as:
@Configuration
@EnableJpaRepositories("<my base jpa repositories package here>")
So far, all I've got (running maven) is an error log:
... bunch of lines here...
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'agentServicesImpl': Unsatisfied dependency expressed through field 'agentRepository';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'agentRepository':
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object
br.ufpa.labes.spm.repository.interfaces.BaseRepositoryQuery.retrieveBySecondaryKey(java.lang.String)! No property retrieveBySecondaryKey found for type Agent!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at br.ufpa.labes.spm.SpmApp.main(SpmApp.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
... here too ...
I suspect that the error could be related to Spring repositories naming and I've already tried to look in other SO threads, but none that fit in this context.