title: Spring batch Custom itemWriter with insert or update mechanism
body: <p>I have <code>Spring Batch</code> with a basic chunk:</p>
  
  <ol>
  <li>Read a CSV file.</li>
  <li>Process it (Transcode some coded value into another ones).</li>
  <li>Write the result into a database.</li>
  </ol>
  
  <p><strong>The problem</strong></p>
  
  <p>In some case, I want insert or update mechnism in my custom <code>ItemWriter</code>.</p>
  
  <p>Exemple: I can get this 2 lines on my <code>CSV</code></p>
  
  <pre><code>C006;Test;OK;01/01/1970;1
  C006;Test;OK;01/01/1970;5
  </code></pre>
  
  <p>You can figure out that they are pretty similar except the last column, the strategy will be:</p>
  
  <ol>
  <li>Check the database if I have a "like so" entity</li>
  <li>if true, update the value with the last item that you get (in our case, the second line with the value 5 in the last column)</li>
  </ol>
  
  <p><strong>What I already did?</strong> </p>
  
  <p>In my entity bean, I added a <em>Unique Constraint</em> anotation like so:</p>
  
  <pre><code>@Table(name = "indicator",
      uniqueConstraints = { @UniqueConstraint(columnNames = 
                                          { "reference", "type", "status", "date" }) })
  </code></pre>
  
  <p>I'm sure now that I cannot persist an entity with the same column's data. Then, I created a custom <code>ItemWriter</code>, and I tried to catch the <em>ConstraintViolationException</em> but it didn't work, I always get an other exeption, even when I tried the parent one.</p>
  
  <p>So do you have any ideas or another way to do it?</p>
  
  <p>I was think about using merge JPA functionality? what do you think about it?</p>
  
  <p><strong>My custom ItemWriter</strong></p>
  
  <pre><code>@Component
  public class ImportItemWriter implements ItemWriter&lt;Indicator&gt;{
  
      @Autowired
      protected IndicatorDao indicatorDao;
  
      public void write(List&lt;? extends Indicator&gt; items) throws Exception {
  
          for (Indicator item : items) {
              Indicator indicator = new Indicator();
  
              indicator.setReference(item.getReference());
              indicator.setType(item.getType());
              indicator.setStatus(item.getStatus());
              indicator.setDueDate(item.getDueDate());
  
              indicator.setValue(item.getValue());
  
              try {
                  indicatorDao.persist(indicator);
              } catch (ConstraintViolationException e) {
                  // TODO: handle exception
              }       
          }
      }
  }
  </code></pre>
  
  <p><strong>Update</strong>
  <strong><em>Problem Solved</em></strong>
  The idea of using <code>Composite PKey</code> is interesting, but I can't use it because I have to create a composite with 9 keys that's not fair in term of performance. I decided to add function to my <code>DAO</code> (<code>isDuplicated</code>) and in my custom <code>ItemWriter</code>, I just make a simple test:</p>
  
  <pre><code>if `isDuplicated()` then `updateEntity()` else `insertNew()`
  </code></pre>