关于 JPA PostgreSQL中使用 GenerationType.IDENTITY 批处理失效
2021/6/6 19:24:55
本文主要是介绍关于 JPA PostgreSQL中使用 GenerationType.IDENTITY 批处理失效,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@NoRepositoryBean public interface BatchRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { <S extends T> void saveInBatch(Iterable<S> entites); }
@Transactional(readOnly = true) public class BatchRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BatchRepository<T, ID> { private static final Logger logger = Logger.getLogger(BatchRepositoryImpl.class.getName()); private final EntityManager entityManager; public BatchRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); this.entityManager = entityManager; } @Override @Transactional public <S extends T> void saveInBatch(Iterable<S> entities) { if (entities == null) { throw new IllegalArgumentException("The given Iterable of entities cannot be null!"); } int i = 0; for (S entity : entities) { entityManager.persist(entity); i++; // Flush a batch of inserts and release memory if (i % batchSize() == 0 && i > 0) { logger.log(Level.INFO, "Flushing the EntityManager containing {0} entities ...", i); entityManager.flush(); entityManager.clear(); i = 0; } } if (i > 0) { logger.log(Level.INFO, "Flushing the remaining {0} entities ...", i); entityManager.flush(); entityManager.clear(); } } private static int batchSize() { int batchsize = Integer.valueOf(Dialect.DEFAULT_BATCH_SIZE); // default batch size Properties configuration = new Properties(); try (InputStream inputStream = BatchRepositoryImpl.class.getClassLoader() .getResourceAsStream("application.properties")) { configuration.load(inputStream); } catch (IOException ex) { logger.log(Level.SEVERE, "Cannot fetch batch size. Using further Dialect.DEFAULT_BATCH_SIZE{0}", ex); return batchsize; } String batchsizestr = configuration.getProperty( "spring.jpa.properties.hibernate.jdbc.batch_size"); if (batchsizestr != null) { batchsize = Integer.valueOf(batchsizestr); } return batchsize; } }
Why To Avoid PostgreSQL (BIG)SERIAL In Batching Inserts Via Hibernate
https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchingAndSerial
自增列 EntityManager (MySQL)
https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertsEntityManagerBatchPerTransaction
这篇关于关于 JPA PostgreSQL中使用 GenerationType.IDENTITY 批处理失效的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-05快速清空 PostgreSQL 数据库中的所有表格,让你的数据库重新焕然一新!
- 2024-01-04在PostgreSQL中创建角色:判断角色是否存在并创建
- 2023-05-16PostgreSQL一站式插件推荐 -- pg_enterprise_views
- 2022-11-22PostgreSQL 实时位置跟踪
- 2022-11-22如何将PostgreSQL插件移植到openGauss
- 2022-11-11PostgreSQL:修改数据库用户的密码
- 2022-11-06Windows 环境搭建 PostgreSQL 物理复制高可用架构数据库服务
- 2022-10-27Windows 环境搭建 PostgreSQL 逻辑复制高可用架构数据库服务
- 2022-10-11PostgreSql安装(Windows10版本)
- 2022-09-13PostgreSQL-Network Address类型操作和函数