Spring Jpa adding custom functionality to all repositories and at the same time other custom funcs to a single repository

后端 未结 1 1232
庸人自扰
庸人自扰 2020-12-16 23:35

Spring documentation here http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations gives example to add c

1条回答
  •  一整个雨季
    2020-12-16 23:56

    You just need to combine the approaches on the doc page you mentioned. Let Car be the entity you want to have a custom repository for.

    CommonCustomRepository defines the methods added to all repos:

    @NoRepositoryBean
    public interface CommonCustomRepository extends JpaRepository {
        String getCustomValue();
    }
    

    The implementation for this repo:

    public class CommonCustomRepositoryImpl extends SimpleJpaRepository implements CommonCustomRepository {
    
        public CommonCustomRepositoryImpl(Class domainClass, EntityManager em) {
            super(domainClass, em);
        }
    
        public CommonCustomRepositoryImpl(JpaEntityInformation entityInformation,
                EntityManager entityManager) {
            super(entityInformation, entityManager);
        }
    
        @Override
        public String getCustomValue() {
            return "CustomValue";
        }
    
    }
    

    Custom methods for CarRepository

    @NoRepositoryBean
    public interface CustomCarRepository {
    
        public String getCustomCarValue();
    }
    

    Implementation of the custom car-related methods

    public class CarRepositoryImpl implements CustomCarRepository {
    
        @PersistenceContext
        private EntityManager em;
    
        @Override
        public String getCustomCarValue() {
            return "CustomCarValue";
        }
    }
    

    The combined interface for CarRepository

    public interface CarRepository extends CommonCustomRepository, CustomCarRepository {
    }
    

    Custom repo factory, just like in the documentation

    public class CustomRepositoryFactoryBean, T, I extends Serializable> extends
        JpaRepositoryFactoryBean {
    
        @Override
        protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
    
            return new CustomRepositoryFactory(entityManager);
        }
    
        private static class CustomRepositoryFactory extends JpaRepositoryFactory {
    
            private EntityManager entityManager;
    
            public CustomRepositoryFactory(EntityManager entityManager) {
                super(entityManager);
    
                this.entityManager = entityManager;
            }
    
            @Override
            protected Object getTargetRepository(RepositoryMetadata metadata) {
    
                return new CommonCustomRepositoryImpl((Class) metadata.getDomainType(), entityManager);
            }
    
            @Override
            protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {
    
                // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
                // to check for QueryDslJpaRepository's which is out of scope.
                return CommonCustomRepositoryImpl.class;
            }
        }
    }
    

    The final bit of configuration, just like in the docs

    
    

    0 讨论(0)
提交回复
热议问题