title: Can an entity be updated instead of being inserted using spring-data-jpa when entity ID is not known without issuing a query?
body: <p>We have a Spring Boot/Data-JPA (1.3.3.RELEASE) application using Hibernate implementation where a CSV file is read and inserted into a database table called FIRE_CSV_UPLOAD. For records that are already present we just update them. 
  We retrieve record ID by querying for unique key (a combination of three columns) but this approach is inefficient for thousands of record in CSV file.</p>
  
  <p>My question is how to update record without querying the table for unique key? If I do not query for ID then the record will be inserted instead of update.</p>
  
  <p>I know one way which is to retrieve all records and store their unique key and ID pairs in a Map. Any other suggestions are very much appreciated. The codes are as below,</p>
  
  <p><strong>Note</strong>: They are minimized for brevity.</p>
  
  <pre><code>@Entity
  @Table(name = "FIRE_CSV_UPLOAD", 
         uniqueConstraints={@UniqueConstraint(columnNames = {"account_number" , "account_type", "bank_client_id"})})
  public class FireCsv {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      @NotNull
      @Column(name="account_number")
      private String accountNumber;
  
      @NotNull
      @Column(name="account_type")
      private String accountType;
  
      @NotNull
      @Column(name="bank_client_id")
      private String bankClientIdNumber;
  
      ...
      other fields/getters/setters
  }
  </code></pre>
  
  <p>--</p>
  
  <pre><code>public interface FireCsvRepository extends JpaRepository&lt;FireCsv, Long&gt; {
  
      @Query("select u from FireCsv u where u.accountNumber = :accountNumber and u.accountType = :accountType and u.bankClientIdNumber = :bankClientIdNumber ")
      FireCsv findRecord(@Param("accountNumber") String accountNumber, 
                         @Param("accountType") String accountType, 
                         @Param("bankClientIdNumber") String bankClientIdNumber);
  }
  </code></pre>
  
  <p>--</p>
  
  <pre><code>@Service
  public class FireCsvServiceImpl implements FireCsvService {
  
      other fields/methods 
      ...
      @Override
      @Transactional
      public FireCsv save(final FireCsv fireCsv) {
  
          FireCsv existingFireCsv = fireCsvRepository.findRecord(fireCsv.getAccountNumber(), fireCsv.getAccountType(), fireCsv.getBankClientIdNumber());
  
          // If record exist then mark as update, if not as insert
          if (existingFireCsv != null) {
              fireCsv.setId(existingFireCsv.getId());
              fireCsv.setRecordStatus(CSVUploadRecordStatus.CSV_UPDATE.getStatus());
          }
          else {
              fireCsv.setRecordStatus(CSVUploadRecordStatus.CSV_INSERT.getStatus());
          }
  
          fireCsv.setRecordStatusDate(new java.sql.Timestamp(new Date().getTime()));
          return fireCsvRepository.save(fireCsv);
      }
  }
  </code></pre>