【发布时间】:2018-01-09 18:48:54
【问题描述】:
我想将基于 JPA batch inserts with Hibernate & Spring Data 中的代码的自定义批量保存方法添加到我的应用程序中的所有 Spring Data JPA 存储库。 Spring Data doc explains how this can be done 具有自定义存储库基类,如下所示。我的问题是如何在下面的示例中设置 batchSize 属性。如下所示使用@Value 注入不起作用。
@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?还是有更直接的方法?
【问题讨论】:
-
如何设置“spring.jpa.properties.hibernate.jdbc.batch_size”值?
-
"spring.jpa.properties.hibernate.jdbc.batch_size" 在 application.properties 中设置。我的问题是如何将值从那里获取到 BulkRepositoryImpl 实例中。
-
你得到的错误是什么?
-
BulkRepositoryImpl 创建没有错误。问题只是没有设置batchSize(默认值为0)。
-
您是否尝试通过构造函数注入值?
标签: java spring repository spring-data-jpa