Does OpenJPA have any support for batch insert?

陌路散爱 提交于 2019-12-08 02:03:43

问题


Does OpenJPA have any support for batch insert similar to Hibernate? I haven't found it in the docs, but I'm hoping I missed it. I know JPA doesn't support it in general.


回答1:


Short answer, yes.

Longer answer, take the link to Hibernate documentation and replace the Session with a JPA EntityManager.

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


来源:https://stackoverflow.com/questions/10721038/does-openjpa-have-any-support-for-batch-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!