title: How can I do massive insert using Hibernate?
body: <p>I am pretty new to <strong>Spring Data JPA</strong> and <strong>Hibernate</strong> and I have the following problem with massive record insertion.</p>
  
  <p>I have the following situation:</p>
  
  <p>1) A <strong>RoomMediaDAO</strong> Spring Data JPA interface:</p>
  
  <pre><code>@Repository
  @Transactional(propagation = Propagation.MANDATORY)
  public interface RoomMediaDAO extends JpaRepository&lt;RoomMedia, Long&gt; {
  
     ....................................................................
     ....................................................................
     ....................................................................
  }
  </code></pre>
  
  <p>2) Into another class I use this <strong>RoomMediaDAO</strong> to persist a list of <strong>RoomMedia</strong> object. The list is pretty big and heavy because contains many object and each oof these objects contains a <strong>byte[]</strong> field representing a <strong>BLOB</strong> on the mapped table.</p>
  
  <p>So I have the follosing statment that persist the <strong>List mediaListToBeInserted</strong>:</p>
  
  <pre><code>roomMediaDAO.save(mediaListToBeInserted);
  </code></pre>
  
  <p>It work fine but it is very slow because it perform the insert one by one, infact in the console I can see something like this:</p>
  
  <pre><code>Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
  Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
  Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
  Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
  Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
  ............................................................................
  ............................................................................
  ............................................................................
  </code></pre>
  
  <p>So this is not a good solution.</p>
  
  <p>How can I say to Hibernate to insert more record with a single insert statment? I mean can I do something like this using Hibernate?</p>
  
  <pre><code>INSERT INTO MyTable ( Column1, Column2 ) VALUES
  ( Value1, Value2 ), 
  ( Value1, Value2 ),
  ( Value1, Value2 ),
  .................................
  .................................
  .................................
  ( Value1, Value2 )
  </code></pre>
  
  <p>I absolutly need something like this to improve the performance of my batch.</p>