JPA批量插入
2020-03-18 14:54:39
742 次阅读
0 个评论
int entityCount = 50;
int batchSize = 25;
EntityManager entityManager = entityManagerFactory()
.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
try {
entityTransaction.begin();
for (int i = 0; i < entityCount; i++) {
if (i > 0 && i % batchSize == 0) {
entityTransaction.commit();
entityTransaction.begin();
entityManager.clear();
}
Post post = new Post(
String.format("Post %d", i + 1)
);
entityManager.persist(post);
}
entityTransaction.commit();
} catch (RuntimeException e) {
if (entityTransaction.isActive()) {
entityTransaction.rollback();
}
throw e;
} finally {
entityManager.close();
}
此外,请确保使用以下配置属性启用JDBC批处理:
<property
name="hibernate.jdbc.batch_size"
value="25"
/>
<property
name="hibernate.order_inserts"
value="true"
/>
<property
name="hibernate.order_updates"
value="true"
/>
00
- 0回答
- 1粉丝
- 0关注
相关话题
- Centos8安装MySQL5.7
- Windows安装MySQL8.0
- Centos8下mysql 8.0忘记密码后重置密码
- Centos8卸载MySQL5.7
- CentOS8.0安装OpenJDK 16
- CentOS 8.x系统停止维护yum无法使用解决方法
- mysql-connector-java-8.0.11连接MySQL 8.0问题
- 阿里云CentOS 8.x系统yum报404的解决方法(Errors during downloading metadata for repository 'appstream':
- Eclipse 安装JavaFx插件
- CentOS设置时间和时区
- Centos7关闭kdump
- MySQL 命令大全
- MySQL签到设计
- MySql分表分页
- 安装nodejs v18时报错GLIBC_2.27