title: JPA inserts optimization
body: <p>I have a jpa code that genrates players (10000) and for each player 100 game and inserts them in a db but it's so slow i've tried using btch writing where i've set the btch size and then used the flush / clean methods but that hasn't changed much </p>
  
  <pre><code>
    List&lt;Player&gt; players = new ArrayList&lt;&gt;();
    Random rd = new Random();
  
    @Override
    public void createMassData() {
      EntityManager em = lab02EntityManager.getEntityManager();
      EntityTransaction tx = em.getTransaction();
      List&lt;Category&gt; allCategories =
          lab02EntityManager
              .getEntityManager()
              .createQuery("select c from Category c")
              .getResultList();
      players = createPlayers();
      tx.begin();
      int trans = 1000;
      int index=0;
      for (Player player : players) {
        em.persist(player);
        index++;
        for (int i = 0; i &lt; 10; i++) {
          em.persist(playGame(player, allCategories));
          index++; }
          if (index &gt; trans){
            index=0;
            em.flush();
            em.clear();
          }
        }
       System.out.println("created ..");
      tx.commit();
    }
  
  
  </code></pre>