title: JPA/Hibernate bulk(batch) insert
body: <p>Here is simple example I've created after reading several topics about jpa bulk inserts, I have 2 persistent objects User, and Site. One user could have many site, so we have one to many relations here. Suppose I want to create user and create/link several sites to user account. Here is how code looks like, considering my willing to use bulk insert for Site objects.</p>
  
  <pre><code>User user = new User("John Doe");
  
  user.getSites().add(new Site("google.com", user));
  user.getSites().add(new Site("yahoo.com", user));
  
  EntityTransaction tx = entityManager.getTransaction();
  tx.begin();
  entityManager.persist(user);
  tx.commit();
  </code></pre>
  
  <p>But when I run this code (I'm using hibernate as jpa implementation provider) I see following sql output:</p>
  
  <pre><code>Hibernate: insert into User (id, name) values (null, ?)
  Hibernate: call identity()
  Hibernate: insert into Site (id, url, user_id) values (null, ?, ?)
  Hibernate: call identity()
  Hibernate: insert into Site (id, url, user_id) values (null, ?, ?)
  Hibernate: call identity()
  </code></pre>
  
  <p>So, I means "real" bulk insert not works or I am confused? </p>
  
  <p>Here is <a href="https://docs.google.com/leaf?id=0BzMGtdLKSCAgZjIyZTM1ZGItODM2OC00MmJmLWE1YTAtYmE0NmM4YTVhYmFi&amp;hl=en" rel="noreferrer">source code</a> for this example project, this is maven project so you have only download and run mvn install to check output.</p>
  
  <p>UPDATED:</p>
  
  <p>After Ken Liu kindly advise, I've disabled Site object id auto generation:</p>
  
  <pre><code>    User user = new User("John Doe");
      user.getSites().add(new Site(1, "google.com", user));
      user.getSites().add(new Site(2, "yahoo.com", user));
      entityManager.setFlushMode(FlushModeType.COMMIT);
      EntityTransaction tx = entityManager.getTransaction();
      tx.begin();
      entityManager.persist(user);
      tx.commit();
  </code></pre>
  
  <p>Now I have following line in debug output:</p>
  
  <p>DEBUG: org.hibernate.jdbc.AbstractBatcher - Executing batch size: 2</p>
  
  <p>It works!</p>