使用 JPA/EJB3 批量插入 [英] Batch inserts with JPA/EJB3

查看:32
本文介绍了使用 JPA/EJB3 批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does JPA/EJB3 framework provide standard way to do batch insert operation...? We use hibernate for persistence framework, So I can fall back to Hibernate Session and use combination session.save()/session.flush() achieve batch insert. But would like to know if EJB3 have a support for this...

推荐答案

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

Neither JPA nor Hibernate do provide particular support for batch inserts and the idiom for batch inserts with JPA would be the same as with 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 提供任何优势.

Using Hibernate's proprietary API in this case doesn't provide any advantage IMO.

  • JPA 1.0 规范
    • 第 4.10 节批量更新和删除操作"

    这篇关于使用 JPA/EJB3 批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆