简体   繁体   English

Spring Data JPA:使用规范实现自定义存储库行为

[英]Spring Data JPA: Implementing Custom Repository Behavior with Specifications

I would like to create a Spring Data JPA repository with custom behavior, and implement that custom behavior using Specification s. 我想创建一个具有自定义行为的Spring Data JPA存储库,并使用Specification实现该自定义行为。 I have gone through the Spring Data JPA documentation for implementing custom behavior in a single repository to set this up, except there is no example of using a Spring Data Specification from within a custom repository. 我已经通过Spring Data JPA文档在单个存储库中实现自定义行为来设置它,除了没有在自定义存储库中使用Spring Data Specification的示例。 How would one do this, if even possible? 如果可能的话,如何做到这一点?

I do not see a way to inject something into the custom implementation that takes a specification. 我没有看到一种方法将某些内容注入到需要规范的自定义实现中。 I thought I would be tricky and inject the CRUD repository portion of the repository into the custom portion, but that results in a circular instantiation dependency. 我认为我会很棘手,并将存储库的CRUD存储库部分注入自定义部分,但这会导致循环实例化依赖。

I am not using QueryDSL. 我没有使用QueryDSL。 Thanks. 谢谢。

I guess the primary source for inspiration could be how SimpleJpaRepository handles specifications. 我想灵感的主要来源可能是SimpleJpaRepository如何处理规范。 The key spots to have a look at are: 要看的关键点是:

this is not using Specification, so not sure if it's relevant to you, but one way that I was able to inject custom behavior is as follows, 这不是使用规范,所以不确定它是否与您相关,但我能够注入自定义行为的一种方式如下,

  1. Basic structure: as follows 基本结构:如下

    i. 一世。 create a generic interface for the set of entity classes which are modeled after a generic parent entity. 为通用父实体之后建模的实体类集创建通用接口。 Note, this is optional. 注意,这是可选的。 In my case I had a need for this hierarchy, but it's not necessary 在我的情况下,我需要这种层次结构,但没有必要

     public interface GenericRepository<T> { // add any common methods to your entity hierarchy objects, // so that you don't have to repeat them in each of the children entities // since you will be extending from this interface } 

    ii. II。 Extend a specific repository from generic (step 1) and JPARepository as 将特定存储库从泛型(步骤1)和JPARepository扩展为

     public interface MySpecificEntityRepository extends GenericRepository<MySpecificEntity>, JpaRepository<MySpecificEntity, Long> { // add all methods based on column names, entity graphs or JPQL that you would like to // have here in addition to what's offered by JpaRepository } 

    iii. III。 Use the above repository in your service implementation class 在服务实现类中使用上面的存储库

    1. Now, the Service class may look like this, 现在,Service类可能看起来像这样,

       public interface GenericService<T extends GenericEntity, ID extends Serializable> { // add specific methods you want to extend to user } 
    2. The generic implementation class can be as follows, 通用实现类可以如下,

       public abstract class GenericServiceImpl<T extends GenericEntity, J extends JpaRepository<T, Long> & GenericRepository<T>> implements GenericService<T, Long> { // constructor takes in specific repository public GenericServiceImpl(J genericRepository) { // save this to local var } // using the above repository, specific methods are programmed } 
    3. specific implementation class can be 具体的实现类可以

       public class MySpecificEntityServiceImpl extends GenericServiceImpl<MySpecificEntity, MySpecificEntityRepository> implements MySpecificEntityService { // the specific repository is autowired @Autowired public MySpecificEntityServiceImpl(MySpecificEntityRepository genericRepository) { super(genericRepository); this.genericRepository = (MySpecificEntityRepository) genericRepository; } } 

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

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