OpenJPA是否支持批量插入?

时间:2012-05-23 13:32:30

标签: hibernate jpa openjpa

OpenJPA是否支持类似于Hibernate的批量插入?我没有在文档中找到它,但我希望我错过了它。我知道JPA doesn't support it in general

1 个答案:

答案 0 :(得分:4)

简短的回答,是的。

更长的答案,获取Hibernate文档的链接,并用JPA EntityManager替换Session。

EntityManager em = emf.createEntityManager();
Transaction 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();
em.close();