title: Spring Data JPA performnace issue with batch save
body: <p>I have the following Entity</p>
  
  <pre><code>@Entity
  @Table(name = "APP_ITEM")
  public class AppItem implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @EmbeddedId
  private AppItemPK AppItemPK;
  
   public AppItemPK getAppItemPK() {
      return appItemPK;
  }
  
  public void setAppItemPK(
          AppItemPK appItemPK) {
      this.appItemPK = appItemPK;
  }
  } 
  
  
  @Embeddable
  public class AppItemPK implements Serializable {
  
  private static final long serialVersionUID = 1L;
  @Column(name = "app_id")
  private Long appId;
  @Column(name = "item_id")
  private Long itemId;
  
  public Long getAppId() {
      return appId;
  }
  
  public void setAppId(Long appId) {
      this.appId = appId;
  }
  
  public Long getItemId() {
      return itemId;
  }
  
  public void setItemId(Long itemId) {
      this.itemId = itemId;
  }
  
  public boolean equals(Object obj) {
      if (obj instanceof AppItemPK) {
          AppItemPK appItemPK = (AppItemPK) obj;
          if (appItemPK.getItemId().equals(this.itemId)
                  &amp;&amp; appItemPK.getAppId().equals(
                          this.appId)) {
              return true;
          }
      }
      return false;
  }
  
  public int hashCode() {
      return this.itemId.hashCode() + this.applicationId.hashCode();
  }
  
  }
  </code></pre>
  
  <p>Using below code to insert record into app_item table</p>
  
  <pre><code>@Transactional(readOnly = false)
  public boolean saveItemSelection(PageHeaderViewData pageHeaderViewData, Map&lt;Long, Boolean&gt; selectedItems,String savedItems){
      long millisSaveStart = Calendar.getInstance().getTimeInMillis();
      log.debug("Inside saveItemSelection appId:"+pageHeaderData.getAppId());
      boolean saveStatus = false;     
      List&lt;AppItem&gt; appItemInsList = new ArrayList&lt;SavedApplicationItem&gt;();
  
      if (pageHeaderData.getAppId() != null) {
  
           for (Entry&lt;Long, Boolean&gt; idEntry : selectedItems.entrySet() ) {
               if (idEntry!= null){                    
  
                       if (idEntry.getValue() &amp;&amp; !savedItems.contains(idEntry.getKey().toString())){                           
                           //log.debug("Inside saveItemSelection SAVED itemId:"+idEntry.getKey()+" , Value:"+idEntry.getValue());          
                           AppItem appItem = new AppItem();
                           AppItemPK appItemPK = new AppItemPK();
                           appItemPK.setAppId(pageHeaderData.getAppId());
                           appItemPK.setItemId(idEntry.getKey());
                           appItem.setAppItemPK(appItemPK);
                           appItem.setUpdateInd(ToggleEnum.Y);                        
                           appItemInsList.add(appItem);
                           //appItemRepository.saveAndFlush(appItem);                          
                       }
                   }
               }
  
  
           } 
  
  
           if (appItemInsList.size() != 0){
               long millisJPASaveStart = Calendar.getInstance().getTimeInMillis();
               appItemRepository.save(appItemInsList);    
               long millisJPASaveEnd = Calendar.getInstance().getTimeInMillis();
               log.debug("JPA save time:"+(millisJPASaveEnd-millisJPASaveStart));
           }
  
           saveStatus = true;
  
           long millisSaveEnd = Calendar.getInstance().getTimeInMillis();
           log.debug("Total save time:"+(millisSaveEnd-millisSaveStart));
      }
  
      return saveStatus;
  
  }//end of saveItemSelection
  </code></pre>
  
  <p>For inserting 5000 records it is taking 13826 milliseconds. </p>
  
  <p>Can someone please let me know, how to improve the performance in above JPA code. We are using hibernate for jpa implementation.</p>