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

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

问题描述

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

解决方案

JPA和Hibernate都没有为批处理提供特别的支持插入和用JPA批量插入的习惯用法和Hibernate一样:

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

(int i = 0; i <100000; i ++){
Customer customer = new Customer(.....);
em.persist(customer);
if(i%20 == 0){// 20,与JDBC批量大小相同
//刷新一批插入并释放内存:
em.flush();
em.clear();
}
}

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

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



引用




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...

解决方案

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();

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

References

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

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