gpt4 book ai didi

java - 使用 JPA/EJB3 进行批量插入

转载 作者:IT老高 更新时间:2023-10-28 20:46:22 24 4
gpt4 key购买 nike

JPA/EJB3 框架是否提供标准方式来执行批量插入操作...?我们使用hibernate作为持久化框架,所以我可以回退到Hibernate Session并使用组合session.save()/session.flush()实现批量插入。但是想知道 EJB3 是否支持这个...

最佳答案

JPA 和 Hibernate 都没有为批量插入提供特别支持,使用 JPA 进行批量插入的习惯用法与使用 Hibernate 时相同:

EntityManager em = ...;
EntityTransaction tx = em.getTransaction();
tx.begin();

for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
em.persist(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
em.flush();
em.clear();
}
}

tx.commit();
session.close();

在这种情况下使用 Hibernate 的专有 API 并没有提供任何 IMO 优势。

引用文献

关于java - 使用 JPA/EJB3 进行批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/448181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com