简体   繁体   English

有没有办法使用通用的 Spring 数据 JPA 存储库实现?

[英]Is there a way to use a generic Spring Data JPA repository implementation?

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.我正在设置一个新的 Spring Boot API 与 100 多个实体(使用JHipster ),我的问题是:鉴于我有一组存储库层方法,我希望我的所有存储库都能够调用这些方法。

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.我已经尝试让所有.*Repository接口扩展.*RepositoryQuery ('RepositoryQuery' 是我的默认自定义接口名称后缀),然后使用特定于实体的.*RepositoryQueryImpl class 实现这些接口。 Note that all the .*RepositoryQueryImpl classes extends a generic-implementation class, named BaseRepositoryQueryImpl .请注意,所有.*RepositoryQueryImpl类都扩展了一个名为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:下面显示的代码带有关键类和接口:

  1. My Super interface我的超级界面
public interface BaseRepositoryQuery<T, PK> {
   public List<T> retrieveByCriteria(T searchCriteria);
   // other methods go here ...
}
  1. 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 ...
}
  1. An entity's RepositoryQuery interface:实体的RepositoryQuery接口:
public interface SomeEntityRepositoryQuery extends BaseRepositoryQuery<SomeEntity, Long> {}
  1. 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);
 }
}
  1. An entity Repository interface:实体Repository接口:
@Repository
public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
  // other methods go here...
}
  1. Then, the idea is that I'd inject some entity repository bean like this (in an Spring controller or service):然后,我的想法是我会注入一些这样的实体存储库 bean(在 Spring controller 或服务中):
@Autowired
  private SomeEntityRepository someEntityRepository;

Please note that 'SomeEntity' could be any entity of my set of persistent entities (sorry for being so obvious).请注意,“SomeEntity”可以是我的一组持久实体中的任何实体(抱歉这么明显)。 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:到目前为止,我所拥有的(运行 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 ...

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.我怀疑该错误可能与 Spring 存储库命名有关,我已经尝试查看其他 SO 线程,但没有一个适合这种情况。

Please check in BaseRepositoryQuery.. incorrect field you might be using.请检查 BaseRepositoryQuery.. 您可能使用的字段不正确。 That's why its complaining about that.这就是它抱怨的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM