Spring Jpa 将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库

标签 spring jpa spring-data spring-data-jpa

此处的 Spring 文档 http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations给出了将自定义功能添加到所有存储库或单个存储库的示例,而不是两者。

假设我想将一些自定义函数添加到所有存储库(使用自定义存储库工厂 Bean)和其他一些仅添加到单个存储库(文档说使用自定义接口(interface)和自定义 Impl);我怎样才能做到这一点?

我在所有存储库中添加了“setCurrentTenansInSession”方法的一些示例代码;现在我想添加一个自定义方法,例如“newCustomMethod”,到单个存储库(即 MyJpaRepository,对于我的自定义存储库工厂)。我该怎么做呢?

自定义行为接口(interface):

@NoRepositoryBean
public interface MyJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 
    public void setCurrentTenantInSession(Object object);       
}

自定义行为实现:
public class MultiTenantSimpleJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyJpaRepository<T, ID> {
    public void setCurrentTenantInSession(Object object) {
        //custom impl
    }
}

自定义存储库工厂 bean:
public class MultiTenantJpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> {

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new MultiTenantJpaRepositoryFactory(entityManager);
    }
}

最后是自定义存储库工厂:
public class MultiTenantJpaRepositoryFactory extends JpaRepositoryFactory {
    public MultiTenantJpaRepositoryFactory(EntityManager entityManager) {
        super(entityManager);
    }

    @Override
    protected JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) {
        final JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());

        final SimpleJpaRepository<?, ?> repo = new MultiTenantSimpleJpaRepository(entityInformation, entityManager);

        repo.setLockMetadataProvider(LockModeRepositoryPostProcessor.INSTANCE.getLockMetadataProvider());
        return repo;
    }

    @Override
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
        return MultiTenantSimpleJpaRepository.class;
    }
}

最佳答案

您只需要结合您提到的文档页面上的方法。让 Car成为您想要为其拥有自定义存储库的实体。
CommonCustomRepository定义添加到所有 repos 的方法:

@NoRepositoryBean
public interface CommonCustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
    String getCustomValue();
}

这个 repo 的实现:
public class CommonCustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements CommonCustomRepository<T, ID> {

    public CommonCustomRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
    }

    public CommonCustomRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
            EntityManager entityManager) {
        super(entityInformation, entityManager);
    }

    @Override
    public String getCustomValue() {
        return "CustomValue";
    }

}
CarRepository 的自定义方法
@NoRepositoryBean
public interface CustomCarRepository {

    public String getCustomCarValue();
}

定制车相关方法的实现
public class CarRepositoryImpl implements CustomCarRepository {

    @PersistenceContext
    private EntityManager em;

    @Override
    public String getCustomCarValue() {
        return "CustomCarValue";
    }
}
CarRepository 的组合接口(interface)
public interface CarRepository extends CommonCustomRepository<Car, Long>, CustomCarRepository {
}

自定义仓库工厂,就像在文档中一样
public class CustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends
    JpaRepositoryFactoryBean<R, T, I> {

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

        return new CustomRepositoryFactory(entityManager);
    }

    private static class CustomRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

        private EntityManager entityManager;

        public CustomRepositoryFactory(EntityManager entityManager) {
            super(entityManager);

            this.entityManager = entityManager;
        }

        @Override
        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return new CommonCustomRepositoryImpl<T, I>((Class<T>) 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;
        }
    }
}

最后一点配置,就像在文档中一样
<jpa:repositories base-package="com.example" factory-class="com.example.CustomRepositoryFactoryBean"/>

关于Spring Jpa 将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765684/

相关文章:

spring-boot - 使用 ssl 在 AWS 上使用 Spring-boot-starter-data-redis 启动错误

java - 从静态 block 或方法初始化静态最终 int 并在注释中使用它

java - JPA参照完整性约束违反oneToMany和批量操作查询

java - 是否可以(如何)在使用 spring 数据的同一项目上使用不同的 jdbc 驱动程序?

java - 尝试在 SQL 查询中附加两个字符串时出现 JPQL 词法错误

java - 我可以实现自己的 JPA 吗?

java - 自定义 StoredProcedure 类作为 Spring 组件并扩展 jdbc StoredProcedure

java - mockito 测试对抽象执行器的调用

java - spring java方法是否通过@Value字符串文字获取字段?

java - 如何使用 AspectJ 设置 springframework @Transactional