Я настраиваю новый API Spring Boot с более чем 100 объектами (используя JHipster), и мой вопрос: учитывая, что у меня есть набор методов уровня репозитория, я хочу, чтобы все мои репозитории могли вызывать эти методы.

Я уже пытался сделать так, чтобы все интерфейсы .*Repository расширяли .*RepositoryQuery ("RepositoryQuery" — это суффикс имени пользовательского интерфейса по умолчанию), а затем реализовывал эти интерфейсы с помощью класса .*RepositoryQueryImpl, специфичного для объекта. Обратите внимание, что все классы .*RepositoryQueryImpl расширяют класс универсальной реализации с именем BaseRepositoryQueryImpl.

Обратите внимание, что '.*' в заданных регулярных выражениях означает любую сущность в моем постоянном наборе сущностей.

Показанный ниже код с ключевыми классами и интерфейсами:

  1. Мой супер интерфейс
public interface BaseRepositoryQuery<T, PK> {
   public List<T> retrieveByCriteria(T searchCriteria);
   // other methods go here ...
}
  1. Моя супер реализация
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 ...
}
  1. Интерфейс объекта RepositoryQuery:
public interface SomeEntityRepositoryQuery extends BaseRepositoryQuery<SomeEntity, Long> {}
  1. Реализация репозитория сущности:
public class SomeEntityRepositoryQueryImpl extends BaseRepositoryQueryImpl<SomeEntity, Long> implements SomeEntityRepositoryQuery {

 public SomeEntityRepositoryQueryImpl(Class<SomeEntity> businessClass) {
   super(businessClass);
 }

 public SomeEntityRepositoryQueryImpl() {
   super(SomeEntity.class);
 }
}
  1. Интерфейс объекта Repository:
@Repository
public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
  // other methods go here...
}
  1. Затем идея состоит в том, что я бы внедрил некоторый компонент репозитория сущностей, подобный этому (в контроллере или службе Spring):
@Autowired
  private SomeEntityRepository someEntityRepository;

Обратите внимание, что SomeEntity может быть любой сущностью из моего набора постоянных сущностей (извините за такую ​​очевидность). Кроме того, я уже установил свою конфигурацию как:

@Configuration
@EnableJpaRepositories("<my base jpa repositories package here>")

Пока все, что у меня есть (работает maven), - это журнал ошибок:

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

Я подозреваю, что ошибка может быть связана с именованием репозиториев Spring, и я уже пытался искать в других потоках SO, но ни один из них не подходил для этого контекста.

2
Guilherme Smethurst 22 Окт 2019 в 05:33

1 ответ

Пожалуйста, проверьте BaseRepositoryQuery. Возможно, вы используете неправильное поле. Вот почему он жалуется на это.

0
user3340774 22 Окт 2019 в 06:17
Итак, все мои методы должны быть связаны с полями сущностей, верно? Значит, я не мог использовать любое имя для методов интерфейса?
 – 
Guilherme Smethurst
22 Окт 2019 в 20:48