title: How can I implement Hibernate massive batch insert into this Spring Boot application?
body: <p>I am pretty new in Hibernate and I have the following problem: I have to perform a massive insert operation (I need to insert a lot of the model object and each of these object contains a <strong>byte[]</strong> field mapping a <strong>blob</strong> field on the DB).</p>
  
  <p>I am using <strong>Spring Boot</strong> (so I need to configure it on Spring Boot) and <strong>Spring Data JPA</strong>.</p>
  
  <p>So I have the following situation:</p>
  
  <p>1) <strong>RoomMedia</strong> my model object:</p>
  
  <pre><code>@Entity
  @Table(name = "room_media")
  public class RoomMedia {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
      //@Column(name = "id_room")
      //private Long idRoom;
  
      @ManyToOne
      @JoinColumn(name = "id_room", nullable = false)     // da rinominare anche sul DB in room_fk
      private Room room;
  
      @Lob
      @Column(name = "media")
      private byte[] media;
  
      private String description;
  
      private Date time_stamp;
  
      public RoomMedia() {
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      ........................................................................
      ........................................................................
      GETTER AND SETTER METHODS
      ........................................................................
      ........................................................................
  }
  </code></pre>
  
  <p>Then into another class I have this code that persist a list of <strong>RoomMedia</strong> objects:</p>
  
  <p>roomMediaDAO.save(mediaListToBeInserted);</p>
  
  <p>where <strong>mediaListToBeInserted</strong> is a <strong>List</strong>.</p>
  
  <p>The problm is that doing in this way it is very slow because this statment generate insertion queries like these:</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 the records are inserted one by one.</p>
  
  <p>I need to insert these records massively, genereting a code like this:</p>
  
  <pre><code>INSERT INTO MyTable ( Column1, Column2 ) VALUES
  ( Value1, Value2 ), 
  ( Value1, Value2 ),
  ( Value1, Value2 ),
  .................................
  .................................
  .................................
  ( Value1, Value2 )
  </code></pre>
  
  <p>Reading this documentation: 
  <a href="https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html" rel="nofollow noreferrer">https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html</a>
  I know that I can set Hibernate to perform <strong>Batch Insert</strong> that should do something like this.</p>
  
  <p>But I have some doubts:</p>
  
  <p>1) Reading the previous documentation link it says that:</p>
  
  <blockquote>
    <p>Hibernate disables insert batching at the JDBC level transparently if
    you use an identity identifier generator.</p>
  </blockquote>
  
  <p>and I have that my primary key of my model object is annoted with:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private Long id;
  </code></pre>
  
  <p>So what it means? That I can't do massive insert of a list of these objects? It seem me strange that there is no way to do it.</p>
  
  <p>In the previous article it explain:</p>
  
  <blockquote>
    <p>You can also do this kind of work in a process where interaction with
    the second-level cache is completely disabled:</p>
    
    <p>hibernate.cache.use_second_level_cache false</p>
    
    <p>However, this is not absolutely necessary, since we can explicitly set
    the CacheMode to disable interaction with the second-level cache.</p>
  </blockquote>
  
  <p>So it seem to me that I can do it in some way but I don't know what is this <strong>second-level cache</strong> and why it have to be disabled to perform batch insert of object having an identity identifier generator setted.</p>
  
  <p>2) In my <strong>Spring Boot</strong> project I have this <strong>application.properties</strong> file containing the following settings related to the database connection and Hibernate configuration. So I think that this should be the point where to add the set to handle the batch massive insert. But what have I to set?</p>
  
  <pre><code>#No auth  protected
  endpoints.shutdown.sensitive=true
  #Enable shutdown endpoint
  endpoints.shutdown.enabled=true
  
  spring.datasource.url = jdbc:mysql://XX.YY.ZZZ.WW:3306/BeTriviusTest?useUnicode=true&amp;characterEncoding=UTF-8
  spring.datasource.username = MyUserName
  spring.datasource.password = MyPswd
  
  spring.datasource.driverClassName=com.mysql.jdbc.Driver
  
  # Keep the connection alive if idle for a long time (needed in production)
  spring.datasource.testWhileIdle = true
  spring.datasource.validationQuery = SELECT 1
  
  # Show or not log for each sql query
  spring.jpa.show-sql = true
  spring.jpa.database = mysql
  # Hibernate ddl auto (create, create-drop, update)
  spring.jpa.hibernate.ddl-auto = validate
  
  # Naming strategy
  spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  
  # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
  # stripped before adding them to the entity manager)
  
  # The SQL dialect makes Hibernate generate better SQL for the chosen database
  spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect
  spring.jpa.database-platform = org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect
  </code></pre>