有没有一种使用通用Spring Data JPA存储库实现的方法?

时间:2019-10-22 02:33:42

标签: java spring generics spring-data-jpa jhipster

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

我已经尝试使所有.*Repository接口扩展为.*RepositoryQuery(“ RepositoryQuery”是我的默认自定义接口名称后缀),然后使用特定于实体的{{1} }类。请注意,所有.*RepositoryQueryImpl类都扩展了名为.*RepositoryQueryImpl的泛型实现类。

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

下面显示的具有关键类和接口的代码:

  1. 我的超级界面
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 ... } 界面:
RepositoryQuery
  1. 实体的存储库实现:
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); } } 界面:
Repository
  1. 然后,我的想法是要在Spring控制器或服务中注入这样的实体存储库bean:
@Repository
public interface SomeEntityRepository extends SomeEntityRepositoryQuery, JpaRepository<SomeEntity, Long> {
  // other methods go here...
}

请注意,“ SomeEntity”可以是我的一组持久实体中的任何实体(很抱歉,它是如此明显)。此外,我已经将配置设置为:

@Autowired
  private SomeEntityRepository someEntityRepository;

到目前为止,我所拥有的(正在运行的maven)是一个错误日志:

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

我怀疑该错误可能与Spring存储库命名有关,并且我已经尝试查找其他SO线程,但是没有一个适合这种情况。

1 个答案:

答案 0 :(得分:0)

请签入BaseRepositoryQuery ..您可能使用的错误字段。这就是为什么它对此抱怨的原因。