title: Hibernate - very slow insert
body: <p>So I try to insert 30000 records (well the final solution would be to insert like 300000 or more records) with Hibernate.
  My problem is that inserting 300 records took like 8-9 seconds which is very slow. The database is Oracle. I tried batch processing but no speed-up.</p>
  
  <p>Hibernate config:</p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      ...
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.max_fetch_depth"&gt;0&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.current_session_context_class"&gt;ch.nevis.estivate.util.TransactionAwareSessionContext&lt;/prop&gt;
              &lt;prop key="javax.persistence.validation.mode"&gt;none&lt;/prop&gt;
              &lt;prop key="hibernate.validator.autoregister_listeners"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.validator.apply_to_ddl"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;false&lt;/prop&gt;
  
              &lt;prop key="hibernate.jdbc.batch_size"&gt;30&lt;/prop&gt;
              &lt;prop key="hibernate.order_inserts"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>@Transactional
  public class DataGenerator {
  
  public void generate(int nr) {
      System.out.println("START   " + new Date());
      int q = nr / 100;
      Role role = (Role) sessionFactory.getCurrentSession().get(Role.class, new Long(4));
      Client client = (Client) sessionFactory.getCurrentSession().get(Client.class, new Long(100));
      Unit unit = (Unit) sessionFactory.getCurrentSession().get(Unit.class, new Long(100));
      TemplateCollection templateCollection = (TemplateCollection) sessionFactory.getCurrentSession().get(TemplateCollection.class, new Long(100));
      for(int i = 0; i &lt; nr; i++) {
          User user = generateUser(i, client, templateCollection);
          sessionFactory.getCurrentSession().save(user);
  
          Profile profile = generateProfile(i, user, unit, client);
          sessionFactory.getCurrentSession().save(profile);
  
          Authorization authorization = generateAuthorization(i, profile, role);
          sessionFactory.getCurrentSession().save(authorization);
  
          if (i % q == 0) {
              System.out.println(i/q + " %     " + new Date());
          }
          if (i % 10 == 0) {  // the batch_size is 30 but I save 3 entities in one go
              sessionFactory.getCurrentSession().flush();
              sessionFactory.getCurrentSession().clear();
          }
      }
      System.out.println("END   " + new Date());
  }
  </code></pre>
  
  <p>Any hint please?<br>
  Perhaps Hibernate is not appropriate to load many records?</p>
  
  <p>Thanks,<br>
  V.</p>
  
  <p>------------ UPDATE -------------<br>
  Removed the custom session context class (i.e. Spring's CurrentSessionContext is in use) but no speed-up.</p>
  
  <p>------------ UPDATE 2 -------------     </p>
  
  <pre><code>public static void main(String[] args) {
      ApplicationContext ctx = new FileSystemXmlApplicationContext("file:/......./applicationContext-testDataGenerator.xml");
      DataGenerator dataGenerator= ctx.getBean("dataGenerator", DataGenerator.class);
      dataGenerator.generate(10000);
  }
  </code></pre>