Batch inserts with JPA/EJB3

后端 未结 5 500
说谎
说谎 2020-12-01 10:45

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 com

5条回答
  •  庸人自扰
    2020-12-01 11:16

    With medium records number you can use this way:

    em.getTransaction().begin();
    for (int i = 1; i <= 100000; i++) {
         Point point = new Point(i, i);
         em.persist(point);
         if ((i % 10000) == 0) {
              em.flush();
              em.clear();
         }
    }
    em.getTransaction().commit();
    

    But with large records number you should perform this task in multiple transactions:

    em.getTransaction().begin();
    for (int i = 1; i <= 1000000; i++) {
          Point point = new Point(i, i);
          em.persist(point);
          if ((i % 10000) == 0) {
              em.getTransaction().commit();
              em.clear();          
              em.getTransaction().begin();
          }
    }
    em.getTransaction().commit();
    

    Ref: JPA Batch Store

提交回复
热议问题