gpt4 book ai didi

java - 有没有办法使用通用的 Spring Data JPA 存储库实现?

转载 作者:行者123 更新时间:2023-12-01 22:40:58 25 4
gpt4 key购买 nike

我正在设置一个包含 100 多个实体的新 Spring Boot API(使用 JHipster ),我的问题是:鉴于我有一组存储库层方法,我希望所有存储库都能够调用这些方法.

我已经尝试使所有 .*Repository 接口(interface)扩展 .*RepositoryQuery (“RepositoryQuery”是我的默认自定义接口(interface)名称后缀),然后实现这些与特定于实体的 .*RepositoryQueryImpl 类的接口(interface)。请注意,所有 .*RepositoryQueryImpl 类都扩展了一个名为 BaseRepositoryQueryImpl 的通用实现类。

请注意,给定正则表达式中的“.*”代表我的持久实体集中的任何实体。

下面显示的代码以及关键类和接口(interface):

  1. 我的 super 界面
public interface BaseRepositoryQuery<T, PK> {
public List<T> retrieveByCriteria(T searchCriteria);
// other methods go here ...
}
  • 我的 Super 实现
  • 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 ...
    }
  • 实体的 RepositoryQuery 接口(interface):
  • public interface SomeEntityRepositoryQuery extends BaseRepositoryQuery<SomeEntity, Long> {}
  • 实体的存储库实现:
  • public class SomeEntityRepositoryQueryImpl extends BaseRepositoryQueryImpl<SomeEntity, Long> implements SomeEntityRepositoryQuery {

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

    public SomeEntityRepositoryQueryImpl() {
    super(SomeEntity.class);
    }
    }
  • 实体Repository接口(interface):
  • @Repository
    public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
    // other methods go here...
    }
  • 然后,我的想法是注入(inject)一些像这样的实体存储库 bean(在 Spring Controller 或服务中):
  • @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 线程,但没有一个适合此上下文。

    最佳答案

    请检查 BaseRepositoryQuery..您可能使用的字段不正确。这就是它提示的原因。

    关于java - 有没有办法使用通用的 Spring Data JPA 存储库实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496375/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com