title: Update one-to-many ArrayList in JPA2.1
body: <pre><code>{
      "id": 1,
      "name": "First-Search",
      "steps": [
          {
              "id": 1,
              "subSearchId": 1,
              "step": 1
          },
          {
              "id": 3,
              "subSearchId": 1,
              "step": 2
          },
          {
              "id": 2,
              "subSearchId": 1,
              "step": 3                
          }
      ]
  }
  </code></pre>
  
  <p>In this json, i want to update the <strong>steps[]</strong> array element step no. 
  if i swap step values , they should update inside db as well,</p>
  
  <p><strong>Condition</strong></p>
  
  <p>My table has Unique key constraints on combination of <strong>subSearchId</strong> and <strong>step</strong></p>
  
  <p>i don't want to delete all the records before update. </p>
  
  <p>is there any best way to update this</p>
  
  <p><strong>mycontroller</strong></p>
  
  <pre><code>JsonNode json = request().body().asJson();
          final SubSearch t = Json.fromJson(json, SubSearch.class);
          SubSearchDao tdao = new SubSearchDao();
          SubSearchStepDao stdao = new SubSearchStepDao();
          SubSearch subSearch = tdao.findById(t.id);
          SubSearchStep sst = null;
          SubSearchStep sstTmp = null;
          boolean orderChanged = false;
  
  //        orderChanged = subSearch.steps.stream().filter(p -&gt; p.step != (t.steps.stream().filter(p1 -&gt; p1.id!=null &amp;&amp; p1.id.equals(p.id)).findFirst()).get().step).findFirst().isPresent();
          for (int i = 0; i &lt; subSearch.steps.size(); i++) {
              sst = t.steps.get(i);
              sstTmp = subSearch.steps.get(i);
              if (sst.id != null) {
                  // true, means steps has been updated
                  if (sst.step != sstTmp.step) {
                      orderChanged = true;
  //                    sst.id=null;
                  }
              }
          }
          if (orderChanged) {
              stdao.deleteAll(subSearch);
              return ok(Json.toJson(tdao.update(t)));
          } else {
              return ok(Json.toJson(tdao.update(t)));
          }
  </code></pre>
  
  <p>right now i am deleting all entries before update, if i get any order changes.</p>
  
  <p><strong>Database:</strong></p>
  
  <pre><code>      id  subSearchId    step  
  ------  -----------  --------
      1            1         1
      2            1         3
      3            1         2
  </code></pre>
  
  <p>i want swap <strong>step</strong> and (<strong>subSearchId    step</strong>) is unique.</p>