2017-08-02 156 views
0

我想将基于代码JPA batch inserts with Hibernate & Spring Data的自定义批量保存方法添加到我的应用程序中的所有Spring Data JPA存储库。 Spring Data doc explains how this can be done具有如下所示的自定义存储库基类。我的问题是如何在下面的例子中设置batchSize属性。如下所示使用@Value注入不起作用。如何在自定义Spring数据JPA存储库中注入配置

@NoRepositoryBean 
public interface BulkRepository<T, ID extends Serializable> 
    extends JpaRepository<T, ID> { 

    void bulkSave(Iterable<T> entities); 
} 

public class BulkRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BulkRepository<T, ID> { 

    private final EntityManager entityManager; 

    @Value("${spring.jpa.properties.hibernate.jdbc.batch_size}") 
    private int batchSize; 

    public BulkRepositoryImpl(JpaEntityInformation entityInformation, 
          EntityManager entityManager) { 
    super(entityInformation, entityManager); 
    this.entityManager = entityManager; 
    } 

    public void bulkSave(Iterable<T> entities) { 
    // implementation using batchSize goes here 
    } 
} 

我必须使用自定义JpaRepositoryFactoryBean建立一个配置BulkRepositoryImpl?还是有更直接的方法?

+0

你如何设置“spring.jpa.properties.hibernate.jdbc.batch_size”值? – chomnoue

+0

在application.properties中设置“spring.jpa.properties.hibernate.jdbc.batch_size”。我的问题是如何从那里获取值到BulkRepositoryImpl实例。 –

+0

你得到的错误是什么? –

回答

1

我遇到了完全相同的问题,找不到任何方法来定义我自己的JpaRepositoryFactoryBean类。似乎自定义存储库基类的依赖关系不会像它们在标准bean中那样自动注入(请参阅herehere)。另外,创建存储库接口实例时,缺省JpaRepositoryFactory仅将JpaEntityInformationEntityManager的实例传递给类构造函数(请参阅here)。就我所知,这可以有效地防止您为扩展SimpleJpaRepository的类添加其他依赖项。

我结束了定义工厂定制方式如下:

@Configuration 
@ConfigurationProperties(prefix = "spring.jpa.properties.hibernate.jdbc") 
public class RepositoryConfiguration { 
    private int batchSize; 
} 

public class MyCustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> { 

    private RepositoryConfiguration repositoryConfiguration; 

    public MyCustomRepositoryFactoryBean(Class<? extends R> repositoryInterface, RepositoryConfiguration repositoryConfiguration) { 
     super(repositoryInterface); 
     this.repositoryConfiguration = repositoryConfiguration; 
    } 

    @Override 
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 
     return new MyCustomRepositoryFactory(entityManager, repositoryConfiguration); 
    } 

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

     private RepositoryConfiguration repositoryConfiguration; 

     MyCustomRepositoryFactory(EntityManager entityManager, RepositoryConfiguration repositoryConfiguration) { 
      super(entityManager); 
      this.repositoryConfiguration = repositoryConfiguration; 
     } 

     @Override 
     @SuppressWarnings("unchecked") 
     protected SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, 
       EntityManager entityManager) { 

      JpaEntityInformation<T, ?> entityInformation = 
        (JpaEntityInformation<T, ?>) getEntityInformation(information.getDomainType()); 

      return new MyCustomRepositoryImpl<T, I>(
        entityInformation, 
        entityManager, 
        repositoryConfiguration); 
     } 

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

虽然无法与MyCustomRepositoryFactoryBean要么@Value注入领域,春天解析传递给构造函数的依赖关系,这样你就可以只提供通过一个bean(上面代码中的RepositoryConfiguration)将其属性传递给MyCustomRepositoryImpl。最后,您将需要指示加入

@EnableJpaRepositories(
     repositoryFactoryBeanClass = MyCustomRepositoryFactoryBean.class 
) 

@Configuration注释豆春季数据创建库时使用FactoryBean类。这是很多的样板,但它的工作原理。

N.B.我正在使用spring-data-jpa:1.11.8.RELEASE

相关问题