当前位置: 首页 > 知识库问答 >
问题:

有没有方法使用通用的SpringDataJPA存储库实现?

杜俊爽
2023-03-14

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

我已经尝试制作所有。*RepositoryQuery接口来扩展。*RepositoryQuery('RepositoryQuery'是我默认的自定义接口名称后缀),然后使用特定于实体的实现这些接口。*RepositoryQueryImpl类。请注意,所有的。*RepositoryQueryImpl类扩展了一个泛型实现类,名为BaseRepositoryQueryImpl

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

下面显示了包含键类和接口的代码:

  1. 我的超级界面
public interface BaseRepositoryQuery<T, PK> {
   public List<T> retrieveByCriteria(T searchCriteria);
   // other methods go here ...
}
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 ...
}
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
public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
  // other methods go here...
}
@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 线程,但没有一个适合此上下文。

共有1个答案

常小白
2023-03-14

请签入BaseRepositoryQuery..您可能使用的字段不正确。这就是它抱怨的原因。

 类似资料:
  • 我有一个Spring Boot应用程序,注释为。几乎所有的存储库都需要实现一些自定义逻辑,这是使用完成的。 是否有方法创建将从机制中排除的存储库?

  • 我正在尝试将通用Jpa规范与Spring启动一起使用,但出现了这个问题。 在我的代码中,我试图使用模块概念,所以我有5个模块(实体、dao、服务、web和前端,带角度),所以这是我的代码: 我的通用Jpa规范接口。 存储库示例。 服务 和控制器

  • 我想使用SpringDataRepository接口来执行本机查询——我认为这种方式最简单,因为复杂性较低。 但是当扩展接口时。

  • 问题内容: 我对包含不带参数的泛型方法的代码感到困惑,所以这种方法的返回泛型类型是什么,例如: 这是通过以下方式调用的: 接口定义为: 我的问题:是否接受字符串,对象和所有内容。那么什么时候将通用返回类型指定为String呢? 问题答案: 编译器从 LHS 分配中使用的具体类型推断出类型。 从此链接: 如果类型参数未出现在方法参数的类型中,则编译器无法通过检查实际方法参数的类型来推断类型参数。如果

  • Firebase存储使用490 MB,但尚未初始化存储桶。我无法跟踪这个存储com从哪里来,但是检查Firebase对一个空存储收取0.10美元的费用是很奇怪的。 我在哪里可以删除此存储?为什么firebase因没有存储桶而收费? 目前我正在使用Firebase身份验证、Firestore、实时数据库、主机和其他功能。

  • 下面的方法由控制器调用,其中是必需的参数,、是可选参数。我必须编写多个方法来根据这些参数获取记录。我想知道有没有一个更好的/有效的方法来重构这个方法,用if/else梯子?